Search Unity

Trouble getting colliders to work with click-to-move

Discussion in 'Scripting' started by QuinnWinters, Mar 30, 2014.

  1. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    494
    I've got the standard ClickToMove script attached to a player object. It works great except it will not recognize colliders so the player goes right through them. I've been all over google and various forums for the past 6 hours trying everything I can find to fix this but I haven't yet run across a working solution.

    Triggers are no good. If the player enters a trigger I can make it set the players transform destination to its current position, but once it's already in the trigger and the trigger has fired I can't do that again so if the player clicks behind the collider a second time the player object will again ignore the collider - unless I use OnTriggerStay, in which case if I set the transform like that the player is permanently frozen in place.

    Rigidbodies are no good. 1 - If using rigidbodies they react to physics, so if the player clicked behind a collider the player object will repeatedly bounce off the collider. 2 - Constraining movement on the rigidbodies nullifies the colliders so click-to-move will once again pass through them. 3 - If using a character controller with a rigidbody the colliders actually work but the player ricochets around between colliders for no apparent reason.

    The closest to a solution I've found is this: http://answers.unity3d.com/questions/257124/click-to-move-questions.html

    But that doesn't work. When you click a place to move to with that script the player will move about 1 unit toward it (if I'm lucky - sometimes it chooses its own direction to go) and then stop. When running Debug.Log on the targetPosition variable in that it never changes and I can't figure out why.

    Does anyone know of a solution for having a character react to colliders when using a click-to-move script? Or perhaps another script that will accomplish the same thing in a different way?

    Any help is appreciated!
     
    Last edited: Mar 30, 2014
  2. Jakob_Unity

    Jakob_Unity

    Joined:
    Dec 25, 2011
    Posts:
    269
    I'm not entirely sure what you're trying to achieve - but maybe NavMesh is what you're looking for.

    See also: https://docs.unity3d.com/Documentation/Manual/NavmeshandPathfinding.html

    Knowing physics already - you can think of NavMeshAgent as the equivalent of the CharacterController. And the NavMeshObstacle as the equivalent of the collider.

    so you could try the following:

    add a NavMeshAgent component to GameObject you want to move - e.g. character.
    add NavMeshObstacle components to the GameObjects you want your moving (character) object to avoid.
    Mark the walkable geometry (ground) as static (upper left corner of inspector) and press "Bake" in the "Navigation" window.

    the click-to-move script for you navmesh agent could be something along the lines of :

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. [RequireComponent (typeof (NavMeshAgent))]
    6. public class ClickToMove : MonoBehaviour {
    7.     private NavMeshAgent m_Agent;
    8.  
    9.     void Start () {
    10.         m_Agent = GetComponent<NavMeshAgent> ();   
    11.     }
    12.    
    13.     void Update () {
    14.         if(Input.GetMouseButtonDown(0)) {
    15.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    16.             RaycastHit hitInfo = new RaycastHit();
    17.             if (Physics.Raycast(ray.origin, ray.direction, out hitInfo)) {
    18.                 m_Agent.destination = hitInfo.point;
    19.             }
    20.         }
    21.     }
    22. }
    23.  
    Your navmeshagent component will be constrained to the generated navmesh (generated when pressing "bake").

    Cheers..
    /Jakob
     
  3. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    494
    Thanks for the reply, I'll give this a try and see how it works. It sounds like the solution I'm looking for.