| using System.Collections; |
| using System.Collections.Generic; |
| using UnityEngine; |
| |
| public class DroneMovement : MonoBehaviour |
| { |
| // Start is called before the first frame update |
| Rigidbody ourDrone; |
| public AxisInputUI AxisLeft; |
| public AxisInputUI AxisRight; |
| public AxisInputUI AxisTop; |
| public AxisInputUI AxisBottom; |
| private bool gyroEnable; |
| private Gyroscope gyro; |
| void Awake() |
| { |
| ourDrone = GetComponent<Rigidbody>(); |
| } |
| void Start() |
| { |
| gyroEnable = EnableGyro(); |
| } |
| private bool EnableGyro() |
| { |
| |
| if (SystemInfo.supportsGyroscope) |
| { |
| gyro = Input.gyro; |
| gyro.enabled = true; |
| |
| return true; |
| } |
| return false; |
| } |
| |
| |
| void FixedUpdate() |
| { |
| // if (!AxisTop.holdL && (!AxisBottom.holdL)) |
| // { |
| MovementUD(); |
| MovementF(); |
| Rotation(); |
| ClampingSpeedValues(); |
| Swerve(); |
| Yaw(); |
| |
| Throttle(); |
| |
| ourDrone.AddRelativeForce(Vector3.up * UpForce); |
| ourDrone.rotation = Quaternion.Euler( |
| new Vector3(tiltAmountF, currentYrot, tiltAmountSideways) |
| ); |
| } |
| public float UpForce; |
| void MovementUD() |
| { |
| if ((Mathf.Abs(Input.gyro.gravity.y) > 0.2f || Mathf.Abs(Input.gyro.gravity.x) > 0.2f)) |
| { |
| if (Input.gyro.gravity.y<-0.1f || Input.gyro.gravity.y>0.1f) |
| { |
| ourDrone.velocity = ourDrone.velocity; |
| } |
| if (!(Input.gyro.gravity.y<-0.1f) && !(Input.gyro.gravity.y>0.1f) && !(Input.gyro.gravity.x<-0.1f) && !(Input.gyro.gravity.x>0.1f)) |
| { |
| ourDrone.velocity = new Vector3(ourDrone.velocity.x, Mathf.Lerp(ourDrone.velocity.y, 0, Time.deltaTime * 5), ourDrone.velocity.z); |
| UpForce = 100; |
| } |
| if (!(Input.gyro.gravity.y<-0.1f) && !(Input.gyro.gravity.y>0.1f) && (Input.gyro.gravity.x<-0.1f|| Input.gyro.gravity.x>0.1f)) |
| { |
| ourDrone.velocity = new Vector3(ourDrone.velocity.x, Mathf.Lerp(ourDrone.velocity.y, 0, Time.deltaTime * 5), ourDrone.velocity.z); |
| UpForce = 110; |
| } |
| if (Input.gyro.gravity.x>0 || Input.gyro.gravity.x<-0.1) |
| { |
| UpForce = 100; |
| } |
| } |
| if (Mathf.Abs(Input.gyro.gravity.y) < 0.2f && Mathf.Abs(Input.gyro.gravity.x) > 0.2f) |
| { |
| UpForce = 350; |
| } |
| |
| |
| |
| |
| if (Input.gyro.gravity.y>0.1f)//DOWN |
| { |
| UpForce = 175; //FORCE |
| if (Mathf.Abs(Input.gyro.gravity.x) > 0.2f) |
| { |
| UpForce = 200; |
| } |
| } |
| |
| else if (Input.gyro.gravity.y<-0.1f)//UP |
| { |
| UpForce = 30; |
| } |
| |
| else if (!(Input.gyro.gravity.y<-0.1f) && !(Input.gyro.gravity.y>0.1f)) |
| { |
| UpForce = 98.1f; |
| } |
| } |
| private float movementFspeed = 500.0f; |
| private float tiltAmountF = 0; |
| private float tiltVelocityF = 0; |
| void MovementF() |
| { |
| if (Input.gyro.gravity.y != 0) |
| { |
| ourDrone.AddRelativeForce(Vector3.forward *- Input.gyro.gravity.y * movementFspeed); |
| tiltAmountF = Mathf.SmoothDamp(tiltAmountF, -20 * Input.gyro.gravity.y, ref tiltVelocityF, 0.1f); |
| } |
| } |
| |
| private float wantedYrot; |
| private float currentYrot; |
| private float rotateAmount = 1f; |
| private float rotationYvelocity=0.4f; |
| |
| void Rotation() |
| { |
| if (Input.gyro.gravity.x>0.1f) |
| { |
| wantedYrot += rotateAmount; |
| } |
| if (Input.gyro.gravity.x<-0.1f) |
| { |
| wantedYrot -= rotateAmount; |
| } |
| |
| currentYrot = Mathf.SmoothDamp(currentYrot, wantedYrot, ref rotationYvelocity, 0.25f); |
| } |
| private Vector3 velocityToSmoothDampToZero; |
| void ClampingSpeedValues() |
| { |
| if (Input.gyro.gravity.y > 0.2f && Mathf.Abs(Input.gyro.gravity.x) > 0.2f) |
| { |
| ourDrone.velocity = Vector3.ClampMagnitude(ourDrone.velocity, Mathf.Lerp(ourDrone.velocity.magnitude, 10.0f, Time.deltaTime * 5f)); |
| } |
| if (Input.gyro.gravity.y > 0.2f && Mathf.Abs(Input.gyro.gravity.x) < 0.2f) |
| { |
| ourDrone.velocity = Vector3.ClampMagnitude(ourDrone.velocity, Mathf.Lerp(ourDrone.velocity.magnitude, 10.0f, Time.deltaTime * 5f)); |
| } |
| if (Input.gyro.gravity.y < 0.2f && Mathf.Abs(Input.gyro.gravity.x) > 0.2f) |
| { |
| ourDrone.velocity = Vector3.ClampMagnitude(ourDrone.velocity, Mathf.Lerp(ourDrone.velocity.magnitude, 5.0f, Time.deltaTime * 5f)); |
| } |
| if (Input.gyro.gravity.y < 0.2f && Mathf.Abs(Input.gyro.gravity.x) < 0.2f) |
| { |
| ourDrone.velocity = Vector3.SmoothDamp(ourDrone.velocity, Vector3.zero, ref velocityToSmoothDampToZero, 0.95f); |
| } |
| } |
| |
| private float sideMovementAmount = 300.0f; |
| private float tiltAmountSideways; |
| private float tiltAmountVelocity; |
| void Swerve() |
| { |
| if (Mathf.Abs(Input.gyro.gravity.x) > 0.2f) |
| { |
| ourDrone.AddRelativeForce(Vector3.right * -Input.gyro.gravity.x * sideMovementAmount); |
| tiltAmountSideways = Mathf.SmoothDamp(tiltAmountSideways, 20 * Input.gyro.gravity.x, ref tiltAmountVelocity, 0.1f); |
| } |
| else |
| { |
| tiltAmountSideways = Mathf.SmoothDamp(tiltAmountSideways, 0, ref tiltAmountVelocity, 0.1f); |
| } |
| } |
| public float speedRotation; |
| void Yaw() |
| { |
| if (AxisLeft.holdL) |
| { |
| |
| currentYrot -= speedRotation; |
| wantedYrot -= speedRotation; |
| } |
| if (AxisRight.holdL) |
| { |
| |
| currentYrot += speedRotation; |
| wantedYrot += speedRotation; |
| } |
| } |
| |
| void Throttle() |
| { |
| if (AxisBottom.holdL) |
| { |
| |
| UpForce = 120; |
| } |
| if (AxisTop.holdL) |
| { |
| |
| UpForce = 40; |
| } |
| } |
| |
| |
| |
| } |