【问题标题】:Car GameObject falling through the Terrain汽车游戏对象从地形上掉下来
【发布时间】:2017-05-11 19:12:36
【问题描述】:

我最近在做一个项目,有一个场景,有山有林有车。

当汽车在地形上移动时,汽车穿透地形

我只想知道如何阻止这种情况发生。

  • 车上有Mesh Collider,并附上RigidBody

  • 地形上有Mesh Collider,凸为False。


public class Motor : MonoBehaviour {

public float moveSpeed = 5.0f;
public float drag = 0.5f;
public float terminalRoatationSpeed = 25.0f;
public Virtualjoystick moveJoystick;

private Rigidbody controller;
private Transform camTransform;


// Use this for initialization
void Start () {


    controller = GetComponent<Rigidbody> ();
    controller.maxAngularVelocity = terminalRoatationSpeed;
    controller.drag = drag;

    camTransform = Camera.main.transform;


}

// Update is called once per frame
void FixedUpdate () {

    Vector3 dir = Vector3.zero;

    dir.x = Input.GetAxis ("Horizontal");
    dir.z = Input.GetAxis ("Vertical");

    if(dir.magnitude > 1)dir.Normalize();

    if(moveJoystick.InputDirection != Vector3.zero)
    {
        dir = moveJoystick.InputDirection;
    }



    // Rotate our Direction vector with Camera 
    Vector3 rotatedDir = camTransform.TransformDirection(dir);
    rotatedDir = new Vector3 (rotatedDir.x, 0, rotatedDir.z);
    rotatedDir = rotatedDir.normalized * dir.magnitude;

    controller.AddForce (rotatedDir * moveSpeed);


  }
}

【问题讨论】:

  • 为您的问题添加更多信息。你如何移动汽车......代码?您是否在汽车上安装了对撞机?这种情况是每次还是有时都会发生?
  • 添加RigidBodys 并确保他们都没有设置IsTrigger 选项。
  • @TamásSzabó 我做到了......它没有工作,但汽车仍在穿过地形
  • 正如@Programmer 所说,你如何移动你的车?您(我假设) CarController 脚本的片段可以帮助我们很多。只需编辑您的问题并包含代码即可。
  • @TamásSzabó 我刚刚包含了写在车上的代码

标签: c# unity3d unity5


【解决方案1】:

我以前回答过这个问题,但找不到其他答案来将此问题标记为重复。您使用 WheelCollider 表示汽车。

您需要将WheelCollider 附加到您汽车的所有车轮上。这样您的汽车将始终位于Terrain Collider 的顶部。

WheelCollider.motorTorque 用于向前或向后移动汽车。

WheelCollider.brakeTorque用于刹车。

WheelCollider.steerAngle 用于驾驶汽车。

那里有很多教程,here 是一个直接来自 Unity 文档的示例:

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

[System.Serializable]
public class AxleInfo {
    public WheelCollider leftWheel;
    public WheelCollider rightWheel;
    public bool motor;
    public bool steering;
}

public class SimpleCarController : MonoBehaviour {
    public List<AxleInfo> axleInfos; 
    public float maxMotorTorque;
    public float maxSteeringAngle;

    // finds the corresponding visual wheel
    // correctly applies the transform
    public void ApplyLocalPositionToVisuals(WheelCollider collider)
    {
        if (collider.transform.childCount == 0) {
            return;
        }

        Transform visualWheel = collider.transform.GetChild(0);

        Vector3 position;
        Quaternion rotation;
        collider.GetWorldPose(out position, out rotation);

        visualWheel.transform.position = position;
        visualWheel.transform.rotation = rotation;
    }

    public void FixedUpdate()
    {
        float motor = maxMotorTorque * Input.GetAxis("Vertical");
        float steering = maxSteeringAngle * Input.GetAxis("Horizontal");

        foreach (AxleInfo axleInfo in axleInfos) {
            if (axleInfo.steering) {
                axleInfo.leftWheel.steerAngle = steering;
                axleInfo.rightWheel.steerAngle = steering;
            }
            if (axleInfo.motor) {
                axleInfo.leftWheel.motorTorque = motor;
                axleInfo.rightWheel.motorTorque = motor;
            }
            ApplyLocalPositionToVisuals(axleInfo.leftWheel);
            ApplyLocalPositionToVisuals(axleInfo.rightWheel);
        }
    }
}

【讨论】:

  • 好的,谢谢你的回答,我会试试这个
  • 我在四个车轮上安装了车轮碰撞器并根据尺寸调整了半径......但是当碰撞器接触地形时,汽车在空中反弹并旋转如何解决这个问题
  • 不,这确实解决了问题....我的问题是当汽车在地形上移动时,如果地形上有斜坡,汽车正在穿过地形而不是爬上斜坡当汽车在行驶时不接触地形时,我需要让汽车在不经过地形的情况下在地形上移动
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 2018-01-23
相关资源
最近更新 更多