- Home /
player movement with camera direction ?
i was follow unity tutorial for character movement but i have problem when i rotate the cam"i use cinmachaine freelook cam" the movement still in world direction i tried to change the the Vector3.RotateTowards from player to main camera but this mead the movement worse how can i change it to make the movement with cam direction thank you
public class playermovement : MonoBehaviour
{
public float turnSpeed = 20f;
Vector3 m_movment;
Animator m_animator;
Rigidbody m_Rigidbody;
AudioSource m_audioSource;
Quaternion m_Rotation = Quaternion.identity;
// Start is called before the first frame update
void Start()
{
m_animator = GetComponent<Animator>();
m_Rigidbody = GetComponent<Rigidbody>();
m_audioSource = GetComponent<AudioSource>();
}
// Update is called once per frame
void FixedUpdate()
{
float haxie = Input.GetAxis("Horizontal");
float vaxie = Input.GetAxis("Vertical");
m_movment.Set(haxie, 0f, vaxie);
m_movment.Normalize();
bool hashorizontalinput = !Mathf.Approximately(haxie, 0f);
bool hasverticalinput = !Mathf.Approximately(vaxie, 0f);
bool isWalking = hashorizontalinput || hasverticalinput;
m_animator.SetBool("Iswalking", isWalking);
if (isWalking)
{
if (!m_audioSource.isPlaying)
{
m_audioSource.Play();
}
}
else
{
m_audioSource.Stop();
}
Vector3 desiredForward = Vector3.RotateTowards(transform.forward, m_movment, turnSpeed * Time.deltaTime, 0f);
m_Rotation = Quaternion.LookRotation(desiredForward);
}
private void OnAnimatorMove()
{
m_Rigidbody.MovePosition(m_Rigidbody.position + m_movment *
m_animator.deltaPosition.magnitude);
m_Rigidbody.MoveRotation(m_Rotation);
}
Answer by lTimesl · Jul 23, 2019 at 12:20 PM
ok i manged to figure it out all i was need to do is to pass the player transform to the maincam transformdirection using transform.transformdirection and its work perfect for me here is the final code if any body looking for something like this
public class playermovement : MonoBehaviour
{
public float turnSpeed = 20f;
public Camera m_Camera;
Vector3 m_movment, realativedir;
Animator m_animator;
Rigidbody m_Rigidbody;
AudioSource m_audioSource;
Quaternion m_Rotation = Quaternion.identity;
// Start is called before the first frame update
void Start()
{
m_animator = GetComponent<Animator>();
m_Rigidbody = GetComponent<Rigidbody>();
m_audioSource = GetComponent<AudioSource>();
}
// Update is called once per frame
void FixedUpdate()
{
float haxie = Input.GetAxis("Horizontal");
float vaxie = Input.GetAxis("Vertical");
m_movment.Set(haxie, 0f, vaxie);
realativedir = Camera.main.transform.TransformDirection(m_movment);
realativedir.y = 0f;
realativedir.Normalize();
bool hashorizontalinput = !Mathf.Approximately(haxie, 0f);
bool hasverticalinput = !Mathf.Approximately(vaxie, 0f);
bool isWalking = hashorizontalinput || hasverticalinput;
m_animator.SetBool("Iswalking", isWalking);
if (isWalking)
{
if (!m_audioSource.isPlaying)
{
m_audioSource.Play();
}
}
else
{
m_audioSource.Stop();
}
Vector3 desiredForward = Vector3.RotateTowards(transform.forward, realativedir, turnSpeed * Time.deltaTime, 0f);
m_Rotation = Quaternion.LookRotation(desiredForward);
}
private void OnAnimatorMove()
{
m_Rigidbody.MovePosition(m_Rigidbody.position + realativedir *
m_animator.deltaPosition.magnitude);
m_Rigidbody.MoveRotation(m_Rotation);
}
Your answer
Follow this Question
Related Questions
How to make a movement at a distance equal to its diameter, which cannot pass through objects? 1 Answer
Camera Script Amalgamation == Camera Script Abomination, Im In Over My Head 0 Answers
Colliding an object 1 Answer
Can you help with the character controller 1 Answer
Move Object to location of Trigger? 1 Answer