I want to ask a question. First of all, I already can detect the player while the player hit the wall, but I could not make the player not goes through the wall. How could I make the player to stop moving while the player hit the wall?
Here is the screenshot (Red capsule is the player):
The first image where the capsule is collided with the brown wall is the imported object from 3ds max and I already applied the collider by ticking the check box as shown on the above image.
Here is the code that I am using:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(CharacterController))]
public class CheckPlayer : MonoBehaviour
{
public float moveSpeed = 5.0f, rotationSpeed = 5.0f; // Define and set for the movement and rotation of the player
private void Update()
{
// Call the Movement and Rotation function
Movement();
Rotation();
}
private void OnTriggerEnter(Collider col)
{
// If the game object collided with the certain tag
if (col.gameObject.tag == "Inn's Objects")
{
// Debug it
Debug.Log("You have collided with the object");
}
// If the game object colided with the certain tag
if (col.gameObject.tag == "Inn's Door")
{
// Load another level
GameManager.LoadLevel("Third Loading Scene");
}
}
private void Movement()
{
// If user press W on the keyboard
if (Input.GetKey(KeyCode.W))
{
// Move the player forward
transform.position -= Vector3.forward * moveSpeed * Time.deltaTime;
}
// But if user press A on the keyboard
else if (Input.GetKey(KeyCode.A))
{
// Move the player to the right
transform.position += Vector3.right * moveSpeed * Time.deltaTime;
}
// But if user press S on the keyboard
else if (Input.GetKey(KeyCode.S))
{
// Move the player backward
transform.position += Vector3.forward * moveSpeed * Time.deltaTime;
}
// But if user press D on the keyboard
else if (Input.GetKey(KeyCode.D))
{
// Move the player to the left
transform.position -= Vector3.right * moveSpeed * Time.deltaTime;
}
}
private void Rotation()
{
// If user press E on the keyboard
if (Input.GetKey(KeyCode.E))
{
// Rotate the player to the right
transform.Rotate(-Vector3.up * rotationSpeed * Time.deltaTime);
}
// But if user press Q on the keyboard
else if (Input.GetKey(KeyCode.Q))
{
// Rotate the player to the left
transform.Rotate(Vector3.up * rotationSpeed * Time.deltaTime);
}
}
}
Any help would be much appreciated!
Thank you