Search Unity

  1. Click here to receive a gift with your purchase of Unity Pro or Unity Enterprise.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Problem with implementation of Jet + hovercraft mechanics.

Discussion in 'Physics' started by pikachuxxx, Dec 21, 2019.

  1. pikachuxxx

    pikachuxxx

    Joined:
    Jan 22, 2019
    Posts:
    17
    So I'm making a flight cum hover craft mechanism, so I used the character controller to move my Gameobject and I want the model attached to it tilt so that it seem like it's moving parallel to the slope rather than colliding with it. Here is my code :
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3.  
    4. public class PlayerMotor : MonoBehaviour
    5. {
    6.     //Public Variales
    7.     [Header("Jet Settigns")]
    8.     [Tooltip("Turing this setting to get the per perfect feel of the mechanism")]
    9.     public float baseSpeed = 30f;
    10.     public float rotSpeedX = 40f;
    11.     public float rotSpeedY = 25f;
    12.     public float Restoration_Speed = 40f;
    13.     public float Tilt_Sensitivity = 3f;
    14.     [Header("Gameplay Settigns")]
    15.     public float Speeding_Height = 12f;
    16.     [Header("Flight Model")]
    17.     public GameObject Flight_Model;
    18.  
    19.     //Script References
    20.     [Space]
    21.     [Header("Script references")]
    22.  
    23.  
    24.     //Private Variables
    25.     float InputX;
    26.     float InputY;
    27.     private CharacterController controller;
    28.     [Space]
    29.     [Header("Jet Parameters")]
    30.     [SerializeField]private float Max_Speed = 60f;
    31.     [SerializeField]private float Min_Speed = 40f;
    32.  
    33.  
    34.     //Gameplay Methods
    35.  
    36.  
    37.     private void Start()
    38.     {
    39.         controller = GetComponent<CharacterController>();
    40.         //transform.rotation = Quaternion.Euler(-50f, 50f, 50f);
    41.  
    42.     }//Start
    43.  
    44.     private void Update()
    45.     {
    46.  
    47.         PlayerTranlationAndInput();
    48.         //StabilizeJet();
    49.         //SlopeAndHeightLogic();
    50.         SpeedControl();
    51.  
    52.  
    53.     }//Update
    54.  
    55.     private void FixedUpdate()
    56.     {
    57.         FakeTilt();
    58.     }//FixedUpdate
    59.  
    60.  
    61.     //Custom Methods
    62.  
    63.  
    64.     void SlopeAndHeightLogic() {
    65.         if (Physics.Raycast(transform.position,Vector3.forward * -1, out RaycastHit HitInfo, 500f))
    66.         {
    67.             print(HitInfo.distance);
    68.             print(Vector3.Angle(Vector3.forward,HitInfo.normal));
    69.  
    70.         }
    71.     }
    72.  
    73.  
    74.  
    75.  
    76.     void PlayerTranlationAndInput()
    77.     {
    78.         // Give player forward player velocity
    79.         Vector3 moveVector = transform.forward * baseSpeed;
    80.  
    81.         // Gather player's input
    82.         InputX = Input.GetAxis(Axis.HORIZONTAL);
    83.         InputY = Input.GetAxis(Axis.VERTICAL);
    84.         // Get the delta direction
    85.         Vector3 yaw = InputX * transform.right * rotSpeedX * Time.deltaTime;
    86.         Vector3 pitch = InputY * transform.up * rotSpeedY * Time.deltaTime;
    87.         Vector3 dir = yaw + pitch;
    88.  
    89.         // Make sure we are limit player from doing a loop
    90.         float maxX = Quaternion.LookRotation(moveVector + dir).eulerAngles.x;
    91.  
    92.         // If has not going to far up/down, add the direction to the moveVector
    93.         if (maxX < 90 && maxX > 70 || maxX > 270 && maxX < 290)
    94.         {
    95.             // Mat kar
    96.         }
    97.         else
    98.         {
    99.             // Add the direction to the current move
    100.             moveVector += dir;
    101.             // Have the player face where he is goind
    102.             transform.localRotation = Quaternion.Euler(0f, 0f, InputX * 10f);
    103.             transform.rotation = Quaternion.LookRotation(moveVector);
    104.         }
    105.         // move him!
    106.         controller.Move(moveVector * Time.deltaTime);
    107.  
    108.  
    109.        
    110.     }//PlayerTranlationAndInput
    111.  
    112.     void StabilizeJet()
    113.     {
    114.         if (InputY == 0f)
    115.         {
    116.             Vector3 Test_Dir = new Vector3(transform.localPosition.normalized.x, 0, transform.localPosition.normalized.z);
    117.  
    118.             Quaternion lookRotation = Quaternion.LookRotation(Test_Dir);
    119.             float step = Restoration_Speed * Time.deltaTime;
    120.             transform.rotation = Quaternion.RotateTowards(transform.rotation, lookRotation, step);
    121.         }
    122.     }//StabilizeJet
    123.  
    124.     void SpeedControl()
    125.     {
    126.         {
    127.             if (Physics.Raycast(transform.position, Vector3.up * -1, out RaycastHit HitInfo, 1000f))
    128.             {
    129.                 Debug.DrawLine(transform.position, HitInfo.point, color: Color.green);
    130.                 if (HitInfo.distance < Speeding_Height && baseSpeed <= Max_Speed)
    131.                 {
    132.                     baseSpeed += Time.deltaTime * 10;
    133.                 }
    134.                 else
    135.                 {
    136.                     if (baseSpeed >= Min_Speed)
    137.                         baseSpeed -= Time.deltaTime * 5;
    138.                 }
    139.             }
    140.         }
    141.  
    142.     }//SpeedControl
    143.  
    144.  
    145.     void FakeTilt()
    146.     {
    147.         Vector3 newRotation = Flight_Model.transform.localEulerAngles;
    148.         newRotation.z = Time.fixedDeltaTime * InputX * -1000f * Tilt_Sensitivity ;
    149.         Flight_Model.transform.localEulerAngles = newRotation;
    150.  
    151.     }
    152. }//Main Class
    153.  
    154.  
     
    Last edited: Dec 22, 2019
unityunity