- Home /
Prevent player from goes through the wall
Hi guys, 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
Uncheck the 'generate colliders' box and add a box collider to the wall gameobject. You also need to have a collider in the player gameobject to collide with the wall.
I already uncheck the generate colliders, but when I uncheck it, the debug did not run and also still goes through the wall. And also before I ask this question on this forum, I already have a collider for each of the walls and the player gameobject
Answer by HarshadK · Nov 27, 2014 at 02:17 PM
The option below is based on the fact that you are using OnTriggerEnter to check for collision in your script above.
Your Wall colliders are set to be Trigger. Try to uncheck the is Trigger from your collider on your wall.
No, when I uncheck the isTrigger, the function OnTriggerEnter did not run
You need to replace the OnTriggerEnter function with OnCollisionEnter(). Something like:
void OnCollisionEnter(Collision col) {
When I replace the OnTriggerEnter
to OnCollisionEnter
, the Debug.Log
inside that function did not run sir.
Can you post a screenshot for inspector settings of both the objects?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
After collision how the run animation to die animation will change.. 0 Answers
Weird NullReferenceException 1 Answer
color in textmesh 1 Answer