- Home /
Flight Control/Banking
I have a ship flying in a zero g environment. I have a problem. The problem is that I need to make the ship bank. When I turn, it makes the ship loo unrealistic. The ship follows the mouse, so by pressing forward and moving the mouse to the right, the ship turns to the right & vice versa. But the ship doesn't bank. Is this even possible to do?
This is the only way I could find out how to do this. It works but it's not efficient. I wanted the ship to follow the mouse, but the ship itself would not follow the mouse, but if I put the script on the camera it would. So what I did was make ship "follow" the camera, but made the follow value negative, therefore putting the camera in front. I don't think there are banking answers if this is the way I'm doing it. Is there an even more efficient way to make space flight control? Then maybe the banking answers will work.
Answer by aldonaletto · Aug 06, 2011 at 03:22 AM
The best way is to have two objects: an empty object to move around, and a child object to do the banking - your model or the first person camera will be attached to this child object. This way the banking will not interfere with the movement, and you can bank sideways and back/forth without any problem.
You can find a similar question with all details about how to do this here.
EDITED: Do the following:
1- Create an empty object and name it "Model";
2- Drag this object to your spaceship (to make it a spaceship child);
3- Zero its rotation and position;
4- Drag your model to this object;
5- Attach the script below to this object;
function Update(){ var v = Input.GetAxis("Vertical"); // use the same axis that move back/forth var h = Input.GetAxis("Horizontal"); // use the same axis that turns left/right transform.localEulerAngles.x = v*15; // forth/back banking first! transform.localEulerAngles.z = -h*15; // left/right }This script just observes the same axes you use to move the spaceship, and banks according to their values - if you use Input.GetAxis("Mouse X") to turn left/right, for instance, substitute the "Horizontal" name above with "Mouse X".
The spaceship should be an empty object, with the object Model childed to it, and the model itself childed to Model. Your camera target should be set to the spaceship object - as it is already, I suppose.
Sorry, I should have been more specific. The camera is behind the ship (third person), not first person.
No problem: child the model to the banking object, and set the camera target to the external object. The basic idea is to separate the main movement from the banking, or else you will get completely crazy, because the banking will interfere with the movement and vice versa. Take a look at that answer, and if you can't see how to apply it to your case, post in your question the movement script and I'll tell you what to do.
Thank you for your patience but I posted a problem (below the original question) that might interfere with the advice you are giving me.
Which script are you using in your camera? $$anonymous$$ouseLook.cs?
Take a look at my answer: I modified it to include the banking script. I hope it works well - let me know if you have problems.
Answer by davedouggreen · Feb 13, 2021 at 02:53 PM
First of all thanks to @aldonaletto for a good solution. For those weary travelers who find themselves here, looking for a basic solution. Here is my version of the idea suggested above with regards to separating the movement from the banking and which seems to work fine, at least for now. This code was is for a basic game where you control a mosquito and I wanted the insect to bank as it moved from left to right for a bit more realism, and as stated, using this code it now does so.
The structure is exactly as suggested above with parent and child objects and with the respective scripts attached to each of those objects.
PlayerController Script
void HandleInput()
{
horInput = Input.GetAxis("Horizontal");
vertInput = Input.GetAxis("Vertical");
//rb.AddForce(Vector3.right * speed * horInput);
//rb.AddForce(Vector3.forward * speed * vertInput);
transform.Translate(Vector3.right * speed * horInput * Time.deltaTime);
//transform.Translate(Vector3.up * speed * vertInput * Time.deltaTime);
}
BankingController Script
void HandleInput()
{
horInput = Input.GetAxis("Horizontal");
z = -horInput;
//modifying the Vector3, based on input multiplied by speed and time
currentEulerAngles += new Vector3(0, 0, z) * Time.deltaTime * rotationSpeed;
//moving the value of the Vector3 into Quanternion.eulerAngle format
currentRotation.eulerAngles = currentEulerAngles;
//apply the Quaternion.eulerAngles change to the gameObject
transform.rotation = currentRotation;
}
Hope someone finds this useful.
Thanks.
Your answer
![](https://koobas.hobune.stream/wayback/20220612171348im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
Space fighter banking 1 Answer
Saitek X52 Throttle value question 2 Answers
How do I use the mouse to control a spacecraft like in this game? 1 Answer
How could I use the mouse as input for a flight control system (and space bar for thrust!) 2 Answers