Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 防止玩家穿墙_C#_Unity3d - Fatal编程技术网

C# 防止玩家穿墙

C# 防止玩家穿墙,c#,unity3d,C#,Unity3d,我想问一个问题。首先,我已经可以在玩家撞墙时检测到玩家,但我无法让玩家不穿墙。当玩家撞到墙上时,我如何让玩家停止移动 这是屏幕截图(红色胶囊是播放器): 胶囊与棕色墙碰撞的第一个图像是从3ds max导入的对象,我已经通过勾选复选框应用了碰撞器,如上图所示 以下是我正在使用的代码: using UnityEngine; using System.Collections; [RequireComponent(typeof(CharacterController))] public class

我想问一个问题。首先,我已经可以在玩家撞墙时检测到玩家,但我无法让玩家不穿墙。当玩家撞到墙上时,我如何让玩家停止移动

这是屏幕截图(红色胶囊是播放器):

胶囊与棕色墙碰撞的第一个图像是从3ds max导入的对象,我已经通过勾选复选框应用了碰撞器,如上图所示

以下是我正在使用的代码:

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);
        }
    }
}
任何帮助都将不胜感激


谢谢

为什么要对墙使用触发器?墙应该有Collison,只需使用Collison网格即可


在旁注中,您可以记录玩家在碰撞点的位置,将移动速度设置为零,然后重置其位置

将移动速度降低到0?这可能会有所帮助。1-更改为碰撞器而不是触发器(请参阅文档)。2-增加力量,而不是改变位置(另请参阅文档)。你的玩家是否有一个刚体连接?@Reniuz:不,这没有帮助,先生,当我将速度更改为0时。它仍然会通过wall@Catwood:是的,我已经将刚体连接到玩家,我取消了“使用重力”复选框,并且是运动学的。因为当我检查使用重力时,它会从地板上掉下来。是的,我错了,先生,现在我已经取消了对墙壁的触发器的检查,一切都正常,当我使用W,A,S,D移动时,玩家会朝着这个方向移动,但它会浮动(就像忽略重力一样),但是当我使用以下代码时:
int xVelocity=0,zVelocity=0;if(Input.GetKey(KeyCode.W)){xVelocity-=5;}else if(Input.GetKey(KeyCode.A)){zVelocity-=5;}else if(Input.GetKey(KeyCode.S)){xVelocity+=5;}else if(Input.GetKey(KeyCode.D)){zVelocity+=5;}刚体.velocity=new Vector3(xVelocity,0,zVelocity),播放器开始奇怪地旋转,但它正在移动