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

Having issues with changes in speed of the objects

Discussion in 'Scripting' started by GoneAway, Jul 4, 2015.

  1. GoneAway

    GoneAway

    Joined:
    Apr 17, 2015
    Posts:
    4
    I am on my first steps of learning how to code and also learning about the unity engine. So I am sure that I am doing something wrong here and probably it is in my code . These past three days I am surfing though the internet trying to figure out what I am doing wrong but I still haven't found out what it is.

    I am using a number of cylinders with the following script attached to it
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MoveCapsule : MonoBehaviour
    5. {
    6.  
    7.     public float moveSpeed = 4f;
    8.     public bool canBite = false;
    9.     public GameObject zombieClone;
    10.     public bool canBiteBite = false;
    11.     //public Transform sPT;
    12.  
    13.     void Start ()
    14.     {
    15.         InvokeRepeating("Wandering", 1, 3f);
    16.     }
    17.    
    18.     void Update ()
    19.     {
    20.         GameObject[] ZombieClones = (GameObject[])
    21.         GameObject.FindObjectsOfType(typeof(GameObject));
    22.         foreach (GameObject go in ZombieClones)
    23.         {
    24.             ZombieCloneS zC= go.GetComponent<ZombieCloneS>();
    25.            
    26.             if(zC == null || zC == this)
    27.             {
    28.                 continue;
    29.             }
    30.         }
    31.  
    32.         if ((canBite && Input.GetKeyDown (KeyCode.E)) || canBiteBite)
    33.         {
    34.             GameObject zombieCloneC = (GameObject) Instantiate(zombieClone, transform.position, transform.rotation);
    35.             Destroy(gameObject);
    36.         }
    37.  
    38.         if(!transform.Find("ZombieDetect").GetComponent<ZombieDetect>().zombieDetected)
    39.         {
    40.             transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime);
    41.         }
    42.         else //if (transform.Find("ZombieDetect").GetComponent<ZombieDetect>().zombieDetected)
    43.         {
    44.             CancelInvoke("Wandering");
    45.             RunToSafety ();
    46.         }
    47.     }
    48.  
    49.     public void Wandering()
    50.     {
    51.         transform.Rotate(0,180,0);
    52.     }
    53.  
    54.     void OnCollisionEnter(Collision other)
    55.     {
    56.         if (other.gameObject.tag == "Player")
    57.         {
    58.             canBite = true;
    59.             //Debug.Log("Player Entered");
    60.         }
    61.         else if (other.gameObject.tag == "Zombie")
    62.         {
    63.             canBiteBite = true;
    64.         }
    65.     }
    66.  
    67.     void OnCollisionExit(Collision other)
    68.     {
    69.         canBite = false;
    70.         canBiteBite = false;
    71.     }
    72.  
    73.     void RunToSafety()
    74.     {
    75.         GameObject[] safetyPoints = GameObject.FindGameObjectsWithTag ("Safety");
    76.         foreach (GameObject sp in safetyPoints)
    77.         {
    78.             transform.position = Vector3.MoveTowards (transform.position, sp.transform.position, moveSpeed * Time.deltaTime);
    79.         }
    80.     }
    81. }
    The problem I am having is that when RunToSafety function is executed and the object MoveTowards the target(sp.tranfrom.position) with the same moveSpeed, it actually moves faster than when transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime) is executing.
    Apparently I am missing something technical here but I can't seem to be able to find out what.
     
  2. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    968
    If you look at the documentation for MoveTowards, you will see that the last paramenter is not a speed, but instead the maximum distance that it will move per call to this method (which translates to per frame because you're calling it in update).
     
  3. GoneAway

    GoneAway

    Joined:
    Apr 17, 2015
    Posts:
    4
    I guess overthinking things at some point you are getting lost the more you are trying to search and you are missing the obvious things. Thanks for pointing that to me. I can't believe I missed it.