Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Delta6 · May 23, 2016 at 07:13 PM · scripting problemscripting beginnerflyingeasyhelicopter

Easy to fly helicopter script

hello I have a helicopter script but its really hard to fly and hover over a target. just wondering if there is a way to fix that. incase you don't know what I mean think of it like a car script. you press the forward key and it starts moving, then you let off the key and it stops. that's what I'm trying to do. if you know what I mean a can help please do so. I'm only a beginner so coding a have a problem with.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Delta6 · May 23, 2016 at 08:25 PM

This is the script

using UnityEngine; using System.Collections;

public class csHellicopter : MonoBehaviour {

 public KeyCode UpKey;
 public KeyCode DownKey;
 public KeyCode FrontSpin;
 public KeyCode BackWardSpin;
 public KeyCode LeftTurn;
 public KeyCode RightTurn;
 public KeyCode LeftSpin;
 public KeyCode RightSpin;
 public KeyCode EngineOnOffKey;

 public GameObject[] MainMotor; //Helicoptor Big Propeller. Use Y Rotate Value.
 public GameObject[] SubMotor; //Helicoptor Small Propeller. User X RotateValue.
 public AudioSource _AudioSource; //Helicoptor Engine Sound.

 public float MainMotorRotateVelocity;
 public float SubMotorRotateVelocity;

 float UpDownVelocity = 0.0f; 
 float MainMotorRotation = 0.0f;
 float SubMotorRotation = 0.0f;
 
 public float UpDownValue;
 float UpDown;
 float yUpDown;

 //Rotate Value
 float Pitch;
 float UpDownTurn;
 float yUpDownTrun;

 float Yaw;
 float LeftRightTurn;
 float yLeftRightTurn;

 float Roll;
 float LeftRightSpin;
 float yLeftRightSpin;

 bool EngineTurnOnOff = false;
 float EngineValue = 0.0f;

 public enum ControlState
 {
     Mouse,
     KeyBoard
 };

 public ControlState ControlType = new ControlState();

 void FixedUpdate()
 {
     if (EngineTurnOnOff)
     {
         GetComponent<Rigidbody>().AddRelativeForce(Vector3.up * UpDownValue * UpDownVelocity);

         if (transform.position.y > 100)
             GetComponent<Rigidbody>().AddRelativeForce(-Vector3.up * UpDownValue * UpDownVelocity);

         UpDown = KeyValue(DownKey,UpKey , UpDown, yUpDown, 1.5f, 0.01f);

         if (ControlType == ControlState.KeyBoard)
         {
             UpDownTurn = KeyValue(BackWardSpin, FrontSpin, UpDownTurn, yUpDownTrun, 1.5f, 0.1f);
             LeftRightTurn = KeyValue(LeftTurn, RightTurn, LeftRightTurn, yLeftRightTurn, 1.5f, 0.1f);
             LeftRightSpin = KeyValue(LeftSpin, RightSpin, LeftRightSpin, yLeftRightSpin, 1, 0.1f);
         }

         else if (ControlType == ControlState.Mouse)
         {
             //Pitch Value
             UpDownTurn = Input.GetAxis("Mouse Y");
             Pitch -= UpDownTurn * Time.fixedDeltaTime *0.01f;
             Pitch = Mathf.Clamp(Pitch, -1.2f, 1.2f);

             //Yaw Value
             LeftRightTurn = Input.GetAxis("Mouse X");
             Yaw += LeftRightTurn * Time.fixedDeltaTime *0.01f;

             LeftRightSpin = KeyValue(LeftSpin, RightSpin, LeftRightSpin, yLeftRightSpin, 1, 0.1f);
         }

         //Pitch Value
         Pitch += UpDownTurn * Time.fixedDeltaTime;
         Pitch = Mathf.Clamp(Pitch, -1.2f, 1.2f);

         //Yaw Value
         Yaw += LeftRightTurn * Time.fixedDeltaTime;

         //Roll Value
         Roll += -LeftRightSpin * Time.fixedDeltaTime;
         Roll = Mathf.Clamp(Roll, -1.2f, 1.2f);

         transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.EulerRotation(Pitch, Yaw, Roll), Time.fixedDeltaTime * 1.5f);
     }
 }
 
 void Update ()
 {
     SoundControl(); // Check Helicoptor Engine Sound
     MotorRotateControl(); // Check Motor Rotate State
     MotorVelocityContorl(); // Check Helicoptor UpDown State
     EngineControl(); // Check Engine Turn On/Off
 }

 float KeyValue(KeyCode A,KeyCode B, float Value , float yValue , float _float , float SmoothTime)
 {
     if(Input.GetKey(A))
         Value -= Time.deltaTime * _float;
     else if (Input.GetKey(B))
         Value += Time.deltaTime * _float;
     else
         Value = Mathf.SmoothDamp(Value, 0, ref yValue, SmoothTime);

     Value = Mathf.Clamp(Value, -1, 1);
     return Value;
 }

 void MotorVelocityContorl()
 {
     if (EngineTurnOnOff)
     {
         float Hovering = (Mathf.Abs(GetComponent<Rigidbody>().mass * Physics.gravity.y) / UpDownValue); //for maintain altitude.

         if (UpDown != 0.0f)
             UpDownVelocity += UpDown * 0.1f; //if Input Up/Down Axes, Increace UpDownVelocity for Increace altitude.
         else
             UpDownVelocity = Mathf.Lerp(UpDownVelocity, Hovering, Time.deltaTime); //if not input Up/Down Axes, Hovering.
     }
    CheckUpDownVelocity();
 }

 void CheckUpDownVelocity()
 {
     if (UpDownVelocity > 1.0f)
         UpDownVelocity = 1.0f;
     else if (UpDownVelocity < 0.1f)
         UpDownVelocity = 0.1f;
 }

 void MotorRotateControl()
 {
     if (MainMotor.Length > 0)
     {
         for (int i = 0; i < MainMotor.Length; i++)
             MainMotor[i].transform.Rotate(0, MainMotorRotation, 0);
     }

     if (SubMotor.Length > 0)
     {
         for (int i = 0; i < SubMotor.Length; i++)
             SubMotor[i].transform.Rotate(0, 0, SubMotorRotation);
     }

     MainMotorRotation = MainMotorRotateVelocity * EngineValue * Time.deltaTime;
     SubMotorRotation = SubMotorRotateVelocity * EngineValue * Time.deltaTime;
 }

 void EngineControl()
 {
     if (Input.GetKeyDown(EngineOnOffKey))
     {
         if (EngineTurnOnOff)
             EngineTurnOnOff = false;
         else
             EngineTurnOnOff = true;
     }

     if (EngineTurnOnOff == true)
     {
         if (EngineValue < 1)
             EngineValue += Time.deltaTime;
         if (EngineValue >= 1)
             EngineValue = 1;
     }

     else if (EngineTurnOnOff == false)
     {
         if (EngineValue > 0)
             EngineValue -= Time.deltaTime / 2;
         if (EngineValue <= 0)
             EngineValue = 0;
     }
 }

 void SoundControl()
 {
     if (EngineValue > 0)
     {
         _AudioSource.mute = false;
         _AudioSource.pitch = EngineValue;
     }
     else
         _AudioSource.mute = true;
 }

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Burak-Duman · Oct 10, 2017 at 10:02 AM

      if (EngineTurnOnOff)
      {   

         if (Input.GetKey(UpKey)) {
          GetComponent<Rigidbody>().AddRelativeForce(Vector3.up * UpDownValue * UpDownVelocity);
         }

               if (transform.position.y > 5000 )
              GetComponent<Rigidbody>().AddRelativeForce(-Vector3.up * UpDownValue * UpDownVelocity);
          UpDown = KeyValue(DownKey,UpKey , UpDown, yUpDown, 1.5f, 0.01f);

          if (ControlType == ControlState.KeyBoard)
          {
              UpDownTurn = KeyValue(BackWardSpin, FrontSpin, UpDownTurn, yUpDownTrun, 1.5f, 0.1f);
              LeftRightTurn = KeyValue(LeftTurn, RightTurn, LeftRightTurn, yLeftRightTurn, 1.5f, 0.1f);
              LeftRightSpin = KeyValue(LeftSpin, RightSpin, LeftRightSpin, yLeftRightSpin, 1, 0.1f);
          }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

63 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Enable/Disable MeshRenderer on trigger enter/exit 0 Answers

how can i save highest played level?,how can i save number of highest played level? 0 Answers

Having trouble deleting objects from a list after they reach a certain scale. 0 Answers

Self rotate issue C# 0 Answers

Weirdly behaving ai script. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges