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;
	}	
	}

alt text

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 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

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! :slight_smile:

hope this will be helpful in some ways for you.

Cheers

Matt

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.