【问题标题】:Unity collider prefab doesn't work on one sideUnity对撞机预制件在一侧不起作用
【发布时间】:2017-06-07 23:34:51
【问题描述】:

我有一个碰撞器预制件(一个立方体,带有一个立方体大小的盒子碰撞器)。在一个场景中,我的玩家与它完美碰撞,但在另一个场景中,玩家出现故障并通过碰撞器的一侧而不是另一侧。旋转它会改变它出现故障的一侧,因此它始终面向相同的全局方向。

这可能是什么原因造成的?我已经确保玩家和对撞机对象的预制件在场景中完全相同,除了玩家控制器脚本移动玩家并且具有将刚体速度设置为 0 的 OnCollisionExit 条件之外,没有任何影响碰撞。

添加了下面的代码。输入是操纵杆(想想直升机上的操纵杆)。就像我说的,碰撞通常很完美,但由于某种原因,只有在这个场景中它没有。我怀疑这是某种层次结构或刚体问题,但我似乎已经检查了所有内容。

https://github.com/ben-humphries/FRC-Driving-Simulation

using UnityEngine;
using UnityEngine.UI;

public class ChassisController : MonoBehaviour {

    public UIController uiController;

    public Canvas PauseCanvas;

    public float speedLinear = 10f;
    public float speedAngular = 100f;
    public float joyDeadZone = 0.5f;

    public float rotationOffset = 3f;

    public bool squaredMovement = false;

    public DriveModes driveMode = DriveModes.Tank;

    [HideInInspector]
    public bool paused = false;


    private Rigidbody rigidbody;

    Vector3 lastLinearPosition;
    float lastAngularPosition;



    void Start () {

        rigidbody = GetComponent<Rigidbody> ();

        lastLinearPosition = Vector3.zero;
        lastAngularPosition = 0f;
        paused = false;

    }

    void FixedUpdate () {

        if (!paused) {

            /*
         * INPUT
         */

            if (driveMode == DriveModes.Tank) {


                if (Mathf.Abs (Input.GetAxis ("VerticalLeft")) > joyDeadZone) {


                    Vector3 rotatePoint = (transform.position) + transform.TransformDirection (Vector3.right) * rotationOffset;
                    Vector3 rotateAxis = transform.TransformDirection (Vector3.up);

                    Debug.DrawRay (rotatePoint, rotateAxis * 10f, Color.red);

                    transform.RotateAround (rotatePoint, rotateAxis, -speedAngular * Input.GetAxis ("VerticalLeft") * Time.fixedDeltaTime * (squaredMovement ? Mathf.Abs (Input.GetAxis ("VerticalLeft")) : 1));

                }
                if (Mathf.Abs (Input.GetAxis ("VerticalRight")) > joyDeadZone) {
                    Vector3 rotatePoint = (transform.position) + transform.TransformDirection (Vector3.left) * rotationOffset;
                    Vector3 rotateAxis = transform.TransformDirection (Vector3.up);

                    Debug.DrawRay (rotatePoint, rotateAxis * 10f, Color.red);


                    transform.RotateAround (rotatePoint, rotateAxis, speedAngular * Input.GetAxis ("VerticalRight") * Time.fixedDeltaTime * (squaredMovement ? Mathf.Abs (Input.GetAxis ("VerticalRight")) : 1));

                }


            } else if (driveMode == DriveModes.Mecanum) {


                if (Mathf.Abs (Input.GetAxis ("VerticalRight")) > joyDeadZone) {
                    transform.Translate (Vector3.forward * -speedLinear * Input.GetAxis ("VerticalRight") * Time.fixedDeltaTime * (squaredMovement ? Mathf.Abs (Input.GetAxis ("VerticalRight")) : 1));
                }

                if (Mathf.Abs (Input.GetAxis ("HorizontalRight")) > joyDeadZone) {
                    transform.Translate (Vector3.right * speedLinear / 5f * Input.GetAxis ("HorizontalRight") * Time.fixedDeltaTime * (squaredMovement ? Mathf.Abs (Input.GetAxis ("HorizontalRight")) : 1));
                }

                if (Mathf.Abs (Input.GetAxis ("TwistRight")) > joyDeadZone) {
                    transform.Rotate (0, speedAngular * Input.GetAxis ("TwistRight") * Time.fixedDeltaTime * (squaredMovement ? Mathf.Abs (Input.GetAxis ("TwistRight")) : 1), 0);

                }

            }

            float linearVelocity = Mathf.Round (((transform.position - lastLinearPosition) / Time.fixedDeltaTime).magnitude * 100f) / 100f;
            lastLinearPosition = transform.position;

            float angularVelocity = Mathf.Round ((transform.eulerAngles.y - lastAngularPosition) / Time.fixedDeltaTime * Mathf.Deg2Rad  * 100f) / 100f;
            lastAngularPosition = transform.eulerAngles.y;

            uiController.UpdateVelocities (linearVelocity, angularVelocity);


        }
    }

    void OnCollisionExit(Collision col){

        rigidbody.isKinematic = true;

        rigidbody.velocity = Vector3.zero;
        rigidbody.angularVelocity = Vector3.zero;

        rigidbody.isKinematic = false;

    }
    public enum DriveModes{
        Tank,
        Mecanum
    }

    public void setDriveMode(){

        int index = PauseCanvas.transform.GetChild (1).GetComponent<Dropdown> ().value;

        if (index == 0) {
            driveMode = DriveModes.Tank;
        } else if (index == 1) {
            driveMode = DriveModes.Mecanum;
        }

        uiController.UpdateDriveMode (index == 0 ? "Tank" : "Mecanum");
    }
}

【问题讨论】:

  • 如何移动这个对象?请添加一些代码!
  • @Programmer 添加代码,这个类控制玩家的移动

标签: c# unity3d


【解决方案1】:

当 GameObject 具有 Rigidbody 和 Collider 并且您想要碰撞时,请不要将 Object 移动 transform.Translate 或使用 transform.Rotate 或 transform.RotateAround 旋转它。当你这样做时,不会发生碰撞。

使用Rigidbody 和碰撞移动游戏对象的正确方法是使用Rigidbody.MovePosition 函数。

旋转它的正确方法是使用Rigidbody.MoveRotation函数。


Rigidbody.MovePosition 的示例:

float h = Input.GetAxisRaw("Horizontal");
float v = Input.GetAxisRaw("Vertical");

Vector3 tempVect = new Vector3(h, 0, v);
tempVect = tempVect.normalized * speed * Time.deltaTime;
rb.MovePosition(transform.position + tempVect);

Rigidbody.MoveRotation 的示例:

Quaternion deltaRotation = Quaternion.Euler(eulerAngleVelocity * Time.deltaTime);
rb.MoveRotation(rb.rotation * deltaRotation);

您需要将transform.Translate、transform.Rotate 和transform.RotateAround 替换为这些。

【讨论】:

  • 我不再使用这些函数,因为它们不能完全按照我的意愿工作,但是当我恢复到该代码并再次尝试时,问题仍然存在。我知道你不应该这样做,但它适用于一个场景而不是另一个场景,我不知道为什么。如果您想更详细地查看它,我已将 github 链接添加到该项目。感谢您的帮助!
  • 它有时是否有效并不重要。我上面提到的是问题,你必须像我在回答中提到的那样解决它
  • 其实我刚刚发现问题,有第二个碰撞器干扰隐藏在一个子对象中。因为它是一个预制件,所以不太确定发生了什么。不过,运动和碰撞确实可以正常工作。感谢您的帮助!
  • 不客气。只是为了让您知道,您正在创建一个有缺陷的游戏。在您编写更多代码之前,请修复它。越早解决这个问题越好。如果您不修复它并将其作为游戏发布,您将收到碰撞有时不起作用的报告。任何时候都不能调用碰撞回调函数。这将是随机的。
  • 感谢您的建议。我通常不会这样做,但这是一个基本上只能由我使用的模拟,所以我不必担心那种事情。
猜你喜欢
  • 1970-01-01
  • 2016-06-06
  • 2020-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多