Search Unity

how do I "Tilt" this while moving

Discussion in 'Scripting' started by simplejuan17, Nov 23, 2017.

  1. simplejuan17

    simplejuan17

    Joined:
    Oct 6, 2017
    Posts:
    3

    While moving in "Horizontal" and "Strafe"

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class HoverMotor2 : MonoBehaviour {
    6.  
    7.     public float speed = 90f;
    8.     public float strafeSpeed = 90f;
    9.     public float turnSpeed = 5f;
    10.     public float hoverForce = 65f;
    11.     public float hoverHeight = 3.5f;
    12.     private float powerInput;
    13.     private float turnInput;
    14.     private float strafeInput;
    15.     private Rigidbody carRigidbody;
    16.  
    17.  
    18.     void Awake ()
    19.     {
    20.         carRigidbody = GetComponent <Rigidbody>();
    21.     }
    22.  
    23.     void Update ()
    24.     {
    25.         powerInput = Input.GetAxis ("Vertical");
    26.         turnInput = Input.GetAxis ("Horizontal");
    27.         strafeInput = Input.GetAxis ("Strafe");
    28.  
    29.         if (Input.GetButtonDown ("jump")) {
    30.             Debug.Log ("Space is pressed");
    31.         }
    32.  
    33.     }
    34.  
    35.  
    36.     void FixedUpdate()
    37.     {
    38.         Ray ray = new Ray (transform.position, -transform.up);
    39.         RaycastHit hit;
    40.  
    41.         if (Physics.Raycast(ray, out hit, hoverHeight))
    42.         {
    43.             float proportionalHeight = (hoverHeight - hit.distance) / hoverHeight;
    44.             Vector3 appliedHoverForce = Vector3.up * proportionalHeight * hoverForce;
    45.             carRigidbody.AddForce(appliedHoverForce, ForceMode.Acceleration);
    46.         }
    47.  
    48.         carRigidbody.AddRelativeForce(strafeInput * strafeSpeed, 0f, powerInput * speed);
    49.         carRigidbody.AddRelativeTorque(0f, turnInput * turnSpeed, 0f);
    50.  
    51.     }
    52.        
    53. }
    54.  
     
  2. javaBroker

    javaBroker

    Joined:
    Jun 14, 2017
    Posts:
    11