- Home /
problem with mapping keys through c#
I am doing a continuation on Unity's tutorial Roll A Ball, for a programming class, in which I have two balls that need to be controlled through different key inputs (ideally one being controlled through the arrow keys and the other one controlled by the wasd keys). However, my instructor explicitly said to not use the input manager in Unity. I am at a loss and need help on what I should do.
This code controls a ball, but I need it to be controlled with the wasd keys:
public float speed;
public Text winText;
Vector3 startPos;
private Rigidbody rb;
void Start ()
{
rb = GetComponent<Rigidbody> ();
startPos = transform.position;
winText.text = "";
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
}
void OnTriggerEnter (Collider other)
{
if (other.gameObject.CompareTag ("Reset")) {
rb.velocity = new Vector3 (0f, 0f, 0f);
rb.angularVelocity = new Vector3(0f, 0f, 0f);
transform.rotation = Quaternion.Euler (new Vector3(0f, 0f, 0f));
transform.position = startPos;
}
if (other.gameObject.CompareTag ("Enemy")) {
rb.velocity = new Vector3 (0f, 0f, 0f);
rb.angularVelocity = new Vector3(0f, 0f, 0f);
transform.rotation = Quaternion.Euler (new Vector3(0f, 0f, 0f));
transform.position = startPos;
}
if (other.gameObject.CompareTag ("Goal")) {
winText.text = "Player 1 wins!";
gameObject.SetActive (false);
other.gameObject.SetActive (false);
}
}
}
Answer by OncaLupe · Oct 29, 2015 at 03:44 AM
This seems weird to me, since avoiding the input manager means you need to hard code inputs unless you make your own rebinding system in game. Hopefully your instructor is doing this for a reason, so I'll just go with it for now. The simplest way is to look for specific KeyCodes like this;
if(Input.GetKey(KeyCode.W))
{
//Key 'W' is being held down
}
Check here for more info:
http://docs.unity3d.com/ScriptReference/Input.GetKey.html http://docs.unity3d.com/ScriptReference/KeyCode.html
@OncaLupe Funny enough, I have been trying to do that before I posted this question. I have been experimenting with it, but I'm never clear as to what I should put between the curly brackets. Looking at pseudo code doesn't help me much.
@$$anonymous$$aniac567 Unfortunately if I answered that I'd basically be doing the project for you. Basically though what you'll want to do is use the AddForce method kind of like how the sample code you linked does, but the values put in will depend on which key is being pressed. For when 'W' is pressed you'd add Z force, 'S' is negative Z force, etc.
http://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
How to get Key with KeyCode.Question? 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
I want to play an Audio Sound for a character while running. 0 Answers