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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to make the power to be working with the space ship and all the other inputs ?

Discussion in 'Scripting' started by benydayag, Sep 18, 2016.

  1. benydayag

    benydayag

    Joined:
    Aug 1, 2016
    Posts:
    91
    This is the script in c#

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class MakeObjectFly : MonoBehaviour {
    6.  
    7.     float turnspeed = 5.0F;
    8.     float speed = 5.0F;
    9.     private float trueSpeed = 1.0F;
    10.     float strafeSpeed = 5.0F;
    11.     Rigidbody _rigidbody;
    12.     private bool liftoff = false;
    13.  
    14.     // Use this for initialization
    15.     void Start () {
    16.  
    17.         _rigidbody = GetComponent<Rigidbody> ();
    18.    
    19.     }
    20.    
    21.     // Update is called once per frame
    22.     void Update () {
    23.  
    24.         var roll = Input.GetAxis ("Roll");
    25.         var pitch = Input.GetAxis ("Pitch");
    26.         var yaw = Input.GetAxis ("Yaw");
    27.         Vector3 strafe = new Vector3 (Input.GetAxis ("Horizontal") * strafeSpeed * Time.deltaTime, Input.GetAxis ("Vertical") * strafeSpeed * Time.deltaTime, 0);
    28.  
    29.         var power = Input.GetAxis ("Power");
    30.  
    31.         //Truespeed controls
    32.  
    33.         if (trueSpeed < 10 && trueSpeed > -3){
    34.             trueSpeed += power;
    35.             liftoff = true;
    36.         }
    37.  
    38.         if (trueSpeed > 10){
    39.             //trueSpeed = 9.99F;
    40.             trueSpeed -= power;
    41.         }
    42.         if (trueSpeed < -3){
    43.             trueSpeed = -2.99F;  
    44.         }
    45.         if (Input.GetKey("backspace")){
    46.             trueSpeed = 0;
    47.         }
    48.  
    49.  
    50.  
    51.         if (liftoff == true) {
    52.             _rigidbody.AddRelativeTorque (pitch * turnspeed * Time.deltaTime, yaw * turnspeed * Time.deltaTime, roll * turnspeed * Time.deltaTime);
    53.             _rigidbody.AddRelativeForce (0, 0, trueSpeed * speed * Time.deltaTime);
    54.             _rigidbody.AddRelativeForce (strafe);
    55.         }
    56.  
    57.    
    58.     }
    59. }
    In the unity i clicked in the menu on Edit > Project Settings > Input
    And then changed the Axes size to 22.

    Now i have Horizontal and Vertical twice of each.
    This is a screenshot of the first two showing on the right in the inspector the keys i set for each one buttons. The other two Horizontal and Vertical are empty i didn't set to them any buttons:
    When i click on i,u or p,o it's not doing anything. What the Horizontal and Vertiacl should do ? And why this settings are wrong how should i set it ?






    Next the Power input. If i understand it right Power mean the space ship should move faster ? Or it's more like landing and lift up ? What i want to do is that when running the game the space ship will be in idle state on ground and when i click the Power it will start lift up and in any point when clicking again it should land back down to ground. so i set to the Power the keys z and x but it's not effecting like i wanted when i click z it's making the space ship move up faster. And again i'm not sure what the Power mean to do ? To make lift/land effect or to give more speed to the space ship when it's already flying ?

    Screenshot of the Power inspector settings:




    The Yaw and Pitch and Roll inputs are working the Pitch making it turn on the pitch and yaw make it move also a bit strange. Roll make it turn around on the X axis left and right. In fact only this 3 inputs are working fine so far i guess.

    I took the script and inputs from this tutorial:

    https://stopsecretdesign.wordpress.com/2011/09/19/unity-spaceship-tutorial/

    Another important thing:
    On the space ship properties in the inspector i have: Rigidbody Use Gravitiy is UNCHECKED.

    I also have Mesh Collider but i'm not using it now if i will do it will give me error and then i need to CHECK the convex but then it will make the space ship move strange like on it's pitch or something. In the other hand i want to use the Mesh Collider if not the player the ThirdPersoncharacter will be able to walk through the space ship and i want it to collide so the player can climb on the space ship for example.

    And also i have on the space ship the Make Object Fly script.

    The Main Camera is under the space ship.

    This is a screenshot of the space ship inspector:



    What i want to do when the game is running:

    1. Space ship is in idle state.

    2. When the player get in the space ship it dos not matter now hwo he will get in but to make something that will start the space ship lift up straight up by pressing on one of the input keys. Clicking another input key will make the space ship move down land on ground. By lifting up and landing i mean to move Vertical up and down.

    3. When the space ship getting to some height point then the player can start fly it around. Not sure how to make it fly around with the user control it.

    4. To think how to make so the user will be able to land the space ship when it's flying around what steps should be done.
     
  2. IsGreen

    IsGreen

    Joined:
    Jan 17, 2014
    Posts:
    206
    Use Time.deltaTime adding forces to Rigidbody is not effective.

    ForceMode is important to adjust effect. Some ForceMode ignore mass, and others not.

    To use RigidBody forces, bassically you need:

    1) Know velocity that you need.
    2) Know actual velocity (rigidbody.velocity.magnitude).
    3) Apply difference between velocity and actual velocity using some type of ForceMode(ignore mass or not).

    Use AddRelativeForces is easier to use AddForce to do this.