Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Slowing moveSpeed when in air

Discussion in 'Editor & General Support' started by blastoii, Jan 22, 2015.

  1. blastoii

    blastoii

    Joined:
    Jan 7, 2015
    Posts:
    4
    The title says it. When jumping I want the player to slow down, because now he can jump to far. But if I lower the moveSpeed the player moves to slow. Please help.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerMovement : MonoBehaviour {
    5.  
    6.     public float moveSpeed;
    7.     public float turnSpeed;
    8.     public GameObject deathParticles;
    9.     public float jumpSpeed;
    10.      
    11.     //private float maxSpeed = 5f;
    12.     private Vector3 input;
    13.     private Vector3 spawn;
    14.     private bool isFalling = false;
    15.  
    16.     //Player position
    17.     void Start () {
    18.         spawn = transform.position;
    19.     }
    20.  
    21.     //Movement
    22.     void FixedUpdate () {
    23.  
    24.         float forwardMovement = Input.GetAxis("Vertical") * moveSpeed * Time.deltaTime;
    25.         float turnMovement = Input.GetAxis("Horizontal") * turnSpeed * Time.deltaTime;
    26.         //input = new Vector3(Input.GetAxisRaw("Horizontal"),0, Input.GetAxisRaw("Vertical"));
    27.  
    28.         transform.Translate(Vector3.forward * forwardMovement);
    29.         transform.Rotate(Vector3.up * turnMovement);
    30.  
    31.         /*if (rigidbody.velocity.magnitude < maxSpeed)
    32.         {          
    33.             rigidbody.AddRelativeForce(input * moveSpeed);
    34.         }*/
    35.  
    36.         if (Input.GetButtonDown("Jump") && isFalling == false)
    37.         {
    38.             rigidbody.velocity = new Vector3(0,jumpSpeed,0);
    39.             isFalling = true;
    40.         }
    41.  
    42.  
    43.  
    44.         if (transform.position.y < -2)
    45.         {
    46.             Die ();
    47.         }
    48.  
    49.     }
    50.     //Check if player is on the ground
    51.     void OnCollisionStay()
    52.     {
    53.         isFalling = false;
    54.  
    55.     }
    56.  
    57.     //Check for enemies
    58.     void OnCollisionEnter(Collision other)
    59.     {
    60.         if (other.transform.tag == "Enemy")
    61.         {
    62.             Die();
    63.         }
    64.     }
    65.  
    66.     void OnTriggerEnter(Collider other)
    67.     {
    68.         if (other.transform.tag == "Enemy")
    69.         {
    70.             Die();
    71.         }
    72.         if (other.transform.tag == "Goal")
    73.         {
    74.             GameManager.CompleteLevel();
    75.         }
    76.     }
    77.  
    78.     void Die()
    79.     {
    80.         transform.position = spawn;
    81.         Instantiate(deathParticles, transform.position, Quaternion.Euler(270,0,0));
    82.     }
    83. }
     
  2. FunghettoGio

    FunghettoGio

    Joined:
    Mar 13, 2014
    Posts:
    3
    You can create another variable called airMoveSpeed so you can use two separate speed values for when the player is on the ground or not.

    Code (csharp):
    1.  
    2. float forwardMovement;
    3. if (isfalling)
    4.   forwardMovement =Input.GetAxis("Vertical")* airMoveSpeed *Time.deltaTime;
    5. else
    6.   forwardMovement =Input.GetAxis("Vertical")* moveSpeed *Time.deltaTime;
    7.  
     
  3. blastoii

    blastoii

    Joined:
    Jan 7, 2015
    Posts:
    4
    Thanks for the reply, but it doesn't work
     
  4. FunghettoGio

    FunghettoGio

    Joined:
    Mar 13, 2014
    Posts:
    3