Making an object move back and forth.

Started by
14 comments, last by Mightymax 7 years, 9 months ago
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 :)

Advertisement

Instead of moving ahead, take a step back, and think why things happen as they happen in the current program.

Understanding the current program improves your understanding of what to change tomorrow.

Do you understand what "transform.Translate" does?

In other words, can you explain why the object moves at all, and why it moves to the right?

Why doesn't it move diagonally instead, for example?

Can you understand what the function of "Vector3.right", "movementSpeed", and "Time.deltaTime" is?

Google can only provide answers if you understand what you're looking for. Otherwise it's just a huge book with random information.

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.

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;
}

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.

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.

Hello to all my stalkers.

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.

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?

Hello to all my stalkers.

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?)

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.
Is this how you get a high reputation? Just going around not answering questions directly?

No, I am trying to teach you how to solve these kind of problems yourself. Letting others solve the problem so you can copy/paste it is not going to work in the long run.

Yes, I understand the code that is written now. I can make the object go any direction.

Great, now can you also figure out when it should change direction?

This topic is closed to new replies.

Advertisement