Hello, I am somewhat new to unity and I have a question about player movement. I want to make a top an overhead view shooter (in the style of geometry wars). I am using the following code to move the player using WASD and it works fine, but how to I get the the player object to rotate. How can I get it to be like the ship in this game http://www.youtube.com/watch?v=Jhhocl7jf3o? So that when the A key is pressed and the ship moves left (it is moving along the ZX plane) it rotates so that the front of ship is facing left, etc. What script would accomplish this?
Thank you.
var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;
private var moveDirection : Vector3 = Vector3.zero;
function Update() {
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded) {
// We are grounded, so recalculate
// move direction directly from axes
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
controller.Move(moveDirection * Time.deltaTime);
}
Something like the following -
var lookDirection : Vector3 = moveDirection;
lookDirection.y = 0;
if ( lookDirection != Vector3.zero )
transform.rotation = Quaternion.Slerp( transform.rotation, Quaternion.LookRotation( lookDirection ), Time.deltaTime );
Thank you. I added your script to the existing one and it rotates, but I have encountered new problems (sorry, I am new). When I try to use WASD to move, the ship just swings around some large arc and I cant get it to go where I want. Is there anyway to have it just snap to a rotation( you know, how third person action games are, you can control 360 degrees of motion and movement with one stick) And how would I handle the camera, if I attach it to the ship, it doesn’t look like it is moving because it is spinning too.
Thank you for your help.
var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;
private var moveDirection : Vector3 = Vector3.zero;
function Update() {
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded) {
// We are grounded, so recalculate
// move direction directly from axes
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
var lookDirection : Vector3 = moveDirection;
lookDirection.y = 0;
if ( lookDirection != Vector3.zero )
transform.rotation = Quaternion.Slerp( transform.rotation, Quaternion.LookRotation( lookDirection ), Time.deltaTime );
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
controller.Move(moveDirection * Time.deltaTime);
}
Also, I found this script, but I dont get how to use it, I want to make it so that the object looks in the direction of the mouse (kind of a reverse of the way geometry wars is) but it doesnt work. When I set something is a target in the unity inspector, it just faces that? How can I make it face the mouse cursor?
var target : Transform;
function Update ()
{
transform.LookAt(Vector3(target.position.x, transform.position.y, target.position.z));
}