Search Unity

Be a Superman!

Discussion in 'Scripting' started by chclau, Apr 29, 2011.

  1. chclau

    chclau

    Joined:
    Apr 4, 2011
    Posts:
    20
    Hi,

    I am rather new at Unity.
    While learning and studying the available examples. I had this idea for a small challenge, specially suitable for beginners.

    Go to the island demo and change the FPSWalker script so you can fly around the island like Superman!

    Sadly, it took me a long time until I figured how to do that.
    But at the end I did it, so if anyone is interested I can put my solution.
     
  2. spaceMan-2.5

    spaceMan-2.5

    Joined:
    Oct 21, 2009
    Posts:
    710
    i´m sure there are many peoples interested in your code.. i like to see it.. !
     
  3. Scoppio

    Scoppio

    Joined:
    Feb 21, 2011
    Posts:
    46
    well, theres no meaning on openint a topic saying that you did it and not showing it right? :D
     
    SparrowGS likes this.
  4. chclau

    chclau

    Joined:
    Apr 4, 2011
    Posts:
    20
    Ok, sorry, here it is :)

    Following is the code that must replace the FPSWalk script on the Island demo so you can fly around the island

    Code (csharp):
    1.  
    2. // This script is used to fly in the island demo
    3. // Replace this script under the First Person Controller Prefab
    4. // You must assign the cam var to the Main Camera on FPS Controller Inspector
    5. //
    6. // The character flies in the direction he is looking at when you press "t"
    7. // When you release the key, the character decelerates and eventually stops on mid-air
    8.  
    9. // Acceleration, deceleration, maximum speed
    10. private static var acc = 0.5;
    11. private static var decel = 0.75;
    12. private static var max_speed = 25.0;   
    13.  
    14. var fly_speed = 0.0;
    15. public var cam : GameObject;
    16. private var moveDirection = Vector3.zero;
    17.  
    18. function FixedUpdate() {
    19.  
    20. // Our move direction is set to where the camera is looking at
    21.     moveDirection = cam.transform.forward;
    22.    
    23.     // Accelerate if "t" key is pressed, decelerate if not
    24.     if (Input.GetKey ("t")) {
    25.         fly_speed += acc;
    26.     }
    27.     else {
    28.         fly_speed -= decel;
    29.     }
    30.    
    31.     Debug.Log(fly_speed);
    32.    
    33.     // Clamp speed between 0 and maximum speed
    34.     fly_speed = Mathf.Clamp(fly_speed,0,max_speed);
    35.                        
    36.     // Move the controller
    37.     var controller : CharacterController = GetComponent(CharacterController);
    38.     var flags = controller.Move(moveDirection * fly_speed * Time.deltaTime);
    39.  
    40. }
    41.  
    42. @script RequireComponent(CharacterController)
    43.  
     
    Last edited: Apr 30, 2011
  5. metaldc4life

    metaldc4life

    Joined:
    Sep 26, 2014
    Posts:
    179
    Neat thanks!