Search Unity

[SOLVED]C#: Turn enemy toward move direction.

Discussion in 'Scripting' started by DarkBladeNemo, Jun 1, 2017.

  1. DarkBladeNemo

    DarkBladeNemo

    Joined:
    Aug 23, 2013
    Posts:
    116
    Hi everyone,

    Okay so what I have is a simple enemy set up to use the Navmesh to navigate and find the player. Currently I'm using transform.LookAt to rotate it toward the player. Now what I want to do is rotate the enemy in the direction it is moving as it looks really weird with the enemy constantly staring at your soul through a brick wall.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine.AI;
    4. using UnityEngine;
    5.  
    6. public class EnemyController : MonoBehaviour {
    7.  
    8.     public PlayerController thePlayer;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.         NavMeshAgent enemy = GetComponent<NavMeshAgent>();
    13.         thePlayer = FindObjectOfType<PlayerController> ();
    14.  
    15.     }
    16.  
    17.     void FixedUpdate(){
    18.        
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update () {
    23.         GetComponent<NavMeshAgent> ().destination = thePlayer.transform.position;
    24.         transform.LookAt (thePlayer.transform.position);
    25.     }
    26. }
    27.  
    This is the code I'm using to handle the movement. Any help would be appreciated.

    Thanks in advance
     
  2. VileGoo

    VileGoo

    Joined:
    Apr 29, 2017
    Posts:
    220
    Im no expert with coding but I think you should try using an 'if' statement to check how close the player is to the enemy. Because right now your code has the enemy looking at the player every frame with no limitations.
     
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Is this what you're looking for? https://docs.unity3d.com/ScriptReference/AI.NavMeshAgent-updateRotation.html
    Just remove the LookAt I'd guess.

    You also have
    Code (csharp):
    1. NavMeshAgent enemy = GetComponent<NavMeshAgent>();
    in Start that is doing nothing. Unless you're using that variable in Start, I'd remove it. If you plan to use it somewhere besides start, then you have to move the 'NavMeshAgent enemy' portion outside of the Start() method, and then change it to:
    Code (csharp):
    1. enemy = GetComponent<NavMeshAgent>();
    In start. For instance, you could then use that variable to set the destination instead of fetching it, again :)
     
  4. DarkBladeNemo

    DarkBladeNemo

    Joined:
    Aug 23, 2013
    Posts:
    116
    Yeah the check in the start was done before I started changing the code a little. Ill have a look at the updateRotation and see if that works.

    Hopefully now the enemy will move through the scene while looking in the direction its going instead of constantly looking at the player.

    Ill let you know if it works. Thank you
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Cool. I've never used the nav mesh agent, but I'll be happy to hear how it works for you. It sounds like that's the right answer :) (I just googled it for your question) lol
     
  6. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,304
    Answer is here, with code
    https://docs.unity3d.com/Manual/nav-CouplingAnimationAndNavigation.html
     
  7. DarkBladeNemo

    DarkBladeNemo

    Joined:
    Aug 23, 2013
    Posts:
    116
    Methos5k I checked out the updateRotation but that made my enemy fly all over the place.
    NA-RA-KU currently the enemy is only a capsule so there isnt any animation involved.

    What I am thinking at the moment is trying to get the rotation by checking the axis the navmesh is currently going in. (if that makes any sense). So I do have a rigidbody attached and if I use LookAt and target the velocity it kinda works but it zips all over the place.

    I dunno :/ Trying to think of something logical and normally just talking complete bs on the forums helps me figure it out.
     
    KqGMRPFkAeESzF8 likes this.
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Hm, sorry to hear that. I hope you figure it out.
     
  9. DarkBladeNemo

    DarkBladeNemo

    Joined:
    Aug 23, 2013
    Posts:
    116
    Okay so i have figured it out. The reason it wasn't turning properly was mainly due to the lookAt and destination.

    So basically this:

    Code (CSharp):
    1. void Update () {
    2.         GetComponent<NavMeshAgent> ().destination = thePlayer.transform.position;
    3.         transform.LookAt(thePlayer.transform.position);
    4.     }
    What I did to fix it was I changed destination the setDestination which then calculates the path and rotates the character Z axis towards the current direction of movement(Atleast I think thats how it works). This results in smooth turning of corners and at the end if the day it will still look at the player when there is nothing else in the way.

    The working piece of code:

    Code (CSharp):
    1. void Update () {
    2.         GetComponent<NavMeshAgent> ().SetDestination (thePlayer.transform.position);
    3.     }
    Thanks for the help guys.

    EDIT: Having a collider on the object that uses the agent will result in it spazzing around. What I did to fix this was to make the collider a trigger. Though this will result in some enemies probably overlapping(I dunno) at least it is working.
     
    Last edited: Jun 3, 2017
  10. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Hey cool, glad ya got that worked out :)