Im working on my first game that i need dodge obstacles with my car and i want to block the way out of the road so i wanted to limit his X position to make he only possoble of moving between -2.8 and 2.8.
here is my movement code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class player : MonoBehaviour
{
public float velocidade;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
movimentação();
}
void movimentação()
{
if (Input.GetKey(KeyCode.D))
{
transform.Translate(velocidade * Time.deltaTime, 0, 0);
}
if (Input.GetKey(KeyCode.A))
{
transform.Translate(-velocidade * Time.deltaTime, 0, 0);
}
if (Input.GetKey(KeyCode.LeftArrow))
{
transform.Translate(-velocidade * Time.deltaTime, 0, 0);
}
if (Input.GetKey(KeyCode.RightArrow))
{
transform.Translate(velocidade * Time.deltaTime, 0, 0);
}
}
void OnTriggerEnter2D(){
Debug.Log ("Bateu");
}
}