I’m working on my first game ever (first of many :D). I’m targeting the android platform. I have managed to put together the basic mechanism of the game.
For the input I’m using a simple UI image as a button to rotate constantly, while holding the button, the player to left relatively to Space.World.
I used a Debug.Log and it seems to print my messages on the console but however it doesn’t change the status of a boolean variable in order to trigger the rotation.
Please help me I’m really stuck for more than two days now. I know it’s probably so easy but I’m a total rookie.
Here is my code:
Player Script:
using UnityEngine;
public class Player : MonoBehaviour
{
private float _turnSpeed = 150f;
private bool _turnLeft;
private void FixedUpdate()
{
TurnLeft(_turnLeft);
}
private void OnCollisionEnter()
{
Destroy(gameObject);
}
private void TurnLeft(bool turn)
{
if (turn)
{
transform.Rotate(-Vector3.up * _turnSpeed * Time.deltaTime, Space.World);
}
}
public void OnClicked()
{
_turnLeft = true;
}
public void OnReleased()
{
_turnLeft = false;
}
}
Button Script:
using UnityEngine;
using UnityEngine.EventSystems;
public class Button : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
[SerializeField] private Player _car;
public void OnPointerDown(PointerEventData eventData)
{
_car.OnClicked();
Debug.Log("I'm clicked");
}
public void OnPointerUp(PointerEventData eventData)
{
_car.OnReleased();
Debug.Log("I'm released");
}
}
Spawner Script:
using UnityEngine;
public class Spawner : MonoBehaviour
{
[SerializeField] private Player _car;
private Player _spawn;
private Vector3 _instantiatePosistion;
private Quaternion _instantiateRotation;
private void Start()
{
_instantiatePosistion = _car.transform.position;
_instantiateRotation = _car.transform.rotation;
}
private void Update()
{
if (_spawn != null) return;
_spawn = Instantiate(_car, _instantiatePosistion, _instantiateRotation);
}
}