- Home /
Controlling 2 players with horizontal and vertical axis
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. Here is the code and a screen shot of the axis settings:! There isn't any difference between the two except the previously mentioned "horizontal2" and "Vertical2" [
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;
}
}
}
// this is a really simple approach to updating wheels
// here we simulate a rear wheel drive car and assume that the car is perfectly symmetric at local zero
// this helps us to figure our which wheels are front ones and which are rear
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)
{
// a simple car where front wheels steer while rear ones drive
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;
}
}
}][1]
[1]: /storage/temp/184389-untitled.png
Your answer
Follow this Question
Related Questions
Moving a cube on the X-axis only moves by 1 unit? 1 Answer
Why is my character rotating without input? 1 Answer
How do I activate Input.GetAxis (Vertical) on an iMac? 1 Answer
moving player with ui buttons 1 Answer
How to make Input.GetAxis(“Mouse X”) get a value even when the mouse is stopped? 1 Answer