简体   繁体   English

防止玩家穿过墙

[英]Prevent player from goes through the wall

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. 胶囊与棕色壁碰撞的第一张图像是从3ds max导入的对象,我已经通过勾选复选框来应用碰撞器,如上图所示。

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 谢谢

Why are you using triggers for walls? 为什么在墙壁上使用触发器? Walls are meant to have colliison, just use a colliison mesh. 墙壁本应具有colliison,只需使用colliison网格即可。

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. 另外,您可以记录玩家在碰撞点的位置,将移动速度设置为零,然后重置其位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM