- Home /
moving and rotating my object in 3d
I'm making a flight game and I'm currently just trying to get my ship to move at a constant speed and be able to turn in 3 dimensions. If i don't use my moveforward.js file, everything works fine, the camera follows behind the ship as it rotates and its great. but there seems to be a compounding problem when i add velocity along the objects z axis. if i turn more than 90 degrees in any direction the ship still wants to go in a positive z direction( relative to the world). so i start flying backwards. Here are my files:
movedirection.js (attached to the ship)
var turnspeed : float= 6.0;
function Update () {
var x = Input.GetAxis("Horizontal") Time.deltaTime turnspeed; var z = Input.GetAxis("Vertical") Time.deltaTime turnspeed; transform.Rotate(z, 0, -x);
}
moveforward.js (attached to the ship)
var speed : float=1.0; function FixedUpdate () {
moveDirection = transform.TransformDirection(Vector3.forward)*Time.deltaTime*speed;
transform.Translate(moveDirection);
}
follow.js (attached to the chase camera)
var target : Transform;
var distance = 1.6;
var damping =2.0;
var smoothRotation = true;
var rotationDamping =2.0;
function LateUpdate() {
var wantedPosition = target.TransformPoint(0, 0, -distance);
transform.position = Vector3.Lerp (transform.position, wantedPosition,
Time.deltaTime * damping);
if (smoothRotation) {
var wantedRotation = Quaternion.LookRotation(target.position -
transform.position, target.up);
transform.rotation = Quaternion.Slerp (transform.rotation, wantedRotation,
Time.deltaTime * rotationDamping);
}
else transform.LookAt (target, target.up);
}.
I think I'm just to close to the problem right now, its like its on the tip of my tongue and I can't quite get it.
1st highlight the script and press the 10101010 button
2 are you moving by physics or by transform magic?
Answer by nibbcnoble · Jul 31, 2011 at 08:55 PM
Im moving by way of transform.translate
var speed : float=1.0; function FixedUpdate () {
moveDirection = transform.TransformDirection(Vector3.forward)*Time.deltaTime*speed;
transform.Translate(moveDirection);
}