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;
}
}
1 Answer
1
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.
This is quite easy to achieve, but this site is not a homework service. You need to start learning the basics yourself. I highly recommend you watch as many of the tutorials as possible. http://unity3d.com/learn/tutorials
– ijidauMoved this to the HelpRoom area, which is a fine place for it. The main site is for things you can't look up or learn from the Docs. For new users, with no game dev experience, it's difficult to know the difference.
– Owen-Reynolds