Question by
fantekvelik · Aug 08, 2021 at 01:59 PM ·
input.getaxissplit-screen
Controlling 2 players using input.getaxis
I am making a game which has 2 players. Its a split-screen game. one player is controller using AWSD and the other using arrow keys. The code for moving the players(Car) is using Horizontal and Vertical Axis. I made 2 separate scripts for the two players. The only difference in the two scripts is that one uses "Horizontal" and "Vertical" while the other uses "horizontal2" and "Vertical2". I've set up the two axis so one uses AWSD and one arrow keys but it doesn't work. If this way of doing it is impossible I would appreciate a suggestion of how to implement this in other way. There isn't any difference between the two except the previously mentioned "horizontal2" and "Vertical2". Here is the code.
private WheelCollider[] wheels;
public Rigidbody target;
public float speed2 = 0.0f;
public float RoundedSpeed = 0.0f;
public float carMaxSpeed = 100.0f;
public float speed3;
public bool isBraking = false;
float range = 0;
public float force = 0;
public float maxAngle = 30;
public float maxTorque = 300;
public GameObject wheelShape;
public static RearWheelDrive cc;
// here we find all the WheelColliders down in the hierarchy
public void speed()
{
speed2 = target.velocity.magnitude * 3.6f;
speed3 = speed2 / 100 + 0.5f;
RoundedSpeed = Mathf.Round(speed2 * 100.0f) * 0.1f;
}
public void Start()
{
wheels = GetComponentsInChildren<WheelCollider>();
cc = this;
for (int i = 0; i < wheels.Length; ++i)
{
var wheel = wheels [i];
// create wheel shapes only when needed
if (wheelShape != null)
{
var ws = GameObject.Instantiate (wheelShape);
ws.transform.parent = wheel.transform;
}
}
}
public void Update()
{
speed();
float angle = maxAngle * Input.GetAxis("Horizontal");
float torque = maxTorque * Input.GetAxis("Vertical");
/* if (!Input.GetKey("w") && Input.GetKey("s"))
{
if (RoundedSpeed == 0f && torque == 0)
{
torque = 0f;
if (Input.GetKey("s"))
{
isBraking = true;
torque = -200;
}
}
else
{
isBraking = false;
}
} */
force = torque;
foreach (WheelCollider wheel in wheels)
{
if (wheel.transform.localPosition.z > 0)
wheel.steerAngle = angle;
if (wheel.transform.localPosition.z < 0)
wheel.motorTorque = torque;
if (wheel.transform.localPosition.z > 0)
wheel.motorTorque = torque;
// update visual wheels if any
if (wheelShape)
{
Quaternion q;
Vector3 p;
wheel.GetWorldPose (out p, out q);
// assume that the only child of the wheelcollider is the wheel shape
Transform shapeTransform = wheel.transform.GetChild (0);
shapeTransform.position = p;
shapeTransform.rotation = q;
}
}
}
Comment