繁体   English   中英

汽车GameObject跌落地形

[英]Car GameObject falling through the Terrain

我最近一直在从事一个项目,那里有一个场景,那里有山脉,森林和汽车。

汽车在地形上行驶时,汽车会穿透地形

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

  • 在汽车上有Mesh Collider并附有RigidBody

  • 在地形上有凸为False的Mesh Collider。


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);


  }
}

我之前已经回答了这个问题,但是找不到其他答案将其标记为重复。 您将WheelCollider用于汽车。

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

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

WheelCollider.brakeTorque用于制动汽车。

WheelCollider.steerAngle用于操纵汽车。

那里有许多教程, 是直接来自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);
        }
    }
}

暂无
暂无

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

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