- Home /
Basic 2D movement C# - Key presses cancel eachother out
Hi there!
Very new to Unity and C# coding.
I have managed to make a rigidbody2D object rotate AND move forward and back in relation to it's own rotation.
However, while holding "W" to go forward, pressing "D" to turn right stops my forward motion and rotates me instead. Pressing "W" again while holding "D" stops my rotation and makes me go forward, etc..
I want to be able to press both forward and left at the same time and move around properly.
Here is the code I am using:
using UnityEngine;
using System.Collections;
public class PlayerMoveScript : MonoBehaviour {
public float moveSpeed = 200;
public float turnSpeed = 1000;
void Update()
{
if (Input.GetKey(KeyCode.W)) {
rigidbody2D.AddForce(transform.up * moveSpeed);
}
if (Input.GetKey(KeyCode.S)) {
rigidbody2D.AddForce(transform.up * -moveSpeed);
}
if (Input.GetKey(KeyCode.A)) {
transform.Rotate(Vector3.forward * -turnSpeed);
}
if (Input.GetKey(KeyCode.D)) {
transform.Rotate(Vector3.forward * turnSpeed);
}
}
}
Any ideas very much appreciated!
Thank you :) Sam
I don't see anything in this code that would stop your motion.
Are you sure this is the only code that is responding to key presses in your app?
Answer by Tanshaydar · Jun 17, 2014 at 05:53 PM
That's why, instead of KeyCodes, you use Input.GetAxis("Horizontal")
and Input.GetAxis("Vertical")
Answer by hahahaha · Jun 17, 2014 at 10:57 PM
Hey all!
I believe the issue was actually the keyboard I was using.
I was writing this code from my Mac at work, using remote connect to my PC at home, in which I encountered this issue.
However, when I got home and tried it and it worked!
I feel like such a noob now.
Thanks for your replies though :)
I'm glad you get it working, but still, using axes ins$$anonymous$$d of keycodes for horizontal and vertical movement is still a better option.
Answer by $$anonymous$$ · Jul 19, 2018 at 12:03 PM
using UnityEngine;
public class PLAYER : MonoBehaviour {
public float moveSpeed = 200f;
private Rigidbody2D rb; // you need to have a rigidbody2d
public void Start() {
rb = GetComponent<Rigidbody2D>(); // then you need to get the component
}
public void Update() // finally, you can use your rb as rigidbody2d
{
if (Input.GetKey(KeyCode.W))
{
rb.AddForce(transform.up * moveSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.S))
{
rb.AddForce(transform.up * -moveSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.D))
{
rb.AddForce(transform.right * moveSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.A))
{
rb.AddForce(transform.right * -moveSpeed * Time.deltaTime);
}
}
}
Answer by madks13 · Jul 19, 2018 at 01:22 PM
I thinks this would be the most standard answer, it's based on the PlayerController from Unity :
//This makes it impossible to add to an object without a RigidBody
[RequireComponent(typeof(Rigidbody))]
public class PlayerMoveScript : MonoBehaviour
{
private Rigidbody _rigidBody;
private float moveSpeed = 200.0f;
private float rotateSpeed = 1000.0f;
private void Awake()
{
//Since rigidbody is required, we can get the component without any checks
_rigidBody = GetComponent<Rigidbody>();
}
private void Update()
{
var speed = Vector3.zero;
var rotation = Vector3.zero;
//The axis names need to be from the defined axis in the game settings
//The values of each axe goes from -1 to 1
//Using AddForce will add force to ludicrous speeds
//Using velocity will limit the speed to moveSpeed
//Movement forward/back
speed.z = Input.GetAxis("FB") * moveSpeed * Time.deltaTime;
//Movement left/right
speed.x = Input.GetAxis("LR") * moveSpeed * Time.deltaTime;
_rigidBody.velocity = speed;
rotation.y = Input.GetAxis("Rotation") * rotateSpeed * Time.deltaTime;
transform.Rotate(rotation);
}
}