Question by
maxhoines · Jan 25, 2018 at 09:09 AM ·
scripting problemscripting beginnerbool
Toggle direction
I'm making a "OneButtonGame" where i have a car that is moving forward, when pressing spacebar i want the car to move right, the next time i press spacebar it turns left, the problem im having is that the car uses both parts of the "turning script" at the same time.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class CharacterRotation : MonoBehaviour { public float turnspeed; bool direction = true;
void Start()
{
turnspeed = turnspeed * Time.deltaTime;
}
void Update()
{
bool left = direction;
bool right = !direction;
left = Input.GetKey(KeyCode.Space);
right = Input.GetKey(KeyCode.Space);
if (right)
{
transform.Rotate(turnspeed * Vector2.up);
Debug.Log("right");
}
else if (left)
{
transform.Rotate(turnspeed * Vector2.down);
Debug.Log("left");
}
if (Input.GetKeyDown(KeyCode.Space))
{
direction = !direction;
}
}
}
Comment