- Home /
My airplane turns but does't roll! Why?
Hello guys,
I am practicing with somewath that in my mind should be a flying airplane.
I created a rigid body and a transform from the mesh of anairplane and I add to it forces in order to have the airplane moving forward and turning left & right.
Here u are the code:
using UnityEngine;
using System.Collections;
public class GameManager : MonoBehaviour{
public float velocitySpeed = 10f;
private Rigidbody playerRB = null;
private Transform playerT = null;
// Use this for initialization
void Start (){
playerRB = (GameObject.FindGameObjectWithTag ("Player") as GameObject).GetComponent<Rigidbody> ();
playerT = (GameObject.FindGameObjectWithTag ("Player") as GameObject).transform;
}
void FixedUpdate ()
{
float rotation = Input.GetAxis ("Horizontal");
float velocity = Input.GetAxis ("Vertical");
if (!Mathf.Approximately (rotation, 0f))
{
if (rotation < 0f) {
playerRB.AddForce (playerT.TransformDirection (Vector3.left) * (velocitySpeed + (velocity * velocitySpeed)));
rotleft();
}
else
{playerRB.AddForce (playerT.TransformDirection (Vector3.right) * (velocitySpeed + (velocity * velocitySpeed)));
rotright();
}
}
else {
playerRB.AddForce (playerT.forward * (velocitySpeed + (velocity * velocitySpeed)));
}
playerT.LookAt(playerT.position + (playerRB.velocity.normalized * 5f));
}
void rotright (){
Vector3 rght = new Vector3 (0,0, 100*velocitySpeed);
playerT.rigidbody.angularVelocity = rght;
}
void rotleft (){
Vector3 lft = new Vector3 (0,0, -100*velocitySpeed);
playerT.rigidbody.angularVelocity = lft;
}
}
Now I would like to add rolling on Z axis when the airplane turn itself right or left. I tryied various ways but no one seems to work. In the script I add, there are two custom functions that act on Z angular velocity(left & right) but the results are unespected: the airplane roll very slight and start to gain altitude...
so I am looking for a good solution in order to roll the airplane during its turning.
Someone could help me please?
Thanks!
Matt
I'd be more than happy to tell you were you went wrong, however your code is unreadable. Please remove any unnecessary white-spacing :)
Answer by prototype7 · Jul 16, 2013 at 03:25 PM
I am going to give you an example of rotating, since angular velocity has some parameters that you are willing to adjust. I used this script to adjust spin ball in pool game, just change the addTorque from y to z axis
public float angDrag;
public float spinForce;
public float maxAngVel;
void FixedUpdate()
{
rigidbody.angularDrag = angDrag;
rigidbody.maxAngularVelocity = maxAngVel;
rigidbody.AddTorque(0,10 * spinForce,0);
if (Input.GetMouseButtonDown(0))
{
this.rigidbody.AddForceAtPosition(Vector3.right * 1000, transform.position);
}
}
Play the game and adjust angDrag, spinForce, maxAngVel in the inspector
I am looking at that, tomorrow (still busy today) I'll try to add and adapt this script to the needs of $$anonymous$$e and I will tell u what it will happen! I think to stop the rotationwith AngularDrag, it should works... In the meantime, thanks for the reply...see u soon! :D
So prototype, i tried but it does't work!
seems that the lookat function create some problems to the main roll of the plane, costraining it to work only on global y axis and not on z (forward) direction.
Some others suggestions, please?
how about to put addTorque after LookAt, just to make sure LooAt doesnt freeze the rotation you want. And the sample AddTorque above need to be triggered every frame because i am not using constantForce.
O$$anonymous$$ protype, I solved using a script oof a friend of $$anonymous$$e that is an engineer :) if u are interested I can cospy & paste it there (the script not my friend! ;)) Some parts are commented in italian buti think is not aproblem for you!
Cheers and still thanks!!!
Answer by Matthew_Crown · Jul 26, 2013 at 02:01 PM
using UnityEngine;
using System.Collections;
public class GameManager : MonoBehaviour
{
public float velocitaBase = 10f;
private Rigidbody playerRB = null;
private Transform playerT = null;
float rotazione = 0f;
float inclinazione = 0f;
float roll = 0f;
float pitch = 0f;
float yaw = 0f;
// Use this for initialization
void Start ()
{
playerRB = (GameObject.FindGameObjectWithTag ("Player") as GameObject).GetComponent<Rigidbody> ();
playerT = (GameObject.FindGameObjectWithTag ("Player") as GameObject).transform;
}
void FixedUpdate ()
{
Quaternion q = Quaternion.identity;
rotazione = Input.GetAxis ("Horizontal"); // ROLL + YAW
inclinazione = Input.GetAxis ("Vertical"); // PITCH
yaw = rotazione * velocitaBase * Time.deltaTime;
roll = yaw * 0.75f;
pitch = inclinazione * Time.deltaTime * 20f;
q.eulerAngles = new Vector3(pitch, yaw, -roll);
playerRB.rotation *= q;
playerRB.velocity = playerT.forward * Time.deltaTime * velocitaBase * 25f;
}
}
U are welcome! :)
hope this will be helpful in some ways for you.
Cheers
Matt
Answer by DiGiaCom-Tech · Feb 02, 2014 at 07:21 PM
The first problem I see in code Matt provided above is that Roll induces Yaw in aircraft so this code has it backwards. The other problem is that after rolling right say 30° and letting go of the stick, the aircraft should roll back to 0° on it's own accord at a given rate. I'm still working/looking for a solution for this.