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

Really odd question about rotation. Pretty challenging problem, if you wanna take a look.

Discussion in 'Scripting' started by Not_Sure, Mar 6, 2015.

  1. Not_Sure

    Not_Sure

    Joined:
    Dec 13, 2011
    Posts:
    3,541
    I'm playing around with a Descent style player controller.

    You know, Descent:


    So I made a toggle so that the Yole (tilting left to right) will lock towards "down".

    Here's where it gets interesting.

    I want to try to make it so that "down" is not just on the Y world axis.

    I want "down" to be pointing at (0, 0, 0), the center of the map.

    It's crude, but here's the code I have so far:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PlayerMovement : MonoBehaviour {
    6.  
    7.     public float playerPitch;        //Looking up and down
    8.     public float playerTurn;        //Looking side to side
    9.     public float playerYole;        //Tilting side to side
    10.     public float playerStrafe;        //Moving side to side
    11.     public float playerThrottle;    //Moving back and forward
    12.     public float playerBob;            //Moving up and down
    13.  
    14.     public bool yoleLockToggle;        //Locks the Yole
    15.     public bool cameraToggle;        //Turns the camera selection on and displays rear
    16.     public bool systemToggle;        //Turns the system selection
    17.  
    18.     Rigidbody playerRigidbody;         //The rigidbody
    19.  
    20.     public float movementSpeed;        //Top movement speed
    21.     public float rotationSpeed;        //Top rotation speed
    22.  
    23.     private Vector3 movement;        //Vector 3 of movement
    24.     private Vector3 rotation;        //Vector 3 of rotation
    25.  
    26.     public bool testBool;
    27.  
    28.  
    29.  
    30.     // Use this for initialization
    31.     void Awake () {
    32.         playerRigidbody = GetComponent <Rigidbody> ();
    33.     }
    34.  
    35.     // Update is called once per frame
    36.     void Update () {
    37.         yoleLock ();
    38.         playerInput ();
    39.     }
    40.  
    41.     void playerInput (){
    42.         // Set the input values
    43.         playerPitch     = Input.GetAxis("Pitch");
    44.         playerTurn         = Input.GetAxis("Turn");
    45.         playerYole         = Input.GetAxis("Yole");
    46.         playerStrafe     = Input.GetAxis("Strafe");
    47.         playerThrottle     = Input.GetAxis("Throttle");
    48.         playerBob         = Input.GetAxis("Bob");
    49.  
    50.         if (Input.GetButtonDown ("YoleLock") == true) {
    51.             yoleLockToggle = !yoleLockToggle;
    52.         }
    53.         if (Input.GetAxis ("Yole") > .01 || Input.GetAxis ("Yole") < -.01) {
    54.             yoleLockToggle = false;
    55.         }
    56.  
    57.         transform.Translate (Vector3.forward * Time.deltaTime * movementSpeed * playerThrottle * -1);
    58.         transform.Translate (Vector3.right * Time.deltaTime * movementSpeed * playerStrafe);
    59.         transform.Translate (Vector3.up * Time.deltaTime * movementSpeed * playerBob);
    60.  
    61.         transform.Rotate (Vector3.back * Time.deltaTime * rotationSpeed * playerYole * 360);
    62.         transform.Rotate (Vector3.left * Time.deltaTime * rotationSpeed * playerPitch * 360);
    63.         if (yoleLockToggle == false) {
    64.             transform.Rotate (Vector3.up * Time.deltaTime * rotationSpeed * playerTurn * 360);
    65.         }
    66.         if (yoleLockToggle == true) {
    67.             transform.Rotate (Vector3.up * Time.deltaTime * rotationSpeed * playerTurn * 360, Space.World);
    68.         }
    69.     }  
    70.  
    71.     void yoleLock (){
    72.         if (yoleLockToggle == true && gameObject.transform.rotation.eulerAngles.z > 175){
    73.             testBool = true;
    74.             transform.Rotate (Vector3.back * Time.deltaTime * rotationSpeed * 90 * -1);
    75.         }
    76.         if (yoleLockToggle == true && gameObject.transform.rotation.eulerAngles.z < 175) {
    77.             testBool = true;
    78.             transform.Rotate (Vector3.back * Time.deltaTime * rotationSpeed * 90);
    79.         }
    80.     }
    81. }
    82.  
    I'm not looking for someone to "do my homework, but I'm not really sure how to even go at it.

    Any suggestions are welcome.

    Thanks!

    EDIT: Oh, and it did occur to me that I could apply the "look at" function to only the z rotation. I'm just not sure how.
     
  2. cranky

    cranky

    Joined:
    Jun 11, 2014
    Posts:
    180
    I am sorry, but I don't quite understand your problem. By down, I believe you mean Vector3.down, correct? Regardless, it seems you want a vector pointing at 0,0,0. All you have to do is:

    Vector3 newDown = -transform.position.normalized;

    That will give you a vector pointing from transform.position to 0,0,0.

    Sorry if I misunderstood and it's no good :p.
     
    Not_Sure likes this.
  3. Not_Sure

    Not_Sure

    Joined:
    Dec 13, 2011
    Posts:
    3,541
    Oh, by 0,0,0 I meant in world space.
    I want to try to make a small planetoid were 0,0,0 is the center and "down" in terms of gravity as well.
     
  4. cranky

    cranky

    Joined:
    Jun 11, 2014
    Posts:
    180
    Right, what I posted will give you a vector pointing to 0,0,0 in world space. Is that what you wanted?
     
    Not_Sure likes this.
  5. Not_Sure

    Not_Sure

    Joined:
    Dec 13, 2011
    Posts:
    3,541
    Ahk!

    I'm an idiot.

    Yes, I think that is actually exactly what I was looking to do. I just misread it the first time.

    THANK YOU!
     
  6. cranky

    cranky

    Joined:
    Jun 11, 2014
    Posts:
    180
    Glad to have helped :).
     
  7. bloomingdedalus

    bloomingdedalus

    Joined:
    Aug 13, 2012
    Posts:
    139
    You're going to have to rewrite your code if you want your rotation algorithms to treat direction to (0,0,0) as "down" - the Vector3.down, left, up, etc. values are just static definitions. Vector3.down always equals (0,-1,0) - Vector3.up always equals (0,1,0) etc

    For example, if you define the direction (0,0,0) to be "down" and you're at location (100,0,0) then "down" is Vector3.left

    Forward really ceases to have a meaning as well in a spherical coordinate system as there are an infinite number of tangent lines tangent to any point on the surface of any sphere so you're going to have to create some new definitions to work with...
     
  8. bloomingdedalus

    bloomingdedalus

    Joined:
    Aug 13, 2012
    Posts:
    139
    Sorry, I was sleep-deprived when I wrote the above and not thinking... Transform.Translate moves around local and Transform.Rotate also moves around local so these should all work. Your script should still work...
     
  9. Not_Sure

    Not_Sure

    Joined:
    Dec 13, 2011
    Posts:
    3,541
    Yeah, that crude script would get a total rework.

    I just wanted to see if a Descent player controller could work on a gamepad.

    It does. Swimmingly.
     
  10. cranky

    cranky

    Joined:
    Jun 11, 2014
    Posts:
    180
    Keep us updated on it! I loved Descent as a kid. Brought back a lot of memories from just thinking about it :).