Hey there I wrote a script to move an object to a target point. this is my script: Code (CSharp): private void moveToRnd(){ float x = transform.position.x - newPos.x; float y = 0f; float z = transform.position.z - newPos.z; Vector3 forward = new Vector3(x,y,z); float length = forward.magnitude; forward = (1 / length) * forward; float curSpeed = speed; //motor is a CharacterMotor motor.SimpleMove(curSpeed * forward); gameObject.transform.LookAt (new Vector3 (newPos.x, transform.position.y, newPos.z)); } the only problem is, that the object moves backwards to my target. (It looks in the right direction) every try to turn the direction (*(-1) or change newPos with transform.position) broke my script and the object doesn´t move... can anyone help me? thank you
You can subtract vectors Code (csharp): Vector3 heading = (newPos - transform.position).normalized; transform.position += heading * speed * Time.deltaTime;
Thanks for the reply, but I had a reason for doing that: it doesn't work with this technique. I tried this any way and it did: nothing... My object does not move Why and how do I use the move method from the charactermotor? I tried it like the scripting api said, but this doesn't work for me (my character moves backwards in the wrong direction) Help please!
Code (CSharp): transform.position.x - newPos.x; This will calculate the position to the transform.position.x from newPos.x. I assume you're going backwards because you want to calculate the opposite. Try: Code (CSharp): float x = newPos.x - transform.position.x; float y = 0f; float z = newPos.z - transform.position.z;
Well, i tried this yesterday and it didn`t worked... now i restarted unity and it works... thank you for your help
Okay there is still a problem: my code works, until I add this line: Code (CSharp): transform.LookAt(new Vector3(newPos.x,transform.position.y,newPos.z)); when i add this, my character doesn`t move... could anyone explain me this? thanks