Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Condition on Character Movement

Discussion in 'Scripting' started by dis_pear, Apr 19, 2015.

  1. dis_pear

    dis_pear

    Joined:
    Mar 7, 2015
    Posts:
    8
    I copied and pasted the character movement script from one of the Unity sample projects. The code is below. How do I make it so this movement only occurs when the player is holding down a key/button?

    Thank you.

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class Char1MovementAlt : MonoBehaviour
    4. {
    5.    public float speed = 6f;  // The speed that the player will move at.
    6.    
    7.    Vector3 movement;  // The vector to store the direction of the player's movement.
    8.    Animator anim;  // Reference to the animator component.
    9.    Rigidbody playerRigidbody;  // Reference to the player's rigidbody.
    10.    int floorMask;  // A layer mask so that a ray can be cast just at gameobjects on the floor layer.
    11.    float camRayLength = 100f;  // The length of the ray from the camera into the scene.
    12.    
    13.    void Awake ()
    14.    {
    15.      // Create a layer mask for the floor layer.
    16.      floorMask = LayerMask.GetMask ("Floor");
    17.      
    18.      // Set up references.
    19.      anim = GetComponent <Animator> ();
    20.      playerRigidbody = GetComponent <Rigidbody> ();
    21.    }
    22.    
    23.    
    24.    void FixedUpdate ()
    25.    {
    26.      // Store the input axes.
    27.      float h = Input.GetAxisRaw ("Horizontal");
    28.      float v = Input.GetAxisRaw ("Vertical");
    29.      
    30.      // Move the player around the scene.
    31.      Move (h, v);
    32.    }
    33.    
    34.    void Move (float h, float v)
    35.    {
    36.      // Set the movement vector based on the axis input.
    37.      movement.Set (h, 0f, v);
    38.      
    39.      // Normalise the movement vector and make it proportional to the speed per second.
    40.      movement = movement.normalized * speed * Time.deltaTime;
    41.      
    42.      // Move the player to it's current position plus the movement.
    43.      playerRigidbody.MovePosition (transform.position + movement);
    44.    }
    45. }
     
  2. dis_pear

    dis_pear

    Joined:
    Mar 7, 2015
    Posts:
    8
  3. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Slightly confusing question -- the character should only move if you're pressing one of the buttons marked for your Horizontal or Vertical axes (eg. the arrow keys or WASD). Is that not what's happening?
     
  4. dis_pear

    dis_pear

    Joined:
    Mar 7, 2015
    Posts:
    8
    Thanks for the reply, sorry I wasn't clear. Here's a scenario to elaborate:


    Player moves thumbstick. Nothing happens.

    Player moves thumbstick WHILE ALSO holding down a button on the controller. Character moves right/left/up/down according to thumbstick input.
     
  5. Juice-Tin

    Juice-Tin

    Joined:
    Jul 22, 2012
    Posts:
    242
  6. dis_pear

    dis_pear

    Joined:
    Mar 7, 2015
    Posts:
    8
    That doesn't help, sadly. I know how to do one or the other but not both at once. How do I require that a button is being pressed before movement will work? I tried wrapping Move (h, v) in an If (Input.GetButton) but that always returns a syntax error.
     
  7. Juice-Tin

    Juice-Tin

    Joined:
    Jul 22, 2012
    Posts:
    242
    Check out the example they provide:
    Code (CSharp):
    1. if (Input.GetButton("Fire1") && Time.time > nextFire) {
    2.             nextFire = Time.time + fireRate;
    3.             GameObject clone = Instantiate(projectile, transform.position, transform.rotation) as GameObject;
    4.         }
    You can easily do something like
    Code (CSharp):
    1. if (Input.GetButton("Fire1")) {
    2.       move(h,v);
    3. }
    Just make sure that "Fire1" or whatever you use is defined in your input.

    If you just want to test it quickly or make sure it works without worrying about the input setup, you can use keyboard keys instead.
    Code (CSharp):
    1. if (Input.GetKey (KeyCode.Q)){
    2.     move(h,v);
    3. }