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

Question Code assistance/pointers!

Discussion in 'Scripting' started by unity-code7, Apr 2, 2022.

  1. unity-code7

    unity-code7

    Joined:
    May 7, 2017
    Posts:
    31
    I'm moving a bus object with the following code and when it gets to the end of the road object I want it to stop, then reverse to it's starting point. I keep coming up with errors in the code, however. The only way I could think thus far of doing this is using a conditional that tracks the final float z values, then the code to reverse the bus object to it's starting point (with another conditional tracking it's starting point z values and making it stop. The errors come up under the second position property as 'Transform.position cannot be used like a method'. I'm not too familiar with C# so I also don't know if I'm using the correct equals (==. ===) operator. Any help? Thanks! =).

    Code (CSharp):
    1.  
    2.  
    3. //move bus on z axis
    4. transform.Translate(Vector3.forward * busSpeed * Time.deltaTime);
    5.  
    6.     //grab z end value to make bus stop then reverse to starting z value
    7.     if (transform.position == transform.position(0,0,-160.1)) {
    8.                     transform.Translate(Vector3.back * busSpeed * Time.deltaTime);                          
    9.         }
    10.     if (transform.position == transform.position (0,0,-145.82) {
    11.                     transform.Translate(Vector3.0,0,0);
    12. }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    transform.position is a vector3 type. You're calling it like a method.

    When you're doing transform.position(xxx) it thinks you're trying to call a method called position on the transform class and pass it three values.

    Since position is a vector3, you need to assign a vector3 to it. Or, compare to it.

    Code (CSharp):
    1. if(transform.position == new Vector3(0,0,-160.1))
    For example.
    Just to note, = is assignment, == is comparison in c#.
    I would suggest doing some tutorials over c# as calling a method is pretty important to understand in c#.
     
  3. Omniglitch

    Omniglitch

    Joined:
    May 29, 2017
    Posts:
    37
    From the code you provided, it looks like line 4 will always move the bus forward, regardless of the conditional statements below.

    How I would recommend doing it is to put the direction vector into a variable, since it's a state that will change periodically. Also it's best to do all the conditionals and math first, then apply that to the transform.

    Something like this...
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class YourClassNameHere : MonoBehavior
    6. {
    7.     private Vector3 movementDirection = Vector3.forward;
    8.     private float maximumPosition = 160.1f;
    9.     private float minimumPosition = 145.82f;
    10.     private float busSpeed = 1f;
    11.  
    12.     void Update
    13.     {
    14.         if ( transform.position.z >= maximumPosition )
    15.             movementDirection = Vector3.back;
    16.  
    17.         if ( transform.position.z <= minimumPosition )
    18.             movementDirection = Vector3.forward;
    19.  
    20.         Vector3 translationVector = new Vector3( movementDirection * busSpeed * Time.deltaTime );
    21.         transform.Translate( translationVector );
    22.     }
    23. }
    Also, you should avoid using == to check the value of a float, because of the inherent inaccuracies of floating point numbers. That's why I used >= and <= to check when it went out of bounds.
     
    Kurt-Dekker likes this.
  4. unity-code7

    unity-code7

    Joined:
    May 7, 2017
    Posts:
    31
    Doh! Facepalm! You're right! I should've picked up on this from the error message. I'm used to seeing parenthesis in methods/functions that I made Vector3 to be the same! Thanks so much for the C# tip too! =).
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Humorously when the equality check fails, your bus object would just keeps going.

    Cue the "&#@$%, I'm a bus object!!!" meme.