为什么即使我给它添加了一个盒子对撞机,机器人也会继续穿过门和其他物体?

Why the droid keep moving through the door and other objects even if i add a box collider to it?

机器人有一个盒子对撞机,门也有一个盒子对撞机。 但是当我移动角色(FPSController/FirstPersoncharacter)时,角色停止的玩家不能穿过门,但机器人可以。

我试着在 droid box collider 上打开 off/on Is Trigger 属性 但它没有改变。

我希望机器人在与其他物体碰撞时表现得像玩家一样,例如机器人是玩家的一部分。

我使用的唯一代码附加到 FirstPersonCharacter:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DroidMove : MonoBehaviour
{
    public GameObject droid;

    private float distance;
    private Camera cam;

    private void Start()
    {
        cam = GetComponent<Camera>();
        distance = Vector3.Distance(cam.transform.position, droid.transform.position);
        droid.SetActive(false);
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.F))
        {
            droid.SetActive(!droid.activeInHierarchy);
        }
    }
}

我试图将一个 Rigidbody 组件添加到机器人的 NAVI 或 Droid_Player 但它没有解决它。我没有任何其他处理碰撞的代码。

更新我到目前为止所做的尝试:

我从 NAVI (Droid) 中删除了盒子碰撞器组件并添加了两个东西:

  1. 角色控制器组件

  2. 控制脚本(此脚本随NAVI(Droid)一起提供)

这是控制脚本:

using UnityEngine;
using System.Collections;

public class Control : MonoBehaviour 
{
    public float rotationDamping = 20f;
    public float speed = 10f;
    public int gravity = 0;
    public Animator animator;

    float verticalVel;  // Used for continuing momentum while in air    
    CharacterController controller;

    void Start()
    {
        controller = (CharacterController)GetComponent(typeof(CharacterController));
    }

    float UpdateMovement()
    {
        // Movement
        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");

        Vector3 inputVec = new Vector3(x, 0, z);
        inputVec *= speed;

        controller.Move((inputVec + Vector3.up * -gravity + new Vector3(0, verticalVel, 0)) * Time.deltaTime);

        // Rotation
        if (inputVec != Vector3.zero)
            transform.rotation = Quaternion.Slerp(transform.rotation, 
                                                  Quaternion.LookRotation(inputVec), 
                                                  Time.deltaTime * rotationDamping);

        return inputVec.magnitude;
    }

    void AnimationControl ()
    {
        if(Input.GetKey("a") || Input.GetKey("s") || Input.GetKey("d") || Input.GetKey("w")) 
        {
            animator.SetBool ("Moving" , true);
        }
        else
        {
            animator.SetBool ("Moving" , false);
        }

        if(Input.GetKey("space")) 
        {
            animator.SetBool ("Angry" , true);
        }
        else
        {
            animator.SetBool ("Angry" , false);
        }

        if(Input.GetKey("mouse 0")) 
        {
            animator.SetBool ("Scared" , true);
        }
        else
        {
            animator.SetBool ("Scared" , false);
        }

    }

    void Update()
    {   
        UpdateMovement();
        AnimationControl();


        if ( controller.isGrounded )
            verticalVel = 0f;// Remove any persistent velocity after landing
    }
}

现在,当我将角色移到门或墙附近时,NAVI(Droid) 也会停止并且不会穿过门或墙,这很好。但现在我有另一个问题。如果我继续移动到门或墙上,看起来角色正在移动 over/on NAVI Droid。

Navi 机器人没有改变位置。

在这张截图中,当我靠近门时,门上的对撞机和 NAVI Droid 正在工作,而 navi droid 无法穿过门:

在此屏幕截图中,您可以看到当我不断将角色移到门口时发生了什么。在顶部屏幕视图中,NAVI 机器人处于相同位置,但在底部的游戏视图中,角色似乎正在移动 over/on NAVI 机器人。

如果我继续向门口移动,角色将不会移动,但机器人看起来像是被推回,或者角色会继续在机器人上方移动。

这是我录制的展示问题的短片。

问题从第 20 秒开始:

Collider problem

我试图将盒子碰撞器添加到 droid 中,尝试过刚体,但仅此脚本和组件没有任何效果,但现在我在视频中遇到了这个问题。

看起来你的机器人可能需要一个 RigidBody 组件。

如果这不能解决问题,您应该在您的问题中添加一些相关的代码片段以显示how/where您已尝试检测和处理碰撞。