Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class Controls : MonoBehaviour
- {
- public PackedSprite playerSprite; // assign your sprite to this in Unity
- public float speed = 6.0F;
- // public float jumpSpeed = 8.0F;
- public float gravity = 20.0F;
- private Vector3 moveDirection = Vector3.zero;
- void Update()
- {
- CharacterController controller = GetComponent<CharacterController>();
- if (controller.isGrounded)
- {
- moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
- moveDirection = transform.TransformDirection(moveDirection);
- moveDirection *= speed;
- // if (Input.GetButton("Jump"))
- // moveDirection.y = jumpSpeed;
- }
- moveDirection.y -= gravity * Time.deltaTime;
- controller.Move(moveDirection * Time.deltaTime);
- DetermineCurrentMoveDirection();
- }
- public void DetermineCurrentMoveDirection()
- {
- var forward = false;
- var backward = false;
- var left = false;
- var right = false;
- // move right
- if (moveDirection.x < 0.01f)
- {
- right = true;
- }
- // move left
- else if(moveDirection.x > -0.01f)
- {
- left = true;
- }
- // move up
- else if(moveDirection.z > 0.01f)
- {
- forward = true;
- }
- // move down
- else if(moveDirection.z < -0.01f)
- {
- backward = true;
- }
- else
- {
- forward = false;
- backward = false;
- left = false;
- right = false;
- }
- // move right
- if (right == true)
- {
- playerSprite.PlayAnim(3);
- }
- // move left
- if (left == true)
- {
- playerSprite.PlayAnim(2);
- }
- // move up
- if (forward == true)
- {
- playerSprite.PlayAnim(0);
- }
- // move down
- if (backward == true)
- {
- playerSprite.PlayAnim(1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement