- Home /
The question is answered, right answer was accepted
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> ();
}
}
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.
Follow this Question
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