- Home /
Airplane Script Control
Hello, I'm new to Unity programming. The problem with plane is that it can move around but, the plane stays facing one direction. This is the program for it at the bottom. Can anyone help. Thank You.
private var controller :CharacterController; controller = gameObject.GetComponent(CharacterController); private var moveDirection = Vector3.zero; private var forward = Vector3.zero; private var right = Vector3.zero;
function Update() { forward = transform.forward; right = Vector3(forward.z, 0, -forward.x); var horizontalInput = Input.GetAxisRaw("Horizontal"); var verticalInput = Input.GetAxisRaw("Vertical"); var targetDirection = horizontalInput right + verticalInput forward;
moveDirection = Vector3.RotateTowards(moveDirection, targetDirection, 200 Mathf.Deg2Rad Time.deltaTime, 1000); var movement = moveDirection Time.deltaTime 2; controller.Move(movement); transform.rotation = Quaternion.LookRotation(moveDirection); if (targetDirection != Vector3.zero) { transform.rotation = Quaternion.LookRotation(moveDirection); } }
@script RequireComponent(CharacterController)
Answer by aldonaletto · Aug 12, 2011 at 12:55 PM
You should do any movement or direction change inside the if:
private var controller :CharacterController; controller = gameObject.GetComponent(CharacterController); private var moveDirection = Vector3.zero; private var forward = Vector3.zero; private var right = Vector3.zero;
function Update() { forward = transform.forward; right = Vector3(forward.z, 0, -forward.x); var horizontalInput = Input.GetAxisRaw("Horizontal"); var verticalInput = Input.GetAxisRaw("Vertical"); var targetDirection = horizontalInput right + verticalInput forward;
if (targetDirection != Vector3.zero){ moveDirection = Vector3.RotateTowards(moveDirection, targetDirection, 200 Mathf.Deg2Rad Time.deltaTime, 1000); var movement = moveDirection Time.deltaTime 2; controller.Move(movement); transform.rotation = Quaternion.LookRotation(moveDirection); } }
@script RequireComponent(CharacterController) But this isn't the best way to do what you want (this script looks like a Frankenstein creature buit from 3rd Person Controller dead scripts, and gets crazy when you try to go backwards).
An easier way to get the same results without this backward issue is:
var turnSpeed: float = 90; var moveSpeed: float = 5;
private var controller :CharacterController; controller = gameObject.GetComponent(CharacterController);
function Update() { var horizontalInput = Input.GetAxisRaw("Horizontal"); var verticalInput = Input.GetAxisRaw("Vertical"); transform.Rotate(0, horizontalInput turnSpeed Time.deltaTime, 0); var movement = transform.forward verticalInput Time.deltaTime * moveSpeed; controller.Move(movement); }
@script RequireComponent(CharacterController)
The plane is able to rotate now without getting stuck in one direction. Thank you for your help.
@jonmar2348, if this answer solved the problem, please mark it as accepted: click the check mark below the voting thumbs, so the answer will become green and help other people with similar problems ;)
Your answer
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
disable scripts 1 Answer
Newbie... Master script? 5 Answers
Vehicle/car/ Movement 0 Answers