Hi, I’m trying to make my own player controller and camera script because I read some bad comment about the standard controller. I want to make a third person controller and camera similar to this awesome game called Splatoon.
I try to create my own and here is what I come up after searching and reading some tutorials.
PlayerControl.cs
using UnityEngine;
using System.Collections;
public class PlayerControl : MonoBehaviour
{
public Rigidbody bulletPrefab;
public Transform gunEnd;
public float movingSpeed = 6f;
Vector3 playerMovement;
Rigidbody playerRigidbody;
void Awake ()
{
playerRigidbody = GetComponent<Rigidbody> ();
}
void Update ()
{
float h = Input.GetAxis ("Horizontal");
float v = Input.GetAxis ("Vertical");
Move (h, v);
Fire ();
}
void Move(float h, float v)
{
playerMovement.Set (h, 0f, v);
playerMovement = playerMovement.normalized * movingSpeed * Time.deltaTime;
playerMovement = transform.TransformDirection (playerMovement);
playerRigidbody.MovePosition (transform.position + playerMovement);
}
void Fire()
{
if (Input.GetButton("Fire1"))
{
Rigidbody bulletInstance;
bulletInstance = Instantiate(bulletPrefab, gunEnd.position, gunEnd.rotation) as Rigidbody;
bulletInstance.AddForce(gunEnd.forward * 5000);
}
}
}
CameraFollow.cs
using UnityEngine;
using System.Collections;
public class CameraFollow : MonoBehaviour
{
public GameObject target;
public float sensitivityX = 5f;
Vector3 offset;
void Awake ()
{
offset = target.transform.position - transform.position;
}
void Update ()
{
float x = Input.GetAxis ("Mouse X") * sensitivityX;
Rotate (x);
}
void Rotate(float x)
{
target.transform.Rotate (0f, x, 0f);
float desiredAngle = target.transform.eulerAngles.y;
Quaternion rotation = Quaternion.Euler (0f, desiredAngle, 0f);
transform.position = target.transform.position - (rotation * offset);
transform.LookAt (target.transform);
}
}
Is this correct? and how can I make the movement and rotation to look good. And I can’t make my character to look down and up because when I do the similar thing I do in left and right rotation, my character also rotate. Is there a similar free camera in assets store? or is there a basic tutorial that I can follow. Thanks