Skip to main content
added 46 characters in body
Source Link
using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour 
{
    public float speed;
    public float tilt;
    public float power;

    /** 
     * Every update perform tranlations and rotations based on user input.
     * */
    void FixedUpdate () {
    {
        float moveHorizontal = Input.GetAxis ("Horizontal");
        float moveVertical = Input.GetAxis ("Vertical");

        Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
        rigidbody.velocity = movement * speed;

        // Add the tilt. (When this line is left out, rotation performs as expected)
        // When it is left in, the drone rotates but snaps back to it's original orientation on key release.
        rigidbody.rotation = Quaternion.Euler (rigidbody.velocity.z, 0.0f, rigidbody.velocity.x * -tilt);

        // Ascend.
        if (Input.GetKey ("t"))
        {
            rigidbody.AddRelativeForce (Vector3.up * power);
        }

        // Descend.
        if (Input.GetKey ("g")) {
            rigidbody.AddRelativeForce (Vector3.down * power);
        }

        // Rotate clockwise.
        if (Input.GetKey ("h")) 
        {
            transform.Rotate(0, 5, 0, Space.Self);
        }

        // Rotate anti-clockwise.
        if (Input.GetKey ("f")) 
        {
            transform.Rotate(0, -5, 0, Space.Self);
        }
    }
}
using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour 
{
    public float speed;
    public float tilt;
    public float power;

    /** 
     * Every update perform tranlations and rotations based on user input.
     * */
    void FixedUpdate () {

        float moveHorizontal = Input.GetAxis ("Horizontal");
        float moveVertical = Input.GetAxis ("Vertical");

        Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
        rigidbody.velocity = movement * speed;

        // Add the tilt. (When this line is left out, rotation performs as expected)
        // When it is left in, the drone rotates but snaps back to it's original orientation on key release.
        rigidbody.rotation = Quaternion.Euler (rigidbody.velocity.z, 0.0f, rigidbody.velocity.x * -tilt);

        // Ascend.
        if (Input.GetKey ("t")){
            rigidbody.AddRelativeForce (Vector3.up * power);
        }

        // Descend.
        if (Input.GetKey ("g")) {
            rigidbody.AddRelativeForce (Vector3.down * power);
        }

        // Rotate clockwise.
        if (Input.GetKey ("h")) {
            transform.Rotate(0, 5, 0, Space.Self);
        }

        // Rotate anti-clockwise.
        if (Input.GetKey ("f")) {
            transform.Rotate(0, -5, 0, Space.Self);
        }
    }
}
using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour 
{
    public float speed;
    public float tilt;
    public float power;

    /** 
     * Every update perform tranlations and rotations based on user input.
     * */
    void FixedUpdate () 
    {
        float moveHorizontal = Input.GetAxis ("Horizontal");
        float moveVertical = Input.GetAxis ("Vertical");

        Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
        rigidbody.velocity = movement * speed;

        // Add the tilt. (When this line is left out, rotation performs as expected)
        // When it is left in, the drone rotates but snaps back to it's original orientation on key release.
        rigidbody.rotation = Quaternion.Euler (rigidbody.velocity.z, 0.0f, rigidbody.velocity.x * -tilt);

        // Ascend.
        if (Input.GetKey ("t"))
        {
            rigidbody.AddRelativeForce (Vector3.up * power);
        }

        // Descend.
        if (Input.GetKey ("g")) {
            rigidbody.AddRelativeForce (Vector3.down * power);
        }

        // Rotate clockwise.
        if (Input.GetKey ("h")) 
        {
            transform.Rotate(0, 5, 0, Space.Self);
        }

        // Rotate anti-clockwise.
        if (Input.GetKey ("f")) 
        {
            transform.Rotate(0, -5, 0, Space.Self);
        }
    }
}
edited title
Link
user1430
user1430

Unity How can I add banking and rotation - Object returns to starting orientation on key releasethis object manipulation script?

edited body
Link

Unity rotation - Rotate and tilt not working togetherObject returns to starting orientation on key release

edited body
Source Link
Loading
added 58 characters in body
Source Link
Loading
added 21 characters in body
Source Link
Loading
Source Link
Loading