Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- [RequireComponent(typeof(CharacterController))]
- public class MoveCharacter : MonoBehaviour {
- public CameraFollow cameraPrefab;
- CharacterController controller;
- public string characterName;
- public float speed = 6.0F;
- public float jumpSpeed = 8.0F;
- public float gravity = 20.0F;
- [HideInInspector]
- public int score;
- // if we are not moving should we be flipped or not?
- public bool preferFlipped = false;
- // input names
- public string horizontalAxisName = "Horizontal";
- public string verticalAxisName = "Vertical";
- public string jumpButtonName = "Jump";
- // input accessors.
- // this is a property which will get the input as a vector.
- // only call it once per update
- private Vector2 inputAxes
- {
- get
- {
- // whenever you query the value this gets called.
- // so thats why you don't want to call this willy nilly.
- return new Vector2(Input.GetAxis(horizontalAxisName), Input.GetAxis(verticalAxisName));
- }
- }
- // this is the same as above.
- private bool inputJumpButton
- {
- get
- {
- return Input.GetButton(jumpButtonName);
- }
- }
- // stored moveDirection
- private Vector3 moveVelocity = Vector3.zero;
- private Vector2 fixedInputLast, updateInputLast;
- // are we flipped?
- private bool __flipped__;// DO NOT ACCESS DIRECTLY
- private bool flipped
- {
- get
- {
- return __flipped__;
- }
- set
- {
- if ( value != __flipped__ )
- {
- __flipped__ = value;
- OnFlipped();
- }
- }
- }
- private float flipX { get { return flipped ? -1.0f : 1.0f; } }
- void Start()
- {
- // Dominic, this is similar to a constructor.
- // You never want to use constructors when your inheriting MonoBehavior
- // but this function is the second thing that gets called otherwise.
- // void Awake() is the first thing that gets called but you only should use it
- // if you absolutely need to get something prior to start.
- // getting the controller [Once] -- this is faster than using GetComponent every update.
- controller = GetComponent<CharacterController>();
- // and set us flipped to how we like
- flipped = preferFlipped;
- }
- // Dominic this was Update. Update is called for every frame which can variate in time.
- // Using FixedUpdate is for physics and is called at a fixed rate so that physics are solid.
- // so you should do physics in FixedUpdate and purely visual stuff in Update or LateUpdate
- void Update() {
- if ( controller.isGrounded )
- {
- // Get the input movement. (once)
- fixedInputLast = inputAxes;
- moveVelocity = transform.TransformDirection(
- fixedInputLast.x ,
- 0,
- fixedInputLast.y ).normalized * speed;
- if ( inputJumpButton )
- moveVelocity.y += jumpSpeed;
- }
- else
- {
- // dominic, when in FixedUpdate instead of Time.deltaTime use Time.fixedDeltaTime
- moveVelocity.y -= gravity * Time.deltaTime;
- }
- controller.Move( moveVelocity * Time.deltaTime );
- }
- // Dominic, i'm going to use the update function to control if the player is flipped or not.
- void LateUpdate()
- {
- // comparing our last moved x update to the last fixed we can consider if we need to turn
- float moveFixedSign = Mathf.Sign(fixedInputLast.x);
- float moveUpdateSign = Mathf.Sign(updateInputLast.x);
- if (moveFixedSign != moveUpdateSign)
- {
- // a direction change or stop occurred.
- if (moveFixedSign == 0.0f)
- {
- // we've come to a stop. So we must use the preferred flip
- flipped = preferFlipped;
- }
- else
- {
- flipped = moveFixedSign > 0.0f;
- }
- }
- updateInputLast = fixedInputLast;// and store
- }
- void OnFlipped()
- {
- // get local scale.
- Vector3 localScale = transform.localScale;
- // flip it.
- localScale.x *= -1.0f;
- // set it
- transform.localScale = localScale;
- }
- public void RewardPoints(int points)
- {
- score += points;
- }
- void OnGUI()
- {
- GUI.Box(new Rect(0, 0, 196, 64), string.Format("{0}\nScore: {1}", characterName, score));
- }
- void OnTriggerEnter(Collider collider)
- {
- // tell it we want to collect.
- collider.SendMessage("Collect", this, SendMessageOptions.DontRequireReceiver);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement