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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Character not moving

Discussion in 'Scripting' started by Effsty, Jun 24, 2016.

  1. Effsty

    Effsty

    Joined:
    Jun 24, 2016
    Posts:
    29
    I have a cube on a plane. In Hierarchy I've used 'create empty' to create a couple of points on the plane. I've attached the following script to the cube (note: it's the one from documentation). I've dragged and dropped the 'points' from the hierarchy to the script on the right, however the cube doesn't move.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5. public class MoveEnemy : MonoBehaviour {
    6.  
    7.     public Transform[] points;
    8.     private int destPoint = 0;
    9.     private NavMeshAgent agent;
    10.  
    11.  
    12.     void Start () {
    13.         agent = GetComponent<NavMeshAgent>();
    14.  
    15.         // Disabling auto-braking allows for continuous movement
    16.         // between points (ie, the agent doesn't slow down as it
    17.         // approaches a destination point).
    18.         agent.autoBraking = false;
    19.  
    20.         GotoNextPoint();
    21.     }
    22.  
    23.  
    24.     void GotoNextPoint() {
    25.         // Returns if no points have been set up
    26.         if (points.Length == 0)
    27.             return;
    28.  
    29.         // Set the agent to go to the currently selected destination.
    30.         agent.destination = points[destPoint].position;
    31.  
    32.         // Choose the next point in the array as the destination,
    33.         // cycling to the start if necessary.
    34.         destPoint = (destPoint + 1) % points.Length;
    35.     }
    36.  
    37.  
    38.     void Update () {
    39.         // Choose the next destination point when the agent gets
    40.         // close to the current one.
    41.         if (agent.remainingDistance < 0.5f)
    42.             GotoNextPoint();
    43.     }
    44. }
     
  2. jaasso

    jaasso

    Joined:
    Jun 19, 2013
    Posts:
    64
    seems fine to me, do you have a navmesh for your scene?
     
    Effsty likes this.
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,201
    Effsty likes this.
  4. Effsty

    Effsty

    Joined:
    Jun 24, 2016
    Posts:
    29
    I have a navmesh and the plane is the one selected as "Static", however the cube is not moving.
     

    Attached Files:

    Last edited: Jun 24, 2016
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,201
    Any error messages in the console?

    Also check that the speed of the navmesh agent is greater than 0.
     
    Effsty likes this.
  6. Effsty

    Effsty

    Joined:
    Jun 24, 2016
    Posts:
    29
    Seems like it is 3.5. I put the cube in the air. When I preview it appears directly on the ground (it has a rigidbody attached) (shouldn't it be falling?) and moves very little in some random pattern. I've attached a video for this.
    Any help it's appreciated :)

     
  7. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,201
    If you have a navmeshagent and a ridigbody on the same object, the rigidbody should be set as kinematic - which means "don't move this with physics". Try toggling "is kinematic" on in your rigidbody.

    The navmeshagent moves the transform directly around on the navmesh. If you have a rigidbody with gravity and such, the rigidbody and navmeshagent will fight each other, and cause all sorts of strange issues.

    The navmeshagent is always glued to the ground, this is why your cube snaps down. The "base offset" parameter changes how far from the ground the center of your object is - move it after pressing play and see what happens.
     
  8. Effsty

    Effsty

    Joined:
    Jun 24, 2016
    Posts:
    29
    I've checked the "is kinematic". I've also removed the rigidbody component.
    The cube still doesn't move :(
     
  9. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,201
    .... have you added the points in the inspector?
     
  10. Effsty

    Effsty

    Joined:
    Jun 24, 2016
    Posts:
    29
    Yes (you mean to the moving script?)
     

    Attached Files:

    • dskt.png
      dskt.png
      File size:
      321.1 KB
      Views:
      883
  11. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,201
    Okay, I'm actually at a loss here.

    Could you upload your project somewhere? I'll have a look.
     
  12. Effsty

    Effsty

    Joined:
    Jun 24, 2016
    Posts:
    29
    This was my fault (a pretty stupid mistake)

    The points I set to follow were totally off the plane.
     

    Attached Files: