Hello!
So I have a cube that when I press “W”, it slides exactly +1 in the x position. It works fine right now, but when it runs into a wall, it goes in a little then starts to glitch and it can’t move. Is there any way I can fix this bug? Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour {
public float speed = 1;
private Vector3 target;
// Use this for initialization
void Start () {
target = transform.position;
}
void Update () {
if (Input.GetKeyDown(KeyCode.A)) {
target += Vector3.forward;
} else if (Input.GetKeyDown(KeyCode.D)) {
target += Vector3.back;
} else if (Input.GetKeyDown(KeyCode.W)) {
target += Vector3.right;
} else if (Input.GetKeyDown(KeyCode.S)) {
target += Vector3.left;
}
transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
}
}