1
\$\begingroup\$

my script is for character moving to the certain point on the terrain. To get this point I have to use mouse. When I click on the terrain, my character goes there. Now, If he reach his point and something like enemy come to push him, he moves away from his point !!? What I want is to make him stop there and no power in the world could move him.

private NavMeshAgent mAgent;
public Vector3 point_;
public float mark;
public bool actives;


void Start()    {
    mAgent = GetComponent<NavMeshAgent>();

}

// Update is called once per frame
void Update()    {
    Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit mousePoint;
    if (Input.GetMouseButtonDown(0))
    {
        if (Physics.Raycast(mouseRay, out mousePoint, 100))
        {
            point_ = mousePoint.point;
            mAgent.SetDestination(mousePoint.point);
            mark = Vector3.Distance(point_, gameObject.transform.position);
        }

    }

}
\$\endgroup\$
2
\$\begingroup\$

You can use a constrain on the rigidbody https://docs.unity3d.com/ScriptReference/Rigidbody-constraints.html

Or if the external force is a physic-related one make it cinematic. https://docs.unity3d.com/ScriptReference/Rigidbody-isKinematic.html

The second one is faster to implement (a click in your rigidbody) so you can test it faster. You can also change it so its kinematic when stoppped and not kinematic when moving via script.

\$\endgroup\$
2
\$\begingroup\$

Could you just set isKinematic on the RigidBody to false once he reaches the destination? This would prevent the rigidbody from being effected by physics, which is kind of like preventing any force in the world from moving it :).

\$\endgroup\$
  • \$\begingroup\$ Zachary, I wish you were right but still he got pushing from enemies !! Image kinematic is true or false is the same. Is it because enemy have also navmeshAgent ?. Adria , Both methods doesn't work and I don't know why !! \$\endgroup\$ – Omer Jun 9 '18 at 8:11
  • \$\begingroup\$ Well if it really is the unity physics making it move then disabling the rigidbody should do the trick. If it still moves even after unity physics are turned off then I would say some other component is making it do that. Without seeing any code or behaviour this is a little hard to identify \$\endgroup\$ – Zachary Sheppard Jun 10 '18 at 4:04
  • \$\begingroup\$ What happens if you turn off (set the enabled variable to false) the collider on that object either while it is moving or when it reaches your destination? \$\endgroup\$ – Zachary Sheppard Jun 13 '18 at 15:37
  • \$\begingroup\$ I don't thing it's from the things you mentioned. I solved this issue by increasing "Stopping Distance" from 0 to 1 for enemy navmeshAgent. Thank you Zachary. \$\endgroup\$ – Omer Jun 13 '18 at 22:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.