对于摩托车赛车游戏我想要赛道的某些部分来提高摩托车的速度。
我有一个公共浮动速度,显示检查员的速度。
我添加了一个带有碰撞器的飞机,这是一个触发器并且标签为“speedfield” 现在我想,一些简单的代码,附在下面的motorcyclescript上,就可以了:
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ("speedfield")) {
Speed = Speed + 50;
}
}
但没有任何反应。我想我在这里遗漏了一些明显的东西。希望你能帮忙!来自德国的问候:)
void FixedUpdate (){
Inputs();
Engine();
}
void Inputs (){
Speed = rigid.velocity.magnitude * 3.6f;
//Freezing rotation by Z axis.
transform.eulerAngles = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, 0);
//If crashed...
if(!crashed){
if(!changingGear)
motorInput = Input.GetAxis("Vertical");
else
motorInput = Mathf.Clamp(Input.GetAxis("Vertical"), -1, 0);
steerInput = Input.GetAxis("Horizontal");
}else{
motorInput = 0;
steerInput = 0;
}
//Reverse bool
if(motorInput < 0 && transform.InverseTransformDirection(rigid.velocity).z < 0)
reversing = true;
else
reversing = false;
}
void Engine (){
//Steer Limit.
SteerAngle = Mathf.Lerp(defsteerAngle, highSpeedSteerAngle, (Speed / highSpeedSteerAngleAtSpeed));
FrontWheelCollider.steerAngle = SteerAngle * steerInput;
//Engine RPM.
EngineRPM = Mathf.Clamp((((Mathf.Abs((FrontWheelCollider.rpm + RearWheelCollider.rpm)) * gearShiftRate) + MinEngineRPM)) / (currentGear + 1), MinEngineRPM, MaxEngineRPM);
// Applying Motor Torque.
if(Speed > maxSpeed){
RearWheelCollider.motorTorque = 0;
}else if(!reversing && !changingGear){
RearWheelCollider.motorTorque = EngineTorque * Mathf.Clamp(motorInput, 0f, 1f) * engineTorqueCurve[currentGear].Evaluate(Speed);
}
if(reversing){
if(Speed < 10){
RearWheelCollider.motorTorque = (EngineTorque * motorInput) / 5f;
}else{
RearWheelCollider.motorTorque = 0;
}
}
}
答案 0 :(得分:1)
确保满足此清单中的所有项目:
Rigidbody
组件。Rigidbody
组件(它没用,可能会减慢速度)。Mesh Collider
已Convex
且isTrigger
设为true
。OnTriggerEnter
方法位于摩托车脚本中。Collision Detection
的{{1}}设置为Rigidbody
和/或稍微降低时间项目设置中的Continuous
。< / LI>
最有可能的问题是由于最后两个要点。
答案 1 :(得分:1)
你改写速度变量:
void FixedUpdate (){
Inputs();
Engine();
}
void Inputs (){
Speed = rigid.velocity.magnitude * 3.6f;
...
}
将其更改为:
[SerializeField] float speedMultiplier = 3.6f;
[SerializeField] float speedMultiplierChange = 2;
void Inputs (){
Speed = rigid.velocity.magnitude * speedMultiplier;
...
}
和
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ("speedfield")) {
speedMultiplier += speedMultiplierChange;
}
}