- Home /
How to get this object to move
I'm having a method in C# that finds the position of the player and the end position. The object jumps to the right position, but I would like the object to "walk" there over a period of time. How do I do that?
The code that actually moves the player is `player.transform.position = Vector3.Lerp(playerPos,endPos, Mathf.SmoothStep(0.0f, 1.0f, 1.0f));`
I've also tried to rotate this object towards this coordinate, but it doesn't rotate all the way towards it.
The code snippet I used for that is `player.transform.rotation = Quaternion.Slerp(player.transform.rotation, Quaternion.LookRotation(endPos), rotationSpeed * Time.deltaTime);`
Here's my code:
public Vector3 MoveToCoordinate(int[] matrix)
{
int x_pos = (int)(matrix[0] * square[0,0].getMWidth());
int z_pos = (int)(matrix[1] * square[0,0].getMLength());
x_pos +=(int)square[0,0].getMWidth()/2;
z_pos +=(int)square[0,0].getMLength()/2;
Vector3 playerPos = player.transform.position;
Vector3 endPos = new Vector3(x_pos,0,z_pos);
WalkToCoordinate(playerPos, endPos);
player.transform.position = Vector3.Lerp(playerPos,endPos, Mathf.SmoothStep(0.0f, 1.0f, 1.0f));
return endPos;
}
Thanks in advance.
I've put the code an a method, and calls this method from Update(), is this possible? It don't work here… The code I'm using when trying to move the player to a given coordinate, it won't move… I can get them to jump to their coordinate, but would like to move them ins$$anonymous$$d.
player.transform.position = Vector3.Lerp(startPos, endPos, $$anonymous$$athf.SmoothStep(0.0f, 1.0f, 1.0f));
As a debugging step, have you tried simply using "player.transform.position = endPos;"? Is endPos really a position, or is it actually an offset from the playerPos? One last question: why are you calling a "WalkToCoordinate" method, and then also modifying the transform directly? $$anonymous$$ight these be conflicting?
endPos is a Vector3 position, not an offset from the player, I'm able to make the player "jump" to this coordinate, I just can't animate that jump. The jump is near instant.
Sorry for the confusing code, the WalkToCoordinate() method is created in a debug purpose. I'm (un)commenting the player.transform.position = (…) and WalkToCoordinate() method just to learn how these things works.
Answer by kmeboe · Sep 28, 2012 at 03:09 PM
Ok, now I think I see the problem. You are using the "Lerp" method, but you're essentially telling it to instantly jump to the end position. There are three things you need to do to accomplish your goal:
Instead of passing SmoothStep to your Lerp, pass Time.deltaTime. You can multiply it by a factor to change the movement if you like -- just keep the factor constant.
Make sure that "startPos" represents the player's position for each call to Lerp(). You can do this by grabbing the transform's position each time, if you want.
To find out when you're done, you can compare the player's current position with the end position, and either see if it is equal (not sure if this will work exactly), or see if it's "close enough," using a distance.
There may be more efficient ways to do this, and maybe others can pipe in if this is the case.
If you need specific help on any of these steps, let me know; I'll be glad to explain further. Good luck!
It worked! ッ
I've updated my code to: "player.transform.position = Vector3.Lerp(player.transform.position,endPos, walkSpeed * Time.deltaTime);" The object now stops when it has reached it's destination, no further program$$anonymous$$g where needed. Have not tested and evaluated the code yet, but so far so good! Thanks!
Answer by $$anonymous$$ · Sep 28, 2012 at 08:59 AM
Appriciate all the comments so far, thanks.
To make things more clear, I'm posting more of my code.
Here is my Update() method:
void Update () {
if(counter == 0)
{
counter++;
getObject();
}
if(move)
{
WalkToCoordinate(startPos, endPos);
}
}
The player is set to move once the dice is ready, this bool is not set to false again, since I don't know how this object can report that it has arrived at its destination. This is the WalkToCoordinate method:
public void WalkToCoordinate(Vector3 startPos1, Vector3 endPos1)
{
int rotationSpeed =15;
int moveSpeed = 1;
Debug.DrawLine(startPos1, endPos1, Color.magenta);
Vector3 moving = endPos1;
Transform goTransform = this.GetComponent<Transform>();
//Look at square
player.transform.rotation = Quaternion.Slerp(player.transform.rotation, Quaternion.LookRotation(endPos1), rotationSpeed * Time.deltaTime);
//Move towards target
player.transform.position = Vector3.Lerp(startPos, endPos, Mathf.SmoothStep(0.0f, 1.0f, 1.0f));
//move = false;
}
I don't understand how to make this object move in a linear space to the given coordinate, and then return something when it has arrived.