I want to make the player move in these directions:
(0,0,50) & (0,0,-50)
so that when the character is moving forward in one of these directions with every players tap on the screen the character switches between these directions and i don’t know how to write these scripts , can anyone please help?
i’ve got this so far but it doesn’t work.
public class bikeController : MonoBehaviour
{
public float speed = 30f;
bool bikePosition = true;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void FixedUpdate()
{
bool bikeChangingPosition = Input.GetButton("Fire1");
if (bikeChangingPosition && bikePosition == true)
{
transform.rotation = Quaternion.Euler(0, 0, 290);
rigidbody.velocity = new Vector3(6, 3, 290);
bikePosition = false;
}
if(bikeChangingPosition && bikePosition == false)
{
transform.rotation = Quaternion.Euler(0, 0, 70);
rigidbody.velocity = new Vector3(3, 6, 70);
bikePosition = true;
}
}
The problem is Input.GetButton(“Fire1”) only sends true when the buttons is held. You want it to toggle, so you’ll need to move bool bikeChangingPosition out of the function and toggle it when Input.GetButtonDown(“Fire1”) is pressed. Then you can adjust the velocity depending which way it’s leaning.
public class bikeController : MonoBehaviour
{
public float speed = 1f;
bool bikeChangingPosition; // name isn't quite accurate
// Update is called once per frame
void Update()
{
GetComponent<Rigidbody>().velocity = new Vector3(0, 0, 290f * speed);
if (Input.GetButtonDown("Fire1"))
{
bikeChangingPosition = !bikeChangingPosition; // toggle left/right lean
}
if (bikeChangingPosition)
{
// leaning right
transform.rotation = Quaternion.Euler(0, 0, 290);
GetComponent<Rigidbody>().velocity += new Vector3(50f * speed, 0f, 0f);
}
else
{
// leaning left
transform.rotation = Quaternion.Euler(0, 0, 70);
GetComponent<Rigidbody>().velocity += new Vector3(-50f * speed, 0f, 0f);
}
}
}
I agree that reading the documentation is a must. But I still think, telling someone that this is easy to fix and to go watch videos does not answer the question. Since this was more of a code logic question than a Unity specific question, there’s no documentation that would flat out tell you how to fix this. If this answers your question, then please mark it as answered.