- Home /
Transform player from one position to another after completing a goal
Hello everybody, I have a really silly question as I've been stuck with my code for awhile and I have no idea how to make it work the way I want it to. So after completing a goal the Player is supposed to transform to another place automatically. I've tried following codes:
Public void Move{
transform.position = Vector3.MoveTowards(new Vector3(0, 0, 0), new Vector3(0, 100, 0), 20);
}
however, when I use this code then nothing happens. The player just stands still. The only way I've managed to make a player move is by using this code:
Transform.localPosition = Vector3.MoveTowards(childTransform.localPosition, nextPos,speed * Time.deltaTime);
However, then the Player keeps moving without stopping. Could anyone tell me how I could make this work? Thank you very much.
Answer by kog513 · Jul 17, 2018 at 08:45 PM
I think the easiest way is to attach a NavMeshAgent to ur player , activate it if the goal is done , setting it's destination to the position u want , and then when his is no longer moving disabling it again ! if u r unfamiliar with the NavMeshAgent , u need to set the obstacls as Navigation static and decide whether they should be walkable or not , then bake it . add a NavMeshAgent component to the player , accessing it thro code is pretty easy , add Using UnityEngine.AI ; then creat a Variable like : private NavMeshAgent MyAgent or whatever u wanna call it , public if u wanna just drag and drop the component , or u can just get it thro : MyAgent = GetComponent() ; in the start methode .. then u can add a bool which decide if the goal is done , disable ur normal mvmnt system by that bool and add this command : MyAgent.SetDestination(vector3) ur wanted position , u can check if he already reached it by : if (MyAgent.IsStopped) I think , then u can set the bool to false again to activate ur normal mvmnt system
Thank you for taking your time trying to help me, but unfortunately your solution didn't work and wouldnt work.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Player hanging on objects. 0 Answers
Why my character moves left when i press right key ? Controls are upside down? 2 Answers
Jitter when using transform.position 1 Answer
Distribute terrain in zones 3 Answers