Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Need help with flying script

Discussion in 'Scripting' started by LoudThunder, Jun 10, 2014.

  1. LoudThunder

    LoudThunder

    Joined:
    Jun 10, 2014
    Posts:
    3
    Hoi guys,

    I geuss this qeustion is asked before but i can't find a answer that helps me.
    I want to make a speeder (like the one in starwars on courosant). The model is coming along, but the script isn't.
    I am trying to use AddForce to move it and make it fly. My current script is this:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class flying : MonoBehaviour {
    5.  
    6.     public float speed;
    7.     public float hover;
    8.     public float turnleftright;
    9.     public float turnupdown;
    10.     public float rotatespeed;
    11.     public float movement;
    12.     public Quaternion rotation = Quaternion.identity;
    13.     public float rotationx;
    14.     public float rotationy;
    15.     public float rotationz;
    16.     public Vector3 AddPos;
    17.     public float vary;
    18.  
    19.     // Use this for initialization
    20.     void Start () {
    21.  
    22.         hover = 50;
    23.         //rotatespeed = 35;
    24.    
    25.     }
    26.    
    27.     // Update is called once per frame
    28.     void Update (){
    29.  
    30.         ///////////////////////////////// key press for gas////////////////////////////
    31.         if (Input.GetKey ("space") && movement <= 5)
    32.         {
    33.             movement = movement + speed;
    34.         }
    35.         else if (Input.GetKey ("space") && movement >= 5)
    36.         {
    37.             movement = movement;
    38.         }
    39.  
    40.         ////////////////////////// key press to leftright //////////////////////////////
    41.         turnleftright = Input.GetAxis ("Horizontal") * rotatespeed;
    42.  
    43.  
    44.         ////////////////////////// key press to updown ////////////////////////////////
    45.         turnupdown = Input.GetAxis ("Vertical") * 100;
    46.  
    47.         }
    48.  
    49.     void FixedUpdate () {
    50.  
    51.         Vector3 down = transform.TransformDirection (Vector3.down);
    52.  
    53.         rotationx = transform.eulerAngles.x;
    54.         rotationy = transform.eulerAngles.y;
    55.         rotationz = transform.eulerAngles.z;
    56.  
    57.         vary = Mathf.Tan (rotationx) * 10;
    58.  
    59. //        Vector3 test (transform.position.x, vary, transform.ImagePosition.z);
    60.  
    61.         // checks if its to low to the ground, and then makes it hover
    62.         if(Physics.Raycast(transform.position, down, 1)){
    63.             rigidbody.AddForce(transform.up * hover);
    64.         }
    65.  
    66.         // move it
    67.         rigidbody.AddRelativeForce (transform.position.x * 2.0f, vary, transform.position.z);
    68.  
    69.         // rotate object
    70.         rigidbody.AddTorque(transform.up * turnleftright);
    71.  
    72.         // rotate up down
    73.         transform.Rotate (new Vector3 (turnupdown, 0, 0) * Time.deltaTime);
    74.  
    75.  
    76.         /////////////////////////////////////////////////////////////////////////////////
    77.  
    78.     }
    79. }
    But it seems that i cant get a smooth flying speeder. I am trying to use the angle of ratotation with some basic giometrie to figure out the force for Y. but it doesn't seem to work properly.

    Can you guys help this starting designer out?

    P.S. i want to use the physics engine, thats why i use AddForce
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    Code (csharp):
    1.  
    2.        rigidbody.AddRelativeForce (transform.position.x * 2.0f, vary, transform.position.z);
    3.  
    Here's one issue, probably your biggest one. Why would you add the position as force?

    What controls the "translation" movement? That's what you want to feed that function, not your current position.
     
  3. LoudThunder

    LoudThunder

    Joined:
    Jun 10, 2014
    Posts:
    3

    I see you chose AddRelativeForce. What s the advantage of that function over plain AddForce?

    With "translation" do you mean the function transform.translate or what creates the value for x? Incase of the latter it shoudl also be deduced from the angle of the object.
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    I wasn't making a recommendation, I was simply quoting your code to point out where the problem is.

    So, it's always going to move forward? Try something like this:
    Code (csharp):
    1.  
    2. rigidbody.AddRelativeForce(0f, 0f, 1f);
    3.  
     
  5. LoudThunder

    LoudThunder

    Joined:
    Jun 10, 2014
    Posts:
    3
    Thanks for the clarification. You got me thinking and i think i made a step forward. thanks.

    The idea is that you keep moving forward (might implement a variable later to controll the forward force) but if you tilt the nose up, you will start flying forward and up. if you start turning to the left you will go left, and stil up if the nose is still up. If the nose is down, you go down, if you turn right you start going right