{
transform.Translate(Vector3.right * movementSpeed * Time.deltaTime);
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.
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.
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.
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?
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?)
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?