- Home /
Question by
GameCreator85 · Sep 08, 2020 at 08:52 PM ·
c#character controller
Moving character along the global axis
I want my character to move along the global axis by simply pressing 'A' to go on the global x-axis and such, and I received code on how to do that. But now I have to tap the button to move the character instead of holding it, which I want to do. Here is my code, thanks:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class controller : MonoBehaviour
{
public float speed = 2f;
void Update(){
Vector3 movement = Vector3.zero;
//Get your movement direction based on key press
if(Input.GetKeyDown(KeyCode.A)){
movement = -1f *Vector3.right;
} else if(Input.GetKeyDown(KeyCode.D)){
movement = Vector3.right;
} else if (Input.GetKeyDown(KeyCode.W)){
movement = Vector3.forward; //Or Vector3.up
} else if(Input.GetKeyDown(KeyCode.S)){
movement = -1f * Vector3.forward; //Or Vector3.up
}
//Move your character position based on your movement direction and your desired movement speed
transform.position = transform.position + (movement * speed * Time.deltaTime);
}
}
Comment
You are using 'Input.GetKeyDown'. Try using 'Input.GetKey' instead. Even better you could use this: Link
Thanks so much, that solved my problem, I can't believe I couldn't figure that out.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to move the character with the current? 0 Answers
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer