I have a problem with getting the right positioning system to work with my character.
Lats say we have a pintA(X=0, Y=0, Z=0) and a pointB(X=10, Y=0,z=10). So I want my character to go from point A to point B. I have this code (Javascript):
var smooth:int; //This I have set to 10 in editor
private var targetPosition:Vector3;
function Update () {
if(Input.GetKeyDown(KeyCode.Mouse0))
{
var playerPlane = new Plane(Vector3.up, transform.position);
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hitdist = 0.0;
if (playerPlane.Raycast (ray, hitdist)) {
var targetPoint = ray.GetPoint(hitdist);
targetPosition = ray.GetPoint(hitdist);
var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
transform.rotation = targetRotation;
}
}
transform.position = Vector3.MoveTowards(transform.position, targetPosition, Time.deltaTime * smooth);
}
Now, this code works fine and does exactly wat it’s supposed to do (move the character slowly to were you pressed with the cursor). However, its not really what I want. This script makes it so that wen the character moves from PointA to PoinB it walk diagonally to point B. I want to do it in a more “blocky” way. So that the character maybe first walks to pointB’s X then turns and moves to PointB’s Z and Finally if it can, then move to pointB’s Y. I would really appreciate answears and even more if the answear could be in javascript (however I do understand C but the rest of the game is in javascript… so… yeah)
Greetings from Erik