Search Unity

If I press SHIFT "speed" increases

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

  1. simplejuan17

    simplejuan17

    Joined:
    Oct 6, 2017
    Posts:
    3
    im still studying coding but i want to actually "see" my codes "work" so please help me

    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.     }
    30.  
    31.  
    32.     void FixedUpdate()
    33.     {
    34.         Ray ray = new Ray (transform.position, -transform.up);
    35.         RaycastHit hit;
    36.  
    37.         if (Physics.Raycast(ray, out hit, hoverHeight))
    38.         {
    39.             float proportionalHeight = (hoverHeight - hit.distance) / hoverHeight;
    40.             Vector3 appliedHoverForce = Vector3.up * proportionalHeight * hoverForce;
    41.             carRigidbody.AddForce(appliedHoverForce, ForceMode.Acceleration);
    42.         }
    43.  
    44.         carRigidbody.AddRelativeForce(strafeInput * strafeSpeed, 0f, powerInput * speed);
    45.         carRigidbody.AddRelativeTorque(0f, turnInput * turnSpeed, 0f);
    46.  
    47.     }
    48. }
    49.  
    50.  
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,281
    Take a look through our learn section for lots of examples https://unity3d.com/learn
    If you want to increase your speed with shift then you want to use Input to detect the shift key(like you do with the axis but use GetKey instead) and then apply the boost to your speed value.
    Ideally you want to allow for key remapping so create a new key mapping for the speed boost item with shift as the default and then use GetButton for it.
     
    Last edited: Nov 22, 2017