How to change Steer angle on Horizantal axis to left/right for Touch screen
basically I'm adding touch screen support for my game and want the wheels on my car to turn based on which side of the screen is pressed, the issue im having is splitting the horizontal axis into left / right to correspond with which side of screen is pressed.
i tried changing float steer too - "float steer = Input.GetAxis("Horizontal") < 0;" to provide me a "Left" i could then translate into touch but you cant do that with a float.
Any help much appreciated thanks.
void FixedUpdate(){
if (Input.touchCount > 0)
{
var touch = Input.touches[0];
if (touch.position.x < Screen.width/2) // left side
{
float steer = Input.GetAxis("Horizontal"); // this needs to know to go in the left direction
float accelerate = Input.GetAxis ("Vertical");
float finalAngle = steer * 45f;
wheelColliders [0].steerAngle = finalAngle; // turns left wheel
wheelColliders [1].steerAngle = finalAngle; // turns right wheel
for (int i = 0; i < 4; i++) {
//wheelColliders[i].motorTorque = accelerate * maxTorque;
wheelColliders [i].motorTorque = maxTorque;
}
}
Since you already know whether you should turn them left or right ala :
if (touch.position.x < Screen.width/2) // left side
couldn't you simply just set the rotation to -45 or 45 accordingly ?
@vintar i'm not sure i understand, both wheels turn together (as its a car). i use the axis.horizontal to turn both wheels together when i press "a or d"(left or right).
if i was to set it to rotate to -45 or 45 it would instantly lock the wheel in that direction? where as currently it turns the wheels depending how long you press the button(atleast that's how it feels).
Your answer
![](https://koobas.hobune.stream/wayback/20220612055952im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
change gravity 1 Answer
Issue with loading scenes with buttons 1 Answer
What am I doing wrong? 0 Answers
Expanding onto the 2D RougeLike Game adding a main menu (URGENT! help required please) 1 Answer