1
votes

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):

enter image description here

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

1
Drop move speed to 0?Renatas M.
A couple of things that might help. 1- change to colliders rather than triggers (see documentation). 2- Add force instead of changing the position (also see documentation).Catwood
Also does your player have a rigid-body attached?Catwood
@Reniuz: No, it does not help sir, when I change the speed to 0. It still goes through the wallUnknown User
@Catwood: Yeah, I already attached the rigidbody to the player and I uncheck the use gravity and is kinematic. Because when I check the use gravity, it fall down from the floor.Unknown User

1 Answers

0
votes

Why are you using triggers for walls? Walls are meant to have colliison, just use a colliison mesh.

On a side note, you can just log the position of the player at the point of collision, set the movement-speed to zero, then reset his position.