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

Make a key get pressed all the time

Discussion in 'Scripting' started by iPwned, Aug 2, 2016.

  1. iPwned

    iPwned

    Joined:
    Jul 30, 2016
    Posts:
    6
    As the title says, I want to script to make a key get pressed all the time.

    For example if I want to get the player to go to the right all the time, I'd like to get the right arrow key pressed all the time, how can I do this?
     
  2. Diaonic

    Diaonic

    Joined:
    Jun 21, 2016
    Posts:
    56
    Something like this should work, you don't need to continually have the right arrow pressed to have the character constantly moving right. Uncomment the moveDir inputs below if you want WASD style movement.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Character : MonoBehaviour {
    5.  
    6.     public int speed = 5;
    7.     // Use this for initialization
    8.     void Start () {
    9.  
    10.     }
    11.  
    12.     // Update is called once per frame
    13.     void Update () {
    14.         characterMovement();
    15.     }
    16.  
    17.     void characterMovement()
    18.     {
    19.         //put your player movement code here
    20.         Vector3 moveDir = Vector3.right;
    21.        // moveDir.x = Input.GetAxis("Horizontal"); // get result of AD keys in X
    22.         //moveDir.z = Input.GetAxis("Vertical"); // get result of WS keys in Z
    23.         // move this object at frame rate independent speed:
    24.         transform.position += moveDir * speed * Time.deltaTime;
    25.     }
    26. }