- Home /
Move forward to the endposition
Hey guys,
I wrote a script which should move the player to a target point. My player consists of a invisible parent with all scripts and colliders attached and a child which has only the model and the rotation script attached. The game uses the 2D physics but the model is 3D.
Let's start with the "RotationScript" in the child:
private float horizontal;
private float vertical;
public bool rotateOn;
public float angle;
// Use this for initialization
void Start () {
rotateOn = true;
}
// Update is called once per frame
void LateUpdate () {
if (rotateOn){
if (Input.GetButton ("Vertical") || Input.GetButton ("Horizontal")) {
horizontal = Input.GetAxisRaw ("Horizontal");
vertical = Input.GetAxisRaw ("Vertical");
//this is for invert horizontal
if(horizontal > 0){
horizontal = -horizontal;
} else if(horizontal < 0){
horizontal = horizontal - horizontal - horizontal;
}
}
}
if (horizontal != 0 || vertical != 0) {
angle = Mathf.Atan2 (horizontal, vertical) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Lerp (transform.rotation, Quaternion.Euler (new Vector3 (0, 0, angle)), Time.deltaTime * 10);
}
}
I take the Input from the axes and lerp it so my model look in the right direction. This works pretty well, the only thing is that I have to invert my horizontal.
And here is my "MoveScript" in the parent:
Vector3 moveVec;
Vector3 dirVec;
void Update () {
dirVec = transform.FindChild("Modell").localRotation * new Vector3(0,1,1);
dirVec.z = 0;
}
public void MovePlayer(Vector3 endPos, float speed) {
rotate.rotateOn = false;
moveVec = new Vector2 (dirVec.x * speed, dirVec.y * speed);
rigidbody2D.velocity = moveVec;
if (Vector3.Distance(endPos, transform.position) <= 0.1f) {
rigidbody2D.velocity = Vector2.zero;
targetPos = true;
} else if (Vector3.Distance(endPos, transform.position) > 0.1f){
targetPos = false;
}
}
The dirVec is the converted vector from the rotation of the model. And moveVec is the same with speed so I can translate it to velocity. Oh and the MovePlayer function is called by FixedUpdate. The whole script work right as well, but without the rotation part.
Ok so my current problem is: The script works, but when the player is in rotation and something starts the movePlayer function my vec's aren't what the should and the player past the endpoint and moves to far. After hours of testing I give up and hope somebody can help me :)
Thank you so much for your time^^!
You are using the LERPed direction (transform.FindChild("$$anonymous$$odell").localRotation), rather than the "final" direction ( Quaternion.Euler (new Vector3 (0, 0, angle))).
If the LERPed direction does not change fast enough, you could certainly miss and overshoot your destination mark.
To test if this is the issue: replace the LERP with a direct assignment (it wont look good, but it's just for testing).
Hey Glurth,
ok I replaced lerp the line with:
transform.rotation = Quaternion.Euler (new Vector3 (0, 0, angle));
Now the problem actually happens rarer, but it still happens. So I made a Debug.Log with all important informations:
dirVec: (1.0, 0.0, 0.0)
moveVec: (7.0, 0.0, 0.0)
Rotation: (0.0, 0.0, 1.0, 0.0)
CurPos: (-10.8, -8.8, 0.4)
EndPos: (-8.8, -8.8, 0.4)
----
dirVec: (0.0, -1.0, 0.0)
moveVec: (0.0, -7.0, 0.0)
Rotation: (0.0, 0.0, 1.0, 0.0)
CurPos: (-10.7, -8.8, 0.4)
EndPos: (-8.8, -8.8, 0.4)
----
dirVec: (0.0, -1.0, 0.0)
moveVec: (0.0, -7.0, 0.0)
Rotation: (0.0, 0.0, 1.0, 0.0)
CurPos: (-10.7, -8.8, 0.4)
EndPos: (-8.8, -8.8, 0.4)
----
Every further run's/frames look like the second and thrid. Something weird happens after the first run of the script.
Oh and to be sure that my endPos is not the problem here's the code(but I don't think there's something wrong):
endPos = transform.position + dirVec * range;
range is a float like 5.0f.
Hope you can help me :)
And thanks!^^
Hmm, I'm not sure why the position does not change after the first pass. When is $$anonymous$$ovePlayer() being called? EDIT: ah, I see now where you said - FixedUpdate.
Also, not sure why your velocity vector direction changes, on the second pass even though the "rotation" you are displaying dosn't.
The next question I would have is, how do you detect when the player is at the endpoint? It appears you are not using colliders. So, you will need to check yourself, if the player has moved PAST the endpoint, since the last call to update.(a common, ugly issue: most unity programmers use colliders for this)
if (Vector3.Distance(endPos, transform.position) <= 0.1f)
e.g. this line is VERY dependent upon the velocity of the object and the timescale between calls.