Currently working on a 2D platformer. I am using a script i found online and it seems to work how i want it to for player controls. I would like to add a second control just on my key board.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WASDmove : MonoBehaviour
{
public float speed;
private float Move;
public float jump;
public bool isJumping;
private Rigidbody2D rb;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Udate is called once per frame
void Update()
{
Move = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(speed * Move, rb.velocity.y);
if(Input.GetButtonDown("Jump") && isJumping == false)
{
rb.AddForce(new Vector2(rb.velocity.x, jump));
Debug.Log("Jump");
}
}
private void OnCollisionEnter2D(Collision2D other)
{
if(other.gameObject.CompareTag("Ground"))
{
isJumping = false;
}
}
private void OnCollisionExit2D(Collision2D other)
{
if (other.gameObject.CompareTag("Ground"))
{
isJumping = true;
}
}
}
The way this code works seems to be good for my game but is there a better way to have just KeyCode
instead?
There are two ways to do this. One of them is to use the statement if (Input.GetKey(KeyCode.W)) *code here*
, and repeat that for all controls. Then, create a second script, attach to player two, and use the code if (Input.GetKey(KeyCode.UpArrow)) *code here*
and repeat that for all controls(This is the less code concise but simpler way, and the one I would not recommend using, as it would require setting setting velocities for each individual input, instead of getting to use the clean Input.GetAxis function).
The other way to do this is to go to Edit
→ Project Settings
→ Input Manager
and then go to axes. Change the horizontal and vertical axes to not include arrow keys, then change the name of “Horizontal” and “Vertical” to whatever you want, such as “HorizontalP1” and “VerticalP1”. Then, change the size of the axes to 32, and rename the two new Axes to whatever you want such as “HorizontalP2” and “VerticalP2”, and change their inputs to only include arrow keys. Then, create two scripts, one for P1, and P2, and reference these axes in movement code. If you want to be extra efficient with your scripts, instead of creating 1 for each player, create 1, and then create a public variable which determines if it is for player 1 controlling it or player 2, and then depending on which one it is, it uses a different axis for control/ uses a different KeyCode system.
I may be misinterpreting the question, but I believe what you are asking is to use physical keycodes for your controls, you can use the line of code, Input.GetKey("KEYCODE")
, or you can go into into Edit
→ Project Settings
→ Input Manager
and then change the axes settings to be the keys that you want. You can also click the box that says Use Physical Keys ☑
, which also may be what you want, which maps every key on the keyboard to a location and lets you reference each location by the key name, if that makes sense.
i guess i am looking for the raw input code?
the presets there allow me to do wasd and the arrows.
basically how can i do it so player 1 uses wasd and player 2 uses arrows.