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

Moving object in different point for times

Discussion in 'Scripting' started by simone_fratoni, Apr 19, 2020.

  1. simone_fratoni

    simone_fratoni

    Joined:
    Jan 17, 2020
    Posts:
    13
    Hello to everyone!
    I want to set my object in different position each 0.1 second.
    Example:
    time 0 position (0,0,0)
    time 0.1 position (5,0,0)
    time 0.2 position (-10,0,0)
    time 0.3 position (10,0,0)

    Can you help me with a script?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    This forum is not about us writing you scripts. It is about you learning scripting, trying some stuff, having questions about it, posting your work here (properly formatted) along with a coherent question about it, and we help you understand and improve it.

    I recommend starting with the basic Unity tutorials linked at the top of this site. It's really pretty easy.
     
    simone_fratoni likes this.
  3. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    From the examples you showed, you want to move to a predefined location at the fixed interval of 0.1s. So define a list of Vector3 which you fill with your targets through the inspector. In your script, save the startTime (which you see as '0'), then each Update() calculate timeDiff = Time.time - startTime. Now timeDiff contains the time that passed since our 0-time in seconds. Multiply it by 10, cast it to an int and you can use it as an index for your target list.

    The above solution is not very elegant, but you provided very little information on what you need or what you need it for. If, for example, the time intervals are not fixed, or the positions are to be selected randomly (potentially from a pool of possible targets), then the solution would look differently and a lot more elegant. With what you provided tho, the above would be one way for how to approach the problem.
     
    simone_fratoni likes this.
  4. Leonetienne500

    Leonetienne500

    Joined:
    Dec 5, 2016
    Posts:
    130
    We are not your private scriptkiddies.
    Take my two words of advice:
    Code (CSharp):
    1. //advice 1
    2. SetInterval("RandomPos", 0.1f);
    3.  
    4. void RandomPos()
    5. {
    6. }
    7.  
    8. //advice 2:
    9. float valBetweenNeg10And10 = Random.Range(-10, 10);
     
    simone_fratoni likes this.
  5. simone_fratoni

    simone_fratoni

    Joined:
    Jan 17, 2020
    Posts:
    13
    Thanks a lot! i'm engineering student and i want to simulate earthquake in my structure. i want to assign movement of my structure that is a simple gameobject in my scene. I used animation with step by step assign new position every 0.1s but i want to create a script to add directly at my object.

    I have result of my structure analysis Time History that give me a table with point contain time and position.

    I have all position of my structure every 0.1s for 40 seconds like this: Time 0.1 position 0,0,0 time 0.2 position 1,0,0......
    i don't need elegance because with excel extract all data in list and can i put this in unity directly with specific word.
    Ex time 0.1 position 0,0,0 ---transform with exel in--> transform.traslate=(0*time.deltatime/0.1,0,0) or as i want that copy directly in unity script

    I am a basic programmer and this is my firt project, sorry for my bad english and my ability.

    i think to use this
    void Update()
    {
    ///I want to start and delta time is 0.1 second between to point

    transform.Translate(0, 0,0);
    transform.Translate(5, 0,0);
    transform.Translate(-10, 0, 0);
    transform.Translate(-10, 0, 0);
    transform.Translate(10, 0, 0);

    ///I want to stop but traslate to infinity
    }
    }
     
  6. simone_fratoni

    simone_fratoni

    Joined:
    Jan 17, 2020
    Posts:
    13
    Thanks for reply!
    I try to use animation and it's works but i want to change with script

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class movement : MonoBehaviour
    {
    public GameObject cube;
    // Start is called before the first frame update
    void Start()
    {

    }
    // Update is called once per frame
    void Update()
    {
    transform.Translate(0, 0,0);
    transform.Translate(5, 0,0);
    transform.Translate(-10, 0, 0);
    transform.Translate(-10, 0, 0);
    transform.Translate(10, 0, 0);
    }
    }
     
  7. simone_fratoni

    simone_fratoni

    Joined:
    Jan 17, 2020
    Posts:
    13
    sorry but is my first time in this forum and in unity. I want to learn but i search for a lot of day my solution without solve problem.

    I try with a simple code like this:
    transform.Translate(0, 0,0);
    transform.Translate(5, 0,0);
    transform.Translate(-10, 0, 0);
    transform.Translate(-10, 0, 0);
    transform.Translate(10, 0, 0);
     
  8. Leonetienne500

    Leonetienne500

    Joined:
    Dec 5, 2016
    Posts:
    130
    You have to mark the code down.
    Like this:
    Code (CSharp):
    1. transform.Translate(0, 0,0);
    2. transform.Translate(5, 0,0);
    3. transform.Translate(-10, 0, 0);
    4. transform.Translate(-10, 0, 0);
    5. transform.Translate(10, 0, 0);
    Otherwise no one is going to read it.

    the Translate function moves the object. It does not set its absolute position. Like, if it is already at position 90, Translate(10) would move it to position 100.

    I don't use the Translate function though. I usually just do
    Code (CSharp):
    1. transform.position += new Vector3(10, 0, 0);
    If you want to SET an objects position, overwriting its current position, use this:
    Code (CSharp):
    1. transform.position = new Vector3(10, 0, 0);
     
  9. simone_fratoni

    simone_fratoni

    Joined:
    Jan 17, 2020
    Posts:
    13
    Thanks, i solve a part of problem now
    void Update()
    {
    transform.position = new Vector3(5, 0, 0);
    transform.position = new Vector3(-10, 0, 0);
    transform.position = new Vector3(10,0, 0);
    transform.position = new Vector3(-10, 0, 0);
    }
    }

    And then how set 0,1s interval each raw?
    like this ex for the last raw transform.position = new Vector3(10*Time.deltaTime/10, 0, 0);
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Okay, just STOP. You are just pawing randomly at code now.

    Stop ALL work on your project and go to the Unity Learn section and work through at least TWO tutorials, preferably ones with some light scripting and coding. Otherwise you're just wasting your own time here theorizing about how scripting works.
     
  11. simone_fratoni

    simone_fratoni

    Joined:
    Jan 17, 2020
    Posts:
    13
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Sequenza : MonoBehaviour
    6. {
    7.  
    8. void Start()
    9.     {
    10.         Vector3[] spostamento = {new Vector3(0, 0, 0), new Vector3(10, 0, 0), new Vector3(-10, 0, 0), new Vector3(5, 0, 0), new Vector3(-5, 0, 0)};
    11.         StartCoroutine (Move(spostamento,1));
    12.     }
    13.     IEnumerator Move(Vector3 destination, float speed)
    14.     {
    15.         while (transform.position !=destination)
    16.         {
    17.             transform.position = Vector3.MoveTowards(transform.position, destination, speed * Time.deltaTime);
    18.             yield return null;
    19.         }
    20.     }
    21. }
    22.  
    i try and try again and this is my result. You want help me?
    Now how can change
    Vector3[] spostamento = {new Vector3(0, 0, 0), new Vector3(10, 0, 0), new Vector3(-10, 0, 0), new Vector3(5, 0, 0), new Vector3(-5, 0, 0)};
    in a group of vector that use in coroutine?
     
    Last edited: Apr 20, 2020