Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by hahahaha · Jun 17, 2014 at 04:57 PM · 2drotationmovementvector

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jeff-Kesselman · Jun 17, 2014 at 05:00 PM 0
Share

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?

4 Replies

· Add your reply
  • Sort: 
avatar image
1

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")

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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 :)

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tanshaydar · Jun 17, 2014 at 10:59 PM 0
Share

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.

avatar image hahahaha · Jun 18, 2014 at 10:43 AM 0
Share

Thanks! I will bear that in $$anonymous$$d :)

avatar image
0

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);
     }
 }

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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);
         }
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Rotate/Flip Image in Unity 2d using Virtual Joystick 1 Answer

Unity 2D: How can i get my sprite to rotate to the direction I am going? 1 Answer

Player rotation affecting Top Down 2D Movement 1 Answer

2D Zig-zag movement and rotation 1 Answer

Locking move direction and rotation in JavaScript 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges