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