Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

A follow target that can be specified by player?

Discussion in 'Scripting' started by Othynrix, Oct 2, 2016.

  1. Othynrix

    Othynrix

    Joined:
    Sep 30, 2016
    Posts:
    4
    Hello!

    I just want to know, what should you do to make an automator where a unit will automatically head towards a target, but you can choose where these efforts are focused? (i.e. a unit will automatically head towards enemy base, but you can change the location in which they will follow to.)

    So basically you hit a key and the follow point will change to the point where the mouse is pointing. The thing is it needs to be on a plane basis (i think X and Z) so that they don't fly toward a target in the air.

    Many thanks :D,
    Othynrix
     
  2. Kalladystine

    Kalladystine

    Joined:
    Jan 12, 2015
    Posts:
    227
    Actual solution depends on what type of pathfinding you're using.
    F.e. if you're using the built in one (NavMeshAgent's) - check this manual as a starter.

    For the "default" behaviour, as an example, you could just setup the destination in the Start/Awake of the prefab to a point/object you know will be valid.

    Hard to say exactly, as your question doesn't have details.
     
  3. Othynrix

    Othynrix

    Joined:
    Sep 30, 2016
    Posts:
    4
    Yes. Thanks, NavMeshAgent works to move an empty object in which the object would follow.

    But that is only the start - I want the prefab to turn a certain direction (facing the direction it's going) as it moves towards the position.

    Also, what details do you require?

    Oth
     
  4. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    This can be accomplished by adding a Facing Script to the unit itself. This way the Navigation System doesn't care, its just trying to move the GameObject to the goal Position.

    Your FaceScript should basically:
    • Get a direction Vector of the Goal by subtracting goal.Position - transform.position
    • Using trig you can get the angle this Vector makes with the world.forward
    • then change your objects rotation to match the rotation you figured out
    The above 3 steps should happen each update.. that way your object will always be facing his goal.
    Note: depending on your map.. if your goal is on the other side of a western barrier and NavMesh is walking you south around the barrier the above algorithm will face you west and you will strafe down the slide of the barrier always pointing to the ultimate goal.

    I'm not very familiar with the Navmesh but you might be able to get the current point its using to move your guy temporarily to make it to the final goal. Then you would use this temporary point as the Goal position in the algorithm above.
     
  5. yepfuk

    yepfuk

    Joined:
    Sep 23, 2015
    Posts:
    67
    I don't understand your question exactly.

    But if you want to your prefab go towards that mouse click position.

    In Update ;
    - When mouse is clicked certain position, get this position and convert it world position.

    Like this;
    Code (CSharp):
    1. Ray ray = Camera.main.ScreenPointToRay(cMousePos);
    2.         RaycastHit hit;
    3.  
    4.         if (Physics.Raycast(ray, out hit))
    5.         {
    6.             positionToMoveByMouse = hit.point;
    7.         }
    In Fixed Update;

    If positionToMoveByMouse is not equals to Vector3.zero then your prefab moves towards that mouse position. When the prefab gets this position then change the position to the enemy base again.
    If positionToMoveByMouse is equals to Vector3.zero then your prefab moves towards that enemy base.

    For your heading rotation, "takatok" 's answer is very good.