3rd person movement not going with position of camera,3rd person character not moving to the direction with the camera
the movement is not changing to the way the camera is facing. here is the code
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ThirdPersonMovement : MonoBehaviour { public CharacterController controller;
public float speed = 6f;
public float turnSmoothTime = 0.1f;
// Update is called once per frame
void Update()
{
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
if(direction.magnitude >= 0.1f)
{
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, targetAngle, 0f);
controller.Move(direction * speed * Time.deltaTime);
}
}
} ,the movement directions are in the same direction no matter the way the camera is facing. here is the code
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ThirdPersonMovement : MonoBehaviour { public CharacterController controller;
public float speed = 6f;
public float turnSmoothTime = 0.1f;
// Update is called once per frame
void Update()
{
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
if(direction.magnitude >= 0.1f)
{
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, targetAngle, 0f);
controller.Move(direction * speed * Time.deltaTime);
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613061429im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How can I make the character turn to face the camera? 0 Answers
Object with fixed position but follows camera rotation 0 Answers
3rd person character controller rotation keeps resetting to 0 0 Answers
How to fix "the type or namespace ".." could not be found" in unity 0 Answers
3rd Person Movement / Camera Control Help or Advice Needed 1 Answer