Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.21 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. using System.Collections;
  4.  
  5.  
  6.  
  7. [RequireComponent(typeof(CharacterController))]
  8.  
  9.  
  10.  
  11. public class MoveCharacter : MonoBehaviour {
  12.  
  13.     public CameraFollow cameraPrefab;
  14.  
  15.    
  16.  
  17.     CharacterController controller;
  18.  
  19.  
  20.  
  21.     public string characterName;
  22.  
  23.  
  24.  
  25.     public float speed = 6.0F;
  26.  
  27.     public float jumpSpeed = 8.0F;
  28.  
  29.     public float gravity = 20.0F;
  30.  
  31.  
  32.  
  33.     [HideInInspector]
  34.  
  35.     public int score;
  36.  
  37.  
  38.  
  39.     // if we are not moving should we be flipped or not?
  40.  
  41.     public bool preferFlipped = false;
  42.  
  43.  
  44.  
  45.     // input names
  46.  
  47.     public string horizontalAxisName = "Horizontal";
  48.  
  49.     public string verticalAxisName = "Vertical";
  50.  
  51.     public string jumpButtonName = "Jump";
  52.  
  53.  
  54.  
  55.     // input accessors.
  56.  
  57.  
  58.  
  59.     // this is a property which will get the input as a vector.
  60.  
  61.     // only call it once per update
  62.  
  63.     private Vector2 inputAxes
  64.  
  65.     {
  66.  
  67.         get
  68.  
  69.         {
  70.  
  71.             // whenever you query the value this gets called.
  72.  
  73.             // so thats why you don't want to call this willy nilly.
  74.  
  75.             return new Vector2(Input.GetAxis(horizontalAxisName), Input.GetAxis(verticalAxisName));
  76.  
  77.         }
  78.  
  79.     }
  80.  
  81.     // this is the same as above.
  82.  
  83.     private bool inputJumpButton
  84.  
  85.     {
  86.  
  87.         get
  88.  
  89.         {
  90.  
  91.             return Input.GetButton(jumpButtonName);
  92.  
  93.         }
  94.  
  95.     }
  96.  
  97.  
  98.  
  99.     // stored moveDirection
  100.  
  101.     private Vector3 moveVelocity = Vector3.zero;
  102.  
  103.  
  104.  
  105.     private Vector2 fixedInputLast, updateInputLast;
  106.  
  107.  
  108.  
  109.     // are we flipped?
  110.  
  111.     private bool __flipped__;// DO NOT ACCESS DIRECTLY
  112.  
  113.  
  114.  
  115.     private bool flipped
  116.  
  117.     {
  118.  
  119.         get
  120.  
  121.         {
  122.  
  123.             return __flipped__;
  124.  
  125.         }
  126.  
  127.  
  128.  
  129.         set
  130.  
  131.         {
  132.  
  133.             if ( value != __flipped__ )
  134.  
  135.             {
  136.  
  137.                 __flipped__ = value;
  138.  
  139.                 OnFlipped();
  140.  
  141.             }
  142.  
  143.         }
  144.  
  145.     }
  146.  
  147.  
  148.  
  149.     private float flipX { get { return flipped ? -1.0f : 1.0f; } }
  150.  
  151.  
  152.  
  153.     void Start()
  154.  
  155.     {
  156.  
  157.         // Dominic, this is similar to a constructor.
  158.  
  159.         // You never want to use constructors when your inheriting MonoBehavior
  160.  
  161.         // but this function is the second thing that gets called otherwise.
  162.  
  163.         // void Awake() is the first thing that gets called but you only should use it
  164.  
  165.         // if you absolutely need to get something prior to start.
  166.  
  167.  
  168.  
  169.         // getting the controller [Once] -- this is faster than using GetComponent every update.
  170.  
  171.        
  172.  
  173.         controller = GetComponent<CharacterController>();
  174.  
  175.  
  176.  
  177.         // and set us flipped to how we like
  178.  
  179.         flipped = preferFlipped;
  180.  
  181.     }
  182.  
  183.  
  184.  
  185.  
  186.  
  187.     // Dominic this was Update. Update is called for every frame which can variate in time.
  188.  
  189.     // Using FixedUpdate is for physics and is called at a fixed rate so that physics are solid.
  190.  
  191.     // so you should do physics in FixedUpdate and purely visual stuff in Update or LateUpdate
  192.  
  193.     void Update() {
  194.  
  195.  
  196.  
  197.         if ( controller.isGrounded )
  198.  
  199.         {
  200.  
  201.             // Get the input movement. (once)
  202.  
  203.             fixedInputLast = inputAxes;
  204.  
  205.  
  206.  
  207.             moveVelocity = transform.TransformDirection(
  208.  
  209.                 fixedInputLast.x ,
  210.  
  211.                 0,
  212.  
  213.                 fixedInputLast.y ).normalized * speed;
  214.  
  215.  
  216.  
  217.             if ( inputJumpButton )
  218.  
  219.                 moveVelocity.y += jumpSpeed;
  220.  
  221.         }
  222.  
  223.         else
  224.  
  225.         {
  226.  
  227.             // dominic, when in FixedUpdate instead of Time.deltaTime use Time.fixedDeltaTime
  228.  
  229.             moveVelocity.y -= gravity * Time.deltaTime;
  230.  
  231.         }
  232.  
  233.  
  234.  
  235.         controller.Move( moveVelocity * Time.deltaTime );      
  236.  
  237.     }
  238.  
  239.  
  240.  
  241.     // Dominic, i'm going to use the update function to control if the player is flipped or not.
  242.  
  243.     void LateUpdate()
  244.  
  245.     {
  246.  
  247.         // comparing our last moved x update to the last fixed we can consider if we need to turn
  248.  
  249.         float moveFixedSign = Mathf.Sign(fixedInputLast.x);
  250.  
  251.  
  252.  
  253.         float moveUpdateSign = Mathf.Sign(updateInputLast.x);
  254.  
  255.  
  256.  
  257.         if (moveFixedSign != moveUpdateSign)
  258.  
  259.         {
  260.  
  261.             // a direction change or stop occurred.
  262.  
  263.  
  264.  
  265.             if (moveFixedSign == 0.0f)
  266.  
  267.             {
  268.  
  269.                 // we've come to a stop. So we must use the preferred flip
  270.  
  271.                 flipped = preferFlipped;
  272.  
  273.             }
  274.  
  275.             else
  276.  
  277.             {
  278.  
  279.                 flipped = moveFixedSign > 0.0f;
  280.  
  281.             }
  282.  
  283.         }
  284.  
  285.  
  286.  
  287.         updateInputLast = fixedInputLast;// and store
  288.  
  289.  
  290.  
  291.     }
  292.  
  293.  
  294.  
  295.     void OnFlipped()
  296.  
  297.     {
  298.  
  299.         // get local scale.
  300.  
  301.         Vector3 localScale = transform.localScale;
  302.  
  303.  
  304.  
  305.         // flip it.
  306.  
  307.         localScale.x *= -1.0f;
  308.  
  309.  
  310.  
  311.         // set it
  312.  
  313.         transform.localScale = localScale;
  314.  
  315.     }
  316.  
  317.  
  318.  
  319.     public void RewardPoints(int points)
  320.  
  321.     {
  322.  
  323.         score += points;
  324.  
  325.     }
  326.  
  327.  
  328.  
  329.     void OnGUI()
  330.  
  331.     {
  332.  
  333.         GUI.Box(new Rect(0, 0, 196, 64), string.Format("{0}\nScore: {1}", characterName, score));
  334.  
  335.     }
  336.  
  337.  
  338.  
  339.     void OnTriggerEnter(Collider collider)
  340.  
  341.     {
  342.  
  343.         // tell it we want to collect.
  344.  
  345.         collider.SendMessage("Collect", this, SendMessageOptions.DontRequireReceiver);
  346.  
  347.     }
  348.  
  349.  
  350.  
  351. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement