Search Unity

Identical Scripts, Different Behavior?

Discussion in 'Scripting' started by williamsmd90, Jan 7, 2019.

  1. williamsmd90

    williamsmd90

    Joined:
    Sep 3, 2018
    Posts:
    22
    I'm new to Unity and scripting, but I've just come across something strange. I was following a tutorial where we made the enemy move around set points in the play area. It works perfectly in the script that I did while following the tutorial a few days ago. Now that I try to use it for another project, it's not showing the same behaviors. Here's the original code:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class EnemyPatrol : MonoBehaviour
    4. {
    5.     public Transform[] patrolPoints;
    6.  
    7.     public float moveSpeed;
    8.  
    9.     private int currentPoint;
    10.  
    11.     private void Start()
    12.     {
    13.         transform.position = patrolPoints[0].position;
    14.  
    15.         currentPoint = 0;
    16.     }
    17.  
    18.     private void FixedUpdate()
    19.     {
    20.         if(transform.position == patrolPoints[currentPoint].position)
    21.         {
    22.             currentPoint++;
    23.         }
    24.  
    25.         if(currentPoint >= patrolPoints.Length)
    26.         {
    27.             currentPoint = 0;
    28.         }
    29.  
    30.         transform.position = Vector3.MoveTowards(transform.position, patrolPoints[currentPoint].position, moveSpeed * Time.deltaTime);
    31.     }
    32. }
    If you can't tell (I'm not sure if it's written well or poorly), the goal was to make the enemy move around certain points but to be able to add more points if necessary instead of having a set number of points. Again, it worked perfectly in the script that I used with the tutorial. Now that I've created a new Unity project with the same parameters set in Unity as the previous project, it doesn't work. The enemy character starts at patrolPoints[0].position, moves to patrolPoints[1].position, and just stays there. I literally copied and pasted after some frustration just to get it to work the way I wanted it to, but the same thing happened.

    Then, I decided to make it as obvious as possible and re-wrote it to just move back and forth between the 2 points that I set in Unity:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class ColliderBehavior : MonoBehaviour
    4. {
    5.     public Transform[] patrolPoints;
    6.  
    7.     public float moveSpeed;
    8.  
    9.     private void Start()
    10.     {
    11.         transform.position = patrolPoints[0].position;
    12.         currentPoint = 0;
    13.     }
    14.  
    15.     private void FixedUpdate()
    16.     {
    17.         if(transform.position == patrolPoints[0].position)
    18.         {
    19.             transform.position = Vector3.MoveTowards(transform.position, patrolPoints[1].position, moveSpeed * Time.deltaTime);
    20.         }
    21.         if(transform.position == patrolPoints[1].position)
    22.         {
    23.             transform.position = Vector3.MoveTowards(transform.position, patrolPoints[0].position, moveSpeed * Time.deltaTime);
    24.         }
    25.     }
    26. }
    With this, it just stays in the position where it starts. What. Is. Happening?
     
  2. RPGcakes

    RPGcakes

    Joined:
    Nov 6, 2018
    Posts:
    22
    Try putting it in Update() instead of FixedUpdate()
     
  3. If you don't use physics, you should use the Update method instead of the FixedUpdate. If you would move collider and use force, then you use FixedUpdate.
    You use the == operator. https://docs.unity3d.com/ScriptReference/Vector3-operator_eq.html Are you sure that your position gets closer than 1e-5? Check that, maybe you need to change the MoveTowards.

    The second code is failing because you left the currentPoints = 0; in the 12th line but you didn't add the variable declaration.
    You always should check the console for errors and warnings, it usually tells you what the problem is.
     
    RPGcakes likes this.
  4. williamsmd90

    williamsmd90

    Joined:
    Sep 3, 2018
    Posts:
    22
    That makes it jump up and down for some reason. The 'y' is going between 0.87 and 0.89. Also, the player object is bouncing off the enemy object even though there's no physic material on either and there's no scripting for collisions yet.
     
  5. williamsmd90

    williamsmd90

    Joined:
    Sep 3, 2018
    Posts:
    22
    I've attached a Rigidbody to the game object because I wanted it to act as a collider after I got it to move back and forth. It's the only reason I added a Rigidbody component and went with FixedUpdate.

    I'm not sure what that means. A large reason that I'm baffled is that it does still work in the other project that I did while following the tutorial.

    I actually meant to take that out of the post on the thread because it was never used. I copied and pasted again and deleted that line just to check. I have the same problem.

    The console isn't giving me any errors. Perhaps I should just restart Unity and see if that fixes things.
     
  6. Can you make a screenshot of your unity? Including the console?

    It rarely helps if you are doing something wrong it won't fix it magically. Although I don't see any harm in it if you save your work.
     
  7. williamsmd90

    williamsmd90

    Joined:
    Sep 3, 2018
    Posts:
    22
    No errors or warnings showed up. I did notice that the Enemy Collider object (the one I'm trying to get to move) went down on the y-axis from 1 to 0.975 even though it should still be at 1 because 0.975 is partially through the ground plane. And the z-axis was cycling through random values between 0 and -10 (even though it wasn't moving at all) before finally staying on 0.
     

    Attached Files: