Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct here to familiarize yourself with the rules and how to post constructively.

  2. Unity 2022.1 is now available as the latest Tech release.
    Dismiss Notice
  3. Improve your project's performance with our new guide on profiling in Unity.
    Dismiss Notice

help me check my code

Discussion in 'Scripting' started by vmp, Dec 11, 2010.

  1. vmp

    vmp

    Joined:
    Mar 13, 2006
    Posts:
    23
    i am just building up some code for a character controller for platform 2.5d, its only has left right direction and walk movement so far,but i am an artist not a coder so i am looking for someone who maybe able to look at what i have so far and give me some ideas on how it could be improved

    Code (csharp):
    1. var Speed : float = 4;
    2. var gravity : float = 20.0;
    3.  
    4. var curDir : int = 2;
    5. var lasDir : int = 2;
    6. var inUse =0;
    7.  
    8. private var movement = Vector3.zero;
    9. private var horizDir : float = 0;
    10. private var thisTransform : Transform;
    11. private var character : CharacterController;
    12.  
    13. function Start()
    14. {
    15.     thisTransform = GetComponent( Transform );
    16.     character = GetComponent( CharacterController );   
    17. }
    18.  
    19. function Update()
    20. {
    21.     charDirection();
    22.     //  move the character 
    23.     character.Move( movement );
    24. }
    25.  
    26. function charDirection()
    27. {
    28.  
    29. horizDir = Input.GetAxis("Horizontal");
    30.  
    31. if (horizDir < 0)
    32.     curDir = -1;
    33. else if (horizDir >0)
    34.     curDir = 1;
    35. else if (horizDir == 0)
    36.     curDir = 0;
    37.    
    38. if (curDir != lasDir)
    39. {
    40.     if (horizDir > 0) // going right
    41.         {
    42.         transform.rotation = Quaternion.LookRotation(Vector3.forward);
    43.         }  
    44.     else if (horizDir <0) // going left
    45.         {
    46.         transform.rotation = Quaternion.LookRotation(-Vector3.forward);
    47.         }
    48.     else // last direction
    49.         {
    50.    
    51.         }
    52. lasDir = curDir;
    53. }
    54.  
    55. movement = Vector3.right * Speed * horizDir;       
    56. movement += Physics.gravity;
    57. movement *= Time.deltaTime;
    58.    
    59. }
    60.  
     
unityunity