- Home /
Need Some Help With Controlls.
Im trying to have 2 characters operated by different keys. One uses WSAD and one uses up down left and right to move. Im using a script that I found and followed a tutorial on that allows my characters to shoot in the direction they are looking but they both use WSAD and arrow keys to move. I need some help making one script have arrow key movement and one having WSAD while retaining the shooting mechanic i mentioned. Heres the script for the movement. Im using a controller so the characters dont spaz out when they get close to a wall. Seems like the only fix unless there is another.
{
public CharacterController controller;
public float speed = 6f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//float x = Input.GetAxis("Horizontal");
//float z = Input.GetAxis("Vertical");
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical). normalized;
//Vector3 move = transform.right * x + transform.forward * z;
//controller.Move(move * speed * Time.deltaTime);
if (direction.magnitude >= 0.1f)
{
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, targetAngle, 0f);
controller.Move(direction * speed * Time.deltaTime);
}
}
}
Answer by ReneSchleese · Jul 03, 2020 at 10:05 AM
Instead of Input.GetAxisRaw(), calculate your horziontal/vertical values by using Input.GetKey(). Input.GetAxisRaw() answers the question of: "Is there any input right now that can be mapped to an axis?". Both WASD as well as arrow keys will answer that question with true. Input.GetKey() answers the question of: "Is a specific key pressed right now?".
Untested example:
// Input.GetKey() returns a bool. Use System.Convert.ToSingle() to convert it to a float
float p1Left = Convert.ToSingle(Input.GetKey(KeyCode.A));
float p1Right = Convert.ToSingle(Input.GetKey(KeyCode.D));
float p1Horizontal = -p1Left + p1Right;
Use the same logic to get p1Vertical, and then repeat the same for p2