- Home /
Movement In World axis Not object axis.
Hi, I'm a 3D artist working on a small project, and note I said artist. I'm in no way shape or form a programmer, I just started learning Javascript about a week ago. Anyway I'm working on a 2.5D Project And i have the Character rotating With the mouse Location. However when the character moves It's relative to The characters axis so he moves in whatever direction he is facing. I'm looking for A very simple script to make it so No matter what direction the character is facing, The movement will always be in the same direction. now and this is most likely very incorrect. but i figured it would be something like this.
Blockquote
Blockquote> function Update() {
Input.GetAxis("Horizontal") * Time.deltaTime * speed, Space.World;
Input.GetAxis("Vertical") * Time.deltaTime * speed, Space.World;
}
} Blockquote
Blockquote
But this keeps coming back with a expecting ";" error on line 6 and 7 so i am very confused. mainly because i have almost no knowledge of this language
I am just trying to do this until My Programmer Can understand exactly what the project is going for.
Answer by Kourosh · Apr 28, 2011 at 04:30 AM
1.Create an empty game object, name it "Direction" and rotate it so that the z axis (forward) is facing the direction you want.
2.Attach the script bellow to your character:
var fixedDirection:Transform; //drag 'n drop the "Direction" game object to this slot. var speed:float = 1.0; //Change to whatever you want. function Update(){
transform.Translate(fixedDirection.forward Input.GetAxis("Vertical") Time.deltaTime speed); transform.Translate(fixedDirection.right Input.GetAxis("Horizontal") Time.deltaTime speed);
}
I haven't tested it tho. But it should work.
I't doesn't work along side of my mouse look script. Which is
ar speed = 4.0;
function Update () {
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);
var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
}
}
It Works fine on its own though.
Ye cause both scripts are trying to manipulate the character at the same time. Here is the solution. You need another empty game object and parent your character under it. Assign the script I wrote here to the parent ins$$anonymous$$d of the main character itself.