• Advertisement
Sign in to follow this  

Making an object move back and forth.

This topic is 647 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

 This is what I have so far. The object moves in 1 direction and doesn't stop. I want the object to move backwards to its original position after it reaches a destination. How do I do this?
 
   public float movementSpeed = 10;
 
    void Update ()
    {
        transform.Translate(Vector3.right * movementSpeed * Time.deltaTime);
 
    }
 
Another question I have is, how do I make the object lets say move in a square?
 
I'm using google and I cant find answers. I'll continue to look. I know what I'm asking for is basic programming knowledge. Any help will be appreciated. Thank you :)

 

Share this post


Link to post
Share on other sites
Advertisement

Is this how you get a high reputation? Just going around not answering questions directly?

 

Yes, I understand the code that is written now. I can make the object go any direction. But I want the object to move forward then backwards then forward then backwards, repeatedly non stop. I want to know how to make the object move from one spot to the next to the next. That's all.

Share this post


Link to post
Share on other sites
Your syntax looks like Unity C#, so I'm going to respond using that.

It seems the simplest approach would be to have a list of waypoints and use the game's time to determine which two waypoints you need to be moving between. Then use https://en.wikipedia.org/wiki/Linear_interpolation to blend between them.

So:
 
List<Vector3> waypoints; // initialize these somewhere

void Update()
{
    // Determine where you are in the sequence based on time. floor the value to the nearest index by casting to int.
    const float kSecondsBetweenWaypoints = 5.0f;
    int sequence = (int)(Time.deltaTime / kSecondsBetweenWaypoints);
    float remainder = Time.deltaTime % kSecondsBetweenWaypoints;

    int next = sequence+1;
    
    // make sure the indices always lie between 0 and waypoints.Count-1
    sequence %= waypoints.Count;
    next %= waypoints.Count;

    Vector3 a = waypoints[sequence];
    Vector3 b = waypoints[next];

    // Linear interpolation
    float blend = remainder / kSecondsBetweenWaypoints;

    transform.position = Vector3.Lerp(a, b, blend); // same as: a * (1.0f - blend) + b * blend;
}
Edited by Nypyren

Share this post


Link to post
Share on other sites

awesome. ty. I will test this out :)

 

I'm currently using this script and I'm having problems.

 

    public float speed = 5;
 
    void Update()
    {
        transform.position = new Vector3(Mathf.PingPong(Time.time * speed, 10), transform.position.y, transform.position.z);
    }
 
I move the object to a position I want it. When I use Unity to play the game it moves my object to a different starting position than the one I positioned the object. LOL.  I have no clue why its repositioning it.
 
I hope someone can help me out. I'm going to experiment with the code you gave me up above.

Share this post


Link to post
Share on other sites

 

awesome. ty. I will test this out :)

 

I'm currently using this script and I'm having problems.

 

    public float speed = 5;
 
    void Update()
    {
        transform.position = new Vector3(Mathf.PingPong(Time.time * speed, 10), transform.position.y, transform.position.z);
    }
 
I move the object to a position I want it. When I use Unity to play the game it moves my object to a different starting position than the one I positioned the object. LOL.  I have no clue why its repositioning it.
 
I hope someone can help me out. I'm going to experiment with the code you gave me up above.

 

It changes position because Mathf.PingPong returns a value from 0 to length (length in your case being 10).

 

I can't remember the exact syntax and I don't have Unity in front on me, but you could try...

- Create a new Transform variable (not public), startPos.

- On Awake or Start, set startPos to the current position. This should grab the position you set in the editor, and store it in startPos.

- On Update, do startPos.x + Mathf.PingPong() instead of just Mathf.PingPong(). This will take the start position, and then add in the PingPong motion on top of that.

Share this post


Link to post
Share on other sites

Can you put this in code for me so I can copy and paste it?

 

This is my first game ever. Ive only been studing code for about a week now.

Sorry, no.

As I said, I don't remember the exact syntax, and I don't have Unity installed.

 

Edit:

That said, the steps I outlined should be fairly clear. Have you tried following them? If so, which step are you having trouble with?

Edited by Lactose!

Share this post


Link to post
Share on other sites

That said, the steps I outlined should be fairly clear. Have you tried following them? If so, which step are you having trouble with?

(Did you miss that initial exchange up top?)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement