Hi there, I am new to Unity and I am learning it in College and have a project. I have added movement to the character and collision detection so the character knows when it has hit a wall. I am unsure of what to do now and I can not find much online that helps me. I want the character to stop at the wall and then move away when the player hits the movement keys. Any help or advice would be appreciated.
public class MoveChar : MonoBehaviour {
[SerializeField]
float moveSpeed = 4f;
public MoveChar movement;
Vector3 forward, right;
void Start () {
forward = Camera.main.transform.forward;
forward.y = 0;
forward = Vector3.Normalize(forward);
right = Quaternion.Euler(new Vector3(0, 90, 0)) * forward;
}
void FixedUpdate () {
if (Input.anyKey)
Move();
}
void Move()
{
Vector3 direction = new Vector3 (Input.GetAxis("HorizontalKey"), 0, Input.GetAxis("VerticalKey"));
Vector3 rightMovement = right * moveSpeed * Time.deltaTime * Input.GetAxis("HorizontalKey");
Vector3 upMovement = forward * moveSpeed * Time.deltaTime * Input.GetAxis("VerticalKey");
Vector3 heading = Vector3.Normalize(rightMovement + upMovement);
transform.forward = heading;
transform.position += rightMovement;
transform.position += upMovement;
}
void OnCollisionEnter (Collision collisionInfo)
{
if (collisionInfo.collider.tag == “Cube”)
{
Debug.Log(“Collision decected”);
}
}
``}
,I am new to Unity and I am struggling to add collision to my project. I have set it up so the character knows when it hits a wall but I am unsure of what to add to get the character to stop at the wall and move when you move away from it.
public class MoveChar : MonoBehaviour {
[SerializeField]
float moveSpeed = 4f;
public MoveChar movement;
Vector3 forward, right;
void Start () {
forward = Camera.main.transform.forward;
forward.y = 0;
forward = Vector3.Normalize(forward);
right = Quaternion.Euler(new Vector3(0, 90, 0)) * forward;
}
void FixedUpdate () {
if (Input.anyKey)
Move();
}
void Move()
{
Vector3 direction = new Vector3 (Input.GetAxis("HorizontalKey"), 0, Input.GetAxis("VerticalKey"));
Vector3 rightMovement = right * moveSpeed * Time.deltaTime * Input.GetAxis("HorizontalKey");
Vector3 upMovement = forward * moveSpeed * Time.deltaTime * Input.GetAxis("VerticalKey");
Vector3 heading = Vector3.Normalize(rightMovement + upMovement);
transform.forward = heading;
transform.position += rightMovement;
transform.position += upMovement;
}
void OnCollisionEnter (Collision collisionInfo)
{
if (collisionInfo.collider.tag == “Cube”)
{
Debug.Log(“Collision decected”);
}
}
}