- Home /
Question by
omveer1997 · Oct 26, 2020 at 11:22 AM ·
cameramovementdirectionrotaion
i want my plane to fly in camera direction where head is facing (i am newb)please suggest some changes and better method
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class controls : MonoBehaviour { public Rigidbody jet; public float speed; public float Hspeed; public float acceleration; public float maxspeed; public float rotation; public float Rotaccr; public float maxlift; public float lift; public float liftinc; public float yrot = 0; public float xrot = 0;
void Start()
{
jet = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate()
{
float Vaxis = Input.GetAxis("Vertical");
if(Vaxis>0 && speed<=maxspeed)
{ speed = speed + acceleration * Time.deltaTime; }
if (Vaxis < 0 && speed >= -maxspeed)
{
speed = speed + acceleration * -1 * Time.deltaTime;
}
if (Vaxis < 0 && speed >= -maxspeed && transform.position.y < 1)
{ speed = speed + acceleration * -0.0005f * Time.deltaTime; }
if (Vaxis < 0 && speed>0)
{ speed = 2*(speed + acceleration) * -0.005f * Time.deltaTime; }
if (Vaxis == 0 && speed >0)
{ speed = speed - acceleration * Time.deltaTime; }
if (Vaxis == 0 && speed < 0)
{ speed = speed + acceleration * Time.deltaTime; }
Debug.Log("speed is" + speed);
float Haxis = Input.GetAxis("Horizontal");
if (Haxis > 0 && Hspeed <= 20)
{
Hspeed = Hspeed+(5 * Time.deltaTime); }
if (Haxis < 0 && Hspeed >= -20)
{
Hspeed = Hspeed+ (-5 * Time.deltaTime); }
if (Haxis == 0)
{ Hspeed = 0; }
if (speed==0)
{ Hspeed = 0; }
//rotation codes
bool Rrot = Input.GetKey("e");
bool Lrot = Input.GetKey("q");
if(Rrot==true && rotation<45 &&transform.position.y>2)
{ rotation = rotation+Rotaccr + 1 * Time.deltaTime;}
if (Lrot == true && rotation > -45 && transform.position.y > 2)
{ rotation = rotation+(-1) * Time.deltaTime - Rotaccr; }
if (Lrot == false && Rrot == false )
{ rotation = 0; }
//lift controls
bool Blift = Input.GetKey("f");
if(Blift==true&&speed>acceleration*2&& lift<maxlift)
{ lift = lift + liftinc * Time.deltaTime; }
if(Blift == false && lift>1)
{ lift =lift-lift*Time.deltaTime; }
//jet yaxisi rotation
float VRaxis = Input.GetAxis("Vertical");
float HRaxis = Input.GetAxis("Horizontal");
if (lift==0 &&VRaxis==1&&HRaxis==1)
{ yrot = yrot + 1 * Mathf.Abs(VRaxis * HRaxis) * Time.deltaTime; }
if (lift == 0 && VRaxis == 1 && HRaxis == -1 )
{ yrot = yrot - 1 *Mathf.Abs(VRaxis*HRaxis) *Time.deltaTime; }
if (lift == 0 && VRaxis == -1 && HRaxis == 1 )
{ yrot = yrot - 1 * Mathf.Abs(VRaxis * HRaxis) * Time.deltaTime; }
if (lift == 0 && VRaxis == -1 && HRaxis == -1)
{ yrot = yrot + 1 * Time.deltaTime; }
if(lift==0 &&VRaxis==0&&HRaxis==0)
{ yrot = 0; }
//xaxis rotation
if (speed > 10 && xrot > -360 &&Blift==true)
{ xrot += -0.005f*Time.deltaTime; }
if (transform.position.y > 10 && xrot < 0 && Blift == true)
{ xrot = xrot+2*Time.deltaTime; }
if (transform.position.y>10&& xrot < 0 && Blift == false)
{ xrot +=1; }
jet.AddForce(Hspeed, lift, speed);
jet.angularVelocity = new Vector3(xrot, yrot, rotation*Time.deltaTime);
}
Comment
Your answer
Follow this Question
Related Questions
Move player according to the camera direction 1 Answer
Camera Orbit Changing Characters Direction, How? How To Stop It? 0 Answers
Relative Movement Problem 1 Answer
Camera relative movement 0 Answers