跟随指导直升机直飞的团结

时间:2019-02-20 11:56:01

标签: unity3d

我对团结还很陌生,但是越来越好。 我正在尝试制造一架直升机,我有一台我用3ds max制成的简单直升机 我用正确的轴输出了。 我发现本教程是通过java脚本ive转换为c#的。 链接到教程 https://andrewgotow.files.wordpress.com/2014/08/helicoptertutorial.pdf

但是它并没有说直升机是如何链接的,所以不知道那是我做错的事情。 我有一个空的游戏对象,上面有脚本和刚体,这是物体和rota的父对象。

在我的ps4上,旋转的右摇杆向左旋转会降低旋转速度或向右加快旋转速度 整个想法随着ps4上的左操纵杆左右旋转 所以看起来不错。 但是我无法让它飞起来,只是翻滚而过 任何帮助将非常感谢您的关注

这是我的项目

https://www.dropbox.com/s/vspdqds99mho0ye/heli2%20-%20Copy.zip?dl=0

这是脚本

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

public class heli : MonoBehaviour
{
 public GameObject main_Rotor_GameObject;            // gameObject to be animated
 public GameObject tail_Rotor_GameObject;          // gameObject to be animated

public float max_Rotor_Force  = 22241.1081f;    // newtons
public float max_Rotor_Velocity = 7200;         // degrees per second
public float rotor_Velocity  = 0.0f;            // value between 0 and 1
public float rotor_Rotation = 0.0f;             // degrees... used for animating rotors

public float max_tail_Rotor_Force  = 15000.0f;      // newtons
public float max_Tail_Rotor_Velocity = 2200.0f;      // degrees per second
public float tail_Rotor_Velocity  = 0.0f;           // value between 0 and 1
public float tail_Rotor_Rotation  = 0.0f;           // degrees... used for animating rotors

public float forward_Rotor_Torque_Multiplier = 0.5f;            // multiplier for control input
public float sideways_Rotor_Torque_Multiplier =  0.5f;          // multiplier for control input

public bool main_Rotor_Active = true;       // boolean for determining if a prop is active
public bool tail_Rotor_Active = true;       // boolean for determining if a prop is active
public Vector3 torqueValue;

public Rigidbody rb;

// Start is called before the first frame update
void Start()
{

}

   //Forces are applied in a fixed update function so that they are 
consistent no matter what the frame rate of the game is.This is
// important to keeping the helicopter stable in the air. If the forces were 
applied at an inconsistent rate, the helicopter would behave 
// irregularly.
void FixedUpdate()
{
    // First we must compute the torque values that are applied to the 
 helicopter by the propellers. The "Control Torque" is used to simulate
    // the variable angle of the blades on a helicopter and the "Torque 
 Value" is the final sum of the torque from the engine attached to the 
    // main rotor, and the torque applied by the tail rotor.
   // Vector3 torqueValue;
    Vector3 controlTorque =  new Vector3(Input.GetAxis("Vertical") * 
 forward_Rotor_Torque_Multiplier, 1.0f, -Input.GetAxis("Horizontal2") * 
 sideways_Rotor_Torque_Multiplier);

    // Now check if the main rotor is active, if it is, then add it's torque 
 to the "Torque Value", and apply the forces to the body of the 
    // helicopter.
    if (main_Rotor_Active == true)
    {
        torqueValue += (controlTorque * max_Rotor_Force * rotor_Velocity);

        // Now the force of the prop is applied. The main rotor applies a 
 force direclty related to the maximum force of the prop and the 
        // prop velocity (a value from 0 to 1)
       // rb.AddRelativeForce(Vector3.up * max_Rotor_Force * rotor_Velocity);

        // This is simple code to help stabilize the helicopter. It 
 essentially pulls the body back towards neutral when it is at an angle to
        // prevent it from tumbling in the air.
        if (Vector3.Angle(Vector3.up, transform.up) < 80)
        {
            transform.rotation = Quaternion.Slerp(transform.rotation, 
Quaternion.Euler(0, transform.rotation.eulerAngles.y, 0), Time.deltaTime * 
rotor_Velocity * 2);
        }
    }

    // Now we check to make sure the tail rotor is active, if it is, we add 
it's force to the "Torque Value"
    if (tail_Rotor_Active == true)
    {
        torqueValue -= (Vector3.up * max_tail_Rotor_Force * 
 tail_Rotor_Velocity);
    }

    // And finally, apply the torques to the body of the helicopter.
   rb.AddRelativeTorque(torqueValue);


}


void Update()
{
    // This line simply changes the pitch of the attached audio emitter to 
   match the speed of the main rotor.
   // GetComponent.< AudioSource > ().pitch = rotor_Velocity;

    // Now we animate the rotors, simply by setting their rotation to an 
  increasing value multiplied by the helicopter body's rotation.
    if (main_Rotor_Active == true)
    {
        main_Rotor_GameObject.transform.rotation = transform.rotation * 
 Quaternion.Euler(0, rotor_Rotation, 0);
    }
    if (tail_Rotor_Active == true)
    {
        tail_Rotor_GameObject.transform.rotation = transform.rotation * 
 Quaternion.Euler(tail_Rotor_Rotation, 0, 0);
    }

    // this just increases the rotation value for the animation of the 
 rotors.
    rotor_Rotation += max_Rotor_Velocity * rotor_Velocity * Time.deltaTime;
    tail_Rotor_Rotation += max_Tail_Rotor_Velocity * rotor_Velocity * 
  Time.deltaTime;

    // here we find the velocity required to keep the helicopter level. With 
 the rotors at this speed, all forces on the helicopter cancel 
    // each other out and it should hover as-is.
    var hover_Rotor_Velocity = (rb.mass * Mathf.Abs(Physics.gravity.y) / 
 max_Rotor_Force);
    var hover_Tail_Rotor_Velocity = (max_Rotor_Force * rotor_Velocity) / 
 max_tail_Rotor_Force;

    // Now check if the player is applying any throttle control input, if 
 they are, then increase or decrease the prop velocity, otherwise, 
    // slowly LERP the rotor speed to the neutral speed. The tail rotor 
velocity is set to the neutral speed plus the player horizontal input. 
    // Because the torque applied by the main rotor is directly proportional 
to the velocity of the main rotor and the velocity of the tail rotor,
    // so when the tail rotor velocity decreases, the body of the helicopter 
 rotates.
    if (Input.GetAxis("Vertical2") != 0.0)
    {
        rotor_Velocity += Input.GetAxis("Vertical2") * 0.001f;
    }
    else
    {
        rotor_Velocity = Mathf.Lerp(rotor_Velocity, hover_Rotor_Velocity, Time.deltaTime * Time.deltaTime * 5);
    }
    tail_Rotor_Velocity = hover_Tail_Rotor_Velocity - Input.GetAxis("Horizontal");

    // now we set velocity limits. The multiplier for rotor velocity is fixed to a range between 0 and 1. You can limit the tail rotor velocity 
    // too, but this makes it more difficult to balance the helicopter variables so that the helicopter will fly well.
    if (rotor_Velocity > 1.0)
    {
        rotor_Velocity = 1.0f;
    }
    else if (rotor_Velocity < 0.0)
    {
        rotor_Velocity = 0.0f;
    }
}

}

0 个答案:

没有答案