Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Jul 24, 2017 at 09:39 PM by nintendo_kid for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by nintendo_kid · Jul 24, 2017 at 05:25 PM · movement script

Movement Scripts interfering with each other

Hey, I'm trying to make a game similar to tag. Right now I am stuck with this one problem that basically causes only one GameObject to move utilizing both WASD and Arrow Keys, the idea being that two separate objects should receive the scripts individually and not interfere with each other. Here is the code for the WASD controls. (PS I'm very new to Unity in general)

 using UnityEngine;
 
 public class WASDControls : MonoBehaviour {
 
     public float speed = 10f;
 
     Vector3 movement;
 
     void FixedUpdate () {
         WASDControl ();
     }
 
     void WASDControl() 
     {
         if (Input.GetKeyDown (KeyCode.W)) 
         {
             movement = Vector3.forward;
             movement = movement.normalized * speed * Time.deltaTime;
         } 
         if (Input.GetKeyDown (KeyCode.S)) 
         {
             movement = Vector3.back;
             movement = movement.normalized * speed * Time.deltaTime;
         }
         if (Input.GetKeyDown (KeyCode.A)) 
         {
             movement = Vector3.left;
             movement = movement.normalized * speed * Time.deltaTime;
         }
         if (Input.GetKeyDown (KeyCode.D)) 
         {
             movement = Vector3.right;
             movement = movement.normalized * speed * Time.deltaTime;
         }
     }
 }

You may notice that for the WASD controls, don't have any code modifying the position of the gameObject, and that's because for some reason, It still moves without it.

And here is the code for the Arrow Controls

 using UnityEngine;
 
 public class ArrowControls : MonoBehaviour {
 
     public float speed = 10f;
 
     Vector3 movement;
     Rigidbody rb;
 
     void Start () 
     {
         rb = GetComponent<Rigidbody> ();
     }
 
     void FixedUpdate () 
     {
         float h = Input.GetAxisRaw ("Horizontal");
         float v = Input.GetAxisRaw ("Vertical");
         Move (h, v);
     }
 
     void Move (float h, float v) 
     {
         movement.Set (h, 0f, v);
 
         movement = movement.normalized * speed * Time.deltaTime;
 
         rb.MovePosition (transform.localPosition + movement);
     }
 }
 

And lastly, here is the code responsible for passing the above scripts to the GameObjects.

 using UnityEngine;
 
 public class PlayerManager : MonoBehaviour {
 
     public static PlayerManager instance;
 
     public GameObject[] players;
     private int playerID = 0;
 
     void Start () 
     {
         instance = this;
         Invoke ("InitializePlayers", 0.0f);
     }
 
     void InitializePlayers () 
     {
         players = GameObject.FindGameObjectsWithTag ("Player");
         foreach (GameObject player in players) 
         {
             MeshRenderer playerMesh = player.GetComponent<MeshRenderer> ();
             playerMesh.material.color = Random.ColorHSV();
             player.tag = "Player" + playerID.ToString();
             playerID++;
         }
 
         players [0].AddComponent<ArrowControls> ();
         players [1].AddComponent<WASDControls> ();
     }
 }

Comment
Add comment
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

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by MattG54321 · Jul 24, 2017 at 07:06 PM

This is because WASD also controls your Horizontal and Vertical input axes.

You need to go to your Input Manager (Edit->Project Settings->Input) and clear out the Alt Positive Button and Alt Negative Button fields from your Horizontal and Vertical input axes.

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

Follow this Question

Answers Answers and Comments

69 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

moving cubes consistantly random 1 Answer

Manual Player Physics Help (C#) 0 Answers

My dragging script "Sucks" all the other objects when touching it 1 Answer

Move Object with pivot 0 Answers

Struggling to smoothly disable diagonal movement 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