Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

Making an object in game face the direction it's traveling while viewing from above.

Discussion in 'Scripting' started by ecnalyr, Oct 7, 2011.

  1. ecnalyr

    ecnalyr

    Joined:
    May 23, 2010
    Posts:
    38
    I am using the below code to move my ship along the X and Y axis in my environment but I cannot get the ship to face the direction in which the ship is moving.

    Every resource I have searched for suggests 'rotating' the ship to match the transform's rotation, but the transform does not rotate - everything simply moves along the X and Y axis (there is no rotation).

    I think what I need to do is somehow extract the 'travel direction' from the below script and use that to rotate my ship in a way that makes the ship face the direction of travel - but I am at a loss as to how to accomplish this.

    Any advice would be appreciated.

    (possibly extraneous information)
    I could fix this by allowing the game object that has the ship attached to it to rotate, but then when i push 'left' on the joystick the ship will not move 'left' according to the user's point of view, it will move to the ship's left - but this is not the behavior I desire.

    Code (csharp):
    1.  
    2. static var blackhole : Vector3 = Vector3.one;
    3.  
    4. @script RequireComponent( CharacterController )
    5.  
    6. var moveJoystick : Joystick;
    7. var rotateJoystick : Joystick;
    8.  
    9.  
    10. var forwardSpeed : float = 4;
    11. var backwardSpeed : float = 4;
    12. var sidestepSpeed : float = 4;                 
    13.  
    14. private var thisTransform : Transform;
    15. private var character : CharacterController;
    16.  
    17. private var velocity : Vector3;
    18.  
    19. function Start()
    20. {
    21.     // Cache component lookup at startup instead of doing this every frame     
    22.     thisTransform = GetComponent( Transform );
    23.     character = GetComponent( CharacterController );   
    24.  
    25.     // Move the character to the correct start position in the level, if one exists
    26.     var spawn = GameObject.Find( "PlayerSpawn" );
    27.     if ( spawn )
    28.         thisTransform.position = spawn.transform.position;
    29. }
    30.  
    31. function OnEndGame()
    32. {
    33.     // Disable joystick when the game ends 
    34.     moveJoystick.Disable();
    35.     rotateJoystick.Disable();  
    36.  
    37.     // Don't allow any more control changes when the game ends
    38.     this.enabled = false;
    39. }
    40.  
    41. function Update()
    42. {
    43.     //transform.LookAt(PlayerRelativeControl.blackhole, Vector3.forward);
    44.     var movement = thisTransform.TransformDirection( Vector3( 0, moveJoystick.position.y, moveJoystick.position.x ) );
    45.  
    46.     //Only x and y movement
    47.     movement.z = 0;
    48.     movement.Normalize();
    49.  
    50.  
    51.     // Apply movement from move joystick
    52.     var absJoyPos = Vector2( Mathf.Abs( moveJoystick.position.x ), Mathf.Abs( moveJoystick.position.y ) ); 
    53.     if ( absJoyPos.y > absJoyPos.x )
    54.     {
    55.         if ( moveJoystick.position.y > 0 )
    56.             movement *= forwardSpeed * absJoyPos.y;
    57.         else
    58.         {
    59.             movement *= backwardSpeed * absJoyPos.y;
    60.         }
    61.     }
    62.     else
    63.     {
    64.             movement *= sidestepSpeed * absJoyPos.x;
    65.     }
    66.    
    67.     //velocity = character.velocity;   
    68.     movement += velocity;  
    69.     movement *= Time.deltaTime;
    70.    
    71.     // Actually move the character 
    72.     character.Move( movement );
    73.  
    74. }
    75.  
    -- To recap, my ship is moving along the X and Y axis in my environment, I am viewing the ship from 'above' (from the Z axis), and the ship is currently always facing one direction while moving (literally the top of the screen) which is useless because it needs to look like it is traveling in the direction of movement.

    How do I get the ship to look like it is traveling in the direction of its movement?

    *****Update from the bottom of the thread*****

    Thanks again for the help, this all seems to be going in the right direction but I must have something messed up with my object that is causing it to behave incorrectly - I will post more information in an attempt to get assistance in finding the problem:

    -The 'object' that I have the above script attached to is a character controller object with a Rotation of X:0 Y: 90 Z: 0
    (If I have it 0,0,0 my ship doesn't move along the x/y window how I want it to)

    -The actual 3d 'ship' model is then attached to the above object (for information's sake, this 'ship' has a Rotation of X:0 Y:0 Z:0)

    When I made the most recent edit BigMisterB mentioned above, the behavior of the ship changed from flicking instantly to opposite facing directions to the following:

    - The ship appears to spin in circles most of the time (while the user is inputing a 'direction' for the ship to move).
    - Occasionally the ship flickers back and forth facing opposite directions (like it used to before this edit).
    - The ship occasionally locks in a direction (while using the joystick) and will drift in the direction indicated by the user.

    I am not quite sure what is happening at this point.

    Thank you for any assistance.

    Edit:

    Here are some things I've noticed in Unity itself while I'm running the game:

    -The Y rotation of the game object appears to only change in increments of 90 when the user attempts to move the ship (actually, pretty much only 90 and 270 (or 89.99999 and 269.999)).

    -The Z rotation of the game objects starts at 0 every time the game is 'played.' While issuing a command, with the joystick, the Z rotation switches to many different numbers, but always locks on "180" after the user ends the joystick command.
     
    Last edited: Oct 11, 2011
  2. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,073
    transform.forward = velocity
     
  3. ecnalyr

    ecnalyr

    Joined:
    May 23, 2010
    Posts:
    38
    That locks the ship pointing in one direction (in my cause toward the top of the screen - for what it's worth) and the ship loses its ability to move along the X axis, can move along the Y axis.

    - This results in a ship that's pointing strait toward the top of my screen and is only able to move up and down on the screen. No horizontal movement along the X.

    Any other tips?
     
  4. ecnalyr

    ecnalyr

    Joined:
    May 23, 2010
    Posts:
    38
    Unity is also giving this message -repeatedly- while running program:

    Look rotation viewing vector is zero
    UnityEngine.Transform:set_forward(Vector3)
    PlayerRelativeControl:Update() (at Assets/Standard Assets (Mobile)/Scripts/PlayerRelativeControl.js:84)
     
  5. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Fairly easy.. you already have the movement, just face that direction... Literally...
    Code (csharp):
    1.  
    2. var lookAt = movement;
    3. lookAt.z = 0; // if z is the up direction
    4. if(lookAt.magnitude > 0) transform.LookAt(transform.position + lookAt);
    5.  
     
  6. ecnalyr

    ecnalyr

    Joined:
    May 23, 2010
    Posts:
    38
    Almost there!

    When I use this code, the ship seems to flicker back and forth (facing direction) with every frame, and the ship barely moves - as if it is moving (and facing) in one direction, then immediately moving (and facing) the opposite direction).

    When I stop issuing movement commands the ship is facing immediately opposite of the direction I was intending on making the ship move.

    What am I not quite getting here?

    Thanks for the assistance so far.
     
  7. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Oh.... sorry

    Code (csharp):
    1.  
    2. var lookAt = movement;
    3. lookAt.z = 0; // if z is the up direction
    4. if(lookAt.magnitude > 0) transform.LookAt(transform.position + lookAt, transform.forward);
    5.  
    You need to use the forward vector of the transform in the LookAt... this will point the top of the camera towards the front of the object. ;)
     
  8. ecnalyr

    ecnalyr

    Joined:
    May 23, 2010
    Posts:
    38
    Thanks again for the help, this all seems to be going in the right direction but I must have something messed up with my object that is causing it to behave incorrectly - I will post more information in an attempt to get assistance in finding the problem:

    -The 'object' that I have the above script attached to is a character controller object with a Rotation of X:0 Y: 90 Z: 0
    (If I have it 0,0,0 my ship doesn't move along the x/y window how I want it to)

    -The actual 3d 'ship' model is then attached to the above object (for information's sake, this 'ship' has a Rotation of X:0 Y:0 Z:0)

    When I made the most recent edit BigMisterB mentioned above, the behavior of the ship changed from flicking instantly to opposite facing directions to the following:

    - The ship appears to spin in circles most of the time (while the user is inputing a 'direction' for the ship to move).
    - Occasionally the ship flickers back and forth facing opposite directions (like it used to before this edit).
    - The ship occasionally locks in a direction (while using the joystick) and will drift in the direction indicated by the user.

    I am not quite sure what is happening at this point.

    Thank you for any assistance.

    Edit:

    Here are some things I've noticed in Unity itself while I'm running the game:

    -The Y rotation of the game object appears to only change in increments of 90 when the user attempts to move the ship (actually, pretty much only 90 and 270 (or 89.99999 and 269.999)).

    -The Z rotation of the game objects starts at 0 every time the game is 'played.' While issuing a command, with the joystick, the Z rotation switches to many different numbers, but always locks on "180" after the user ends the joystick command.
     
    Last edited: Oct 10, 2011
  9. ecnalyr

    ecnalyr

    Joined:
    May 23, 2010
    Posts:
    38
    Is there other information I could post that could help result in an answer?
     
  10. mrdatsun

    mrdatsun

    Joined:
    Apr 8, 2020
    Posts:
    12
    The answers in this thread absolutely do not work. Thread should probably be deleted as if the OP eventually found a solution it was not shared.
     
unityunity