- Home /
How to set NavMeshAgent to move acording to own gameObject.tranform.forward?
Ok, i'm studing about the NavMesh componets implementing a game where the Camera always follow the player from behind. My problem is i want to move my player forward acording to my gameObject.transform.forward:
Vector3 targetPosition = new Vector3 (horizontalImput, 0f, verticalImput);
and send it to the NavMeshAgent handle the movimentation:
navMeshAgent.Move (targetPosition speed Time.deltaTime);
the NavMeshAgent move my gameObject correctly if his transform.foward is the same as the global Y axis but my object rotate (45º for example) how to i handle it?
After a while, ins$$anonymous$$d of using the input values to make the targetPosition i tried to use the transform.forward to create the targetPosition but i've notice that the nav$$anonymous$$eshAgent.$$anonymous$$ove doesn't work the way i was thinking.
Vector3 targetPosition = transform.forward; targetPosition.x = targetPosition.x horizontalImput; targetPosition.y = 0f; targetPosition.z = targetPosition.z verticalImput;
I've also tried to:
targetPosition.x = horizontalImput $$anonymous$$athf.Cos(rotationAngle) + verticalImput $$anonymous$$athf.Sin(rotationAngle); targetPosition.z = horizontalImput $$anonymous$$athf.Sin(rotationAngle) + verticalImput $$anonymous$$athf.Cos(rotationAngle);
but this operation didn't work cause the $$anonymous$$athf.Sin and $$anonymous$$athf.Cos was overflowing.
So if there is any kind of tip it would be helpfull =)
Answer by gabrielbl · Sep 14, 2015 at 10:19 AM
Well, after a while doing other suff i manage to get a solution!
// Create a target position according to input and lookingPosition.
Vector3 verticalMovimentation = transform.forward * verticalImput;
Vector3 horizontalMovimentation = transform.right * horizontalImput;
float slerpTValue = Mathf.Abs(verticalImput / horizontalImput)/2;
Vector3 targetPosition = Vector3.Slerp(horizontalMovimentation, verticalMovimentation, slerpTValue);
targetPosition.y = 0.0f;
// Move player
navMeshAgent.Move(targetPosition * speed * Time.deltaTime);
not sure if it is the best way of doing it but fit as glove to me.