Question by
Vincent_89 · Jan 03, 2021 at 09:10 AM ·
cameramovement
3rd person controller with static cameras
I'm trying to do a 3rd person controller with static cameras (like ps1 resident evil) I have this code which is working fine so far:
public CharacterController cc;
public float speed = 1.5f;
public Animator anim;
public float turnSmoothTime = 0.1f;
float turnSmoothVelocity;
void Update ()
{
float horizontal = Input.GetAxisRaw ("Horizontal");
float vertical = Input.GetAxisRaw ("Vertical");
Vector3 direction = new Vector3 (horizontal, 0, vertical).normalized;
if (direction.magnitude >= 0.1f)
{
float targetAngle = Mathf.Atan2 (-direction.z, direction.x) * Mathf.Rad2Deg;
float angle = Mathf.SmoothDampAngle (transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler (0f, angle, 0f);
cc.Move (direction * speed * Time.deltaTime);
}
}
The problem that I have is the relation between the cameras relative positions and the character position. For instance: if camera rotation Y is 0 the code is working fine but if camera rotation Y is 180 when I press right the player go left and viceversa. How can I fix that if camera rotation is 180 and I press left the player will go left and press right the player will go right?
Comment