Search Unity

Trouble making character face direction of travel

Discussion in 'Scripting' started by ecnalyr, Mar 21, 2011.

  1. ecnalyr

    ecnalyr

    Joined:
    May 23, 2010
    Posts:
    38
    Ideally I want my character to always face the 'blackhole' variable which I would have set up as my target that the player character always faces,

    but - if I do the above, the character always faces the object, but my joystick flight controls cause the player to move relative to the direction its facing instead of the direction of the world. I have a fixed camera andI just want the character to move along x and y in world space and either always face the center OR at the very least face the direction it is traveling - forward.

    I am not sure how to change the direction of the character without hurting my control scheme, any tips would be appreciated.

    Here is the code:



    Code (csharp):
    1.  
    2. //var blackhole : Transform.target;
    3. static var blackhole : Vector3 = Vector3.one;
    4. var blasterPrefab : GameObject; // The blaster prefab
    5.  
    6.  
    7.  
    8. @script RequireComponent( CharacterController )
    9.  
    10. var moveJoystick : Joystick;
    11. var rotateJoystick : Joystick;
    12.  
    13.  
    14. var forwardSpeed : float = 4;
    15. var backwardSpeed : float = 4;
    16. var sidestepSpeed : float = 4;                 
    17.  
    18. private var thisTransform : Transform;
    19. private var character : CharacterController;
    20.  
    21. private var velocity : Vector3;
    22.  
    23. function Start()
    24. {
    25.     // Cache component lookup at startup instead of doing this every frame     
    26.     thisTransform = GetComponent( Transform );
    27.     character = GetComponent( CharacterController );   
    28.  
    29.     // Move the character to the correct start position in the level, if one exists
    30.     var spawn = GameObject.Find( "PlayerSpawn" );
    31.     if ( spawn )
    32.         thisTransform.position = spawn.transform.position;
    33. }
    34.  
    35. function OnEndGame()
    36. {
    37.     // Disable joystick when the game ends 
    38.     moveJoystick.Disable();
    39.     rotateJoystick.Disable();  
    40.  
    41.     // Don't allow any more control changes when the game ends
    42.     this.enabled = false;
    43. }
    44.  
    45. function Update()
    46. {
    47.     //transform.LookAt(PlayerRelativeControl.blackhole, Vector3.forward);
    48.     var movement = thisTransform.TransformDirection( Vector3( 0, moveJoystick.position.y, moveJoystick.position.x ) );
    49.  
    50.     //Only x and y movement
    51.     movement.z = 0;
    52.     movement.Normalize();
    53.  
    54.  
    55.     // Apply movement from move joystick
    56.     var absJoyPos = Vector2( Mathf.Abs( moveJoystick.position.x ), Mathf.Abs( moveJoystick.position.y ) ); 
    57.     if ( absJoyPos.y > absJoyPos.x )
    58.     {
    59.         if ( moveJoystick.position.y > 0 )
    60.             movement *= forwardSpeed * absJoyPos.y;
    61.         else
    62.         {
    63.             movement *= backwardSpeed * absJoyPos.y;
    64.         }
    65.     }
    66.     if ( absJoyPos.x > absJoyPos.y )
    67.     {
    68.         if ( moveJoystick.position.x > 0 )
    69.             movement *= sidestepSpeed * absJoyPos.x;
    70.         else
    71.         {
    72.             movement *= sidestepSpeed * absJoyPos.x;
    73.         }
    74.     }
    75.         if ( rotateJoystick.tapCount == 2 )
    76.         {
    77.             var blaster : GameObject = Instantiate(blasterPrefab, GameObject.Find("blasterPoint").transform.position, Quaternion.identity);
    78.             // Propells blasted ammunition forward
    79.             //blaster.rigidbody.AddForce(blaster.transform.forward * 2000);
    80.             //end fire weapon phase                
    81.         }
    82.    
    83.     //velocity = character.velocity;   
    84.     movement += velocity;  
    85.     movement *= Time.deltaTime;
    86.    
    87.     // Actually move the character 
    88.     character.Move( movement );
    89.  
    90. }
     
    Last edited: Mar 21, 2011
  2. Loftless

    Loftless

    Joined:
    Mar 20, 2011
    Posts:
    16
    http://forum.unity3d.com/threads/82493-Face-forward-based-on-rigid-body-velocity

    following that would be my best advice, as far as facing direction that you're moving. If you wanted it to move more smoothly towards a direction, I would advise looking in the documentation at Quaternion.Slerp.

    Also, pertaining to looking at a specific object, the Transform.LookAt function is another good thing to look up.

    Hope that helps a bit.

    -Rob
     
unityunity