- Home /
Mouse based based aeroplane movement
I tried to move a aeroplane for my game using this following script below
var horizontalSpeed : float = 2.0;
var verticalSpeed : float = 2.0;
function FixedUpdate () {
Screen.showCursor = false; //hide the mouse cursor in game
// Get the mouse delta. This is not in the range -1...1
var h : float = horizontalSpeed * Input.GetAxis ("Mouse X");
var v : float = verticalSpeed * Input.GetAxis ("Mouse Y");
transform.Rotate (-v, h, 0);
}
it is used together with the following script
var speed = 20;
function Update () {
if (Input.GetKey(KeyCode.UpArrow)){
speed +=2;
}
if (Input.GetKey(KeyCode.DownArrow)){
speed -=2;
}
transform.Translate(Vector3.forward *speed* Time.deltaTime);
}
the up/down arrow keys increase and decrease the speed, problem is it does not work excatly as i expect does anyone have any input or suggestions for the code
my objective is to get the plane to move up, down, right, left
but the 'left side to right side' tilt of the plane (Roll), is controlled by the left and right arrow keys, but the plane keeps turning upside down after moveing the mouse for a while
I need it to turn where i point, but (the roll is done from the left/right keys), what is going wrong here, (I am new to unity script, but i learnt java and javascript for web development)
Answer by tonydemot · Feb 08, 2012 at 05:24 AM
you can add a mathf.clamp to clamp the rotation to a certain degree or do you want it to spin around ?
Thanks but i don't know how mathf does clamping like that?, well i don;t want it to spin around, i want it to move like the 1st person script, but roll when turning left or right, and pitching up and down when increase up and down
Answer by aliakbo · Feb 08, 2012 at 07:11 PM
Your problem is here
var speed = 20;
function Update () {
if (Input.GetKey(KeyCode.UpArrow)){
speed +=2;
}
if (Input.GetKey(KeyCode.DownArrow)){
speed -=2;
}
transform.Translate(Vector3.forward*speed* Time.deltaTime);
}
As you can see,
speed +=2;
Speed will constantly increase while you are holding down the up/down arrow. You could restrict the limit on speed by putting if in a if statement
if( speed< 20)
speed +=2;
Thanks the movement can be limited, true but my real problem is the movement using the mouse, i need the aeroplane to be directed by the mouse, can you help me with that
Your answer
![](https://koobas.hobune.stream/wayback/20220613063826im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Reseting materials color after mass selecting them during run time? 1 Answer
Yaw Pitch Roll order? 0 Answers
RTS movement not working properly 1 Answer
mouse project settings 0 Answers
Trying to keep an object 1.5 above the terrain at all times 1 Answer