- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- [RequireComponent(typeof(Rigidbody2D))]
- public class SimpleCharacterController : MonoBehaviour
- {
- public float moveSpeed = 70;
- public float m_MovementSmoothing = 0.1f;
- public bool normalizedMovement = true;
- public GameObject upObject;
- public GameObject leftObject;
- public GameObject rightObject;
- public GameObject downObject;
- Rigidbody2D rb;
- Animator currentAnimator;
- enum Direction { Up, Right, Down, Left };
- enum Expression { Neutral, Angry, Smile, Surprised };
- Direction currentDirection;
- Direction previousDirection;
- float angle = 180;
- float speed;
- Vector2 axisVector = Vector2.zero;
- Vector3 currentVelocity = Vector3.zero;
- Animator downAnimator;
- Animator upAnimator;
- Animator rightAnimator;
- Animator leftAnimator;
- void Start()
- {
- rb = GetComponent<Rigidbody2D>();
- upObject.SetActive(false);
- leftObject.SetActive(false);
- rightObject.SetActive(false);
- downObject.SetActive(true);
- downAnimator = downObject.GetComponent<Animator>();
- upAnimator = upObject.GetComponent<Animator>();
- rightAnimator = rightObject.GetComponent<Animator>();
- leftAnimator = leftObject.GetComponent<Animator>();
- currentAnimator = downAnimator;
- }
- void Update()
- {
-
- speed = rb.velocity.magnitude;
-
- axisVector = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
-
- if (normalizedMovement == true)
- {
- axisVector.Normalize();
- }
-
-
-
- if (!(axisVector.x == 0 && axisVector.y == 0))
- {
-
- angle = Mathf.Atan2(axisVector.x, axisVector.y) * Mathf.Rad2Deg;
-
- angle = Mathf.RoundToInt(angle);
- }
- if (angle > -45 && angle < 45)
- {
- currentDirection = Direction.Up;
- }
- else if (angle < -135 || angle > 135)
- {
- currentDirection = Direction.Down;
- }
- else if (angle >= 45 && angle <= 135)
- {
- currentDirection = Direction.Right;
- }
- else if (angle <= -45 && angle >= -135)
- {
- currentDirection = Direction.Left;
- }
-
- if (previousDirection != currentDirection)
- {
- if (currentDirection == Direction.Up)
- {
-
- upObject.SetActive(true);
- rightObject.SetActive(false);
- leftObject.SetActive(false);
- downObject.SetActive(false);
- currentAnimator = upAnimator;
- }
- else if (currentDirection == Direction.Down)
- {
-
- upObject.SetActive(false);
- rightObject.SetActive(false);
- leftObject.SetActive(false);
- downObject.SetActive(true);
- currentAnimator = downAnimator;
- }
- else if (currentDirection == Direction.Right)
- {
-
- upObject.SetActive(false);
- rightObject.SetActive(true);
- leftObject.SetActive(false);
- downObject.SetActive(false);
- currentAnimator = rightAnimator;
- }
- else if (currentDirection == Direction.Left)
- {
-
- upObject.SetActive(false);
- rightObject.SetActive(false);
- leftObject.SetActive(true);
- downObject.SetActive(false);
- currentAnimator = leftAnimator;
- }
- }
-
- currentAnimator.SetFloat("Speed", speed);
-
- previousDirection = currentDirection;
-
-
- if (Input.GetKey(KeyCode.Space))
- {
- PlayAnimation("Swing");
- }
- else if (Input.GetKey(KeyCode.C))
- {
- PlayAnimation("Thrust");
- }
- else if (Input.GetKey(KeyCode.X))
- {
- PlayAnimation("Bow");
- }
- else if (Input.GetKey(KeyCode.V))
- {
- SetExpression(Expression.Neutral);
- }
- else if (Input.GetKey(KeyCode.B))
- {
- SetExpression(Expression.Angry);
- }
- }
- void FixedUpdate()
- {
-
- Move();
- }
- void PlayAnimation(string animationName)
- {
-
- currentAnimator.Play(animationName, 0);
- }
- void SetExpression(Expression expressionToSet)
- {
-
- int expressionNumber = (int)expressionToSet;
-
- if (!(currentDirection == Direction.Up))
- {
- currentAnimator.SetInteger("Expression", expressionNumber);
- }
- }
- void Move()
- {
-
- Vector2 targetVelocity = new Vector2(axisVector.x * moveSpeed * 10f, axisVector.y * moveSpeed * 10) * Time.fixedDeltaTime;
-
- rb.velocity = Vector3.SmoothDamp(rb.velocity, targetVelocity, ref currentVelocity, m_MovementSmoothing);
- }
- }