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
  4. Dismiss Notice

Question Stopping Vector3 movement

Discussion in 'Scripting' started by Silver100, Jun 18, 2023.

  1. Silver100

    Silver100

    Joined:
    May 4, 2019
    Posts:
    30
    Hi,

    I'm using this to more an object. When I set to 0 it wont stop and if reversed / same force back it kind or
    works but shakes. Is there a better way to stop vector movement?

    transform.Translate(Vector3.forward * Time.deltaTime * speed);

    transform.Translate(Vector3.back * Time.deltaTime * speed);
     
  2. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,137
    Just don't execute the code responsible for moving if you don't want to move.
    Code (csharp):
    1. if (!Mathf.Approximately(speed, 0f)) transform.Translate(Vector3.forward * Time.deltaTime * speed);
     
    Last edited: Jun 18, 2023
    Silver100 likes this.
  3. Silver100

    Silver100

    Joined:
    May 4, 2019
    Posts:
    30
    Thanks I will try and re-arrange, this almost works but has sleight shake at the end.
    ** Update. This is before realising the errors, IEnmertor in update error & Vector3.zero could have been used instead & not cancelling forces with opposite directions.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Flightmovement : MonoBehaviour// Note this is before noticing the IEnmerator  error and how movement is handled ** cancelling movement should have been Vector3.zero
    5. {
    6.  
    7.     void Update()
    8.     {
    9.         StartCoroutine(Mover(v: 30));
    10.  
    11.         IEnumerator Mover(int v)// Objects orientation is wrong way round so down is forward
    12.         //This section should be re-arranged outside of update
    13.         {
    14.              transform.Translate(Vector3.down * Time.deltaTime * 1000);// By cancelling forces (incorrect, should have Vector3.zero this section should be re-arranged)
    15.             yield return new WaitForSeconds(18.0f);
    16.  
    17.             transform.Translate(Vector3.up * Time.deltaTime * 1000);// Cancels movement (note my error - should be using Vector3.zero & arrange correctly)
    18.             yield return new WaitForSeconds(0f);
    19.  
    20.             transform.Translate(Vector3.back * Time.deltaTime * 100);// Lands
    21.             yield return new WaitForSeconds(6.5f);
    22.             transform.Translate(Vector3.forward * Time.deltaTime * 100);// Freezes with sleight (incorrect sould be Vector3)
    23.  
    24.  
    25.         }
    26.  
    27.     }
    28. }
     
    Last edited: Jun 20, 2023
  4. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,876
    Please use code tags. Anyone with more than a dozen posts on this forum should know this by now.

    You're starting a coroutine on EVERY frame update, which is just not useful. No wonder there's jitter.
     
    Bunny83 and Silver100 like this.
  5. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    549
    Silver, it looks like you're trying to create some sort of state machine using coroutines but you've misunderstood how they work. If you want to create a sequence of movements using a coroutine then you can do it this way:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MoveIt : MonoBehaviour
    6. {
    7.  
    8.     Vector3 direction;
    9.  
    10.     void Start()
    11.     {
    12.         StartCoroutine(Mover());
    13.     }
    14.  
    15.     void Update()
    16.     {
    17.         transform.Translate(direction*Time.deltaTime);
    18.     }
    19.  
    20.     IEnumerator Mover()
    21.     {
    22.         direction=Vector3.up;
    23.         yield return new WaitForSeconds(3);
    24.         direction=Vector3.right;
    25.         yield return new WaitForSeconds(3);
    26.         direction=Vector3.down;
    27.         yield return new WaitForSeconds(3);
    28.         direction=Vector3.zero;
    29.         StopCoroutine(Mover());
    30.     }
    31. }
    32.  

    There are many ways of creating state machines where your program goes through a sequence of actions. Delegates and Actions are another great way of doing this sort of thing and you may find them a little easier to work with.
     
    Silver100 likes this.
  6. Silver100

    Silver100

    Joined:
    May 4, 2019
    Posts:
    30
    Thanks all for the help on this, I cant believe I put IEnumerator in update too. I definitely need to learn more about IEnumerator and arranging accordingly. I will look into delegated and actions method too.
     
    Last edited: Jun 19, 2023