Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved Best way to move this code to the Update function?

Discussion in 'Getting Started' started by Gummi_Ghost, Nov 23, 2021.

  1. Gummi_Ghost


    Sep 21, 2020
    Hello,I am using
    in this code, and it does work, but the movement is instant instead of updating each frame. I researched that the way to fix this was to put it in the update function. However. I'm not sure how to do this if I need to have an if statement in another function which determines which movement I should use?

    Another question, which forum is the best for others to critique your code? I think it's the Documentation forum but I'm not sure.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class IEOMovement : MonoBehaviour
    5. {
    6.     public GameObject IEO;
    7.     [Range(0f, 1f)]
    8.     public float spawnChance = 0.5f;
    9.     [Range(0f, 15f)]
    10.     public float speed = 10f;
    11.     public GameObject LeftScreenPoint;
    12.     public GameObject RightScreenPoint;
    13.     private float minSpawnTime = 5f;
    14.     private float maxSpawnTime = 15f;
    16.     void Update ()
    17.     {
    18.       //How do I move leftToRightMovement(); to here, if I need to have an if statement in another function which determines which movement I should use?
    19.     }
    20.     void Start()
    21.     {
    22.         StartCoroutine(Spawn());
    23.     }
    24.     private IEnumerator Spawn()
    25.     {
    27.         if (Random.value < spawnChance)
    28.         {
    30.             if ((gameObject).CompareTag("leftIEO"))
    31.             {
    32.                 leftToRightMovement();
    33.             }
    34.             else
    35.             {
    36.                 rightToLeftMovement();
    37.             }
    38.             // Setup for next spawn
    39.             yield return new WaitForSeconds(Random.Range(minSpawnTime, maxSpawnTime));
    40.             StartCoroutine(Spawn());
    41.         }
    42.     }
    43.     void leftToRightMovement()
    44.     {
    45.         //place the gameObject prefab at the LeftScreenPoint's position
    46.         //Instantiate(IEO, LeftScreenPoint.transform.position, LeftScreenPoint.transform.rotation);
    47.         IEO.transform.position = LeftScreenPoint.transform.position;
    48.         //moves object to the RightScreenPoint
    49.         IEO.transform.position = Vector3.MoveTowards(IEO.transform.position, RightScreenPoint.transform.position, (speed * Time.deltaTime));
    50.     }
    51. }
  2. Joe-Censored


    Mar 26, 2013
    Right now you're only calling leftToRightMovement() from within the Spawn() coroutine. This would mean movement only occurs on the specific frame Spawn() runs. I'm not sure exactly what you are going for. If it just to get leftToRightMovement() called every frame, just remove it from Spawn() and call it in Update(). If your issue is that all needed movement is completing in a single frame, then you likely have your "speed" variable set way too high.
  3. Gummi_Ghost


    Sep 21, 2020
    Hello and thank you! Your answer worked! This was actually a shorter version of the code I had, but. I was able to make it work, and found that there are a lot of unessacery things in my code I will remove. Thanks again!
    Joe-Censored likes this.