Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This script is used to control the players movement on the X and Y axes.
- using UnityEngine;
- using System.Collections;
- public class PlayerController2d2 : MonoBehaviour {
- /* Public variables which can be modified */
- // Allows us to set the player GameObject from the Inspector
- public GameObject playerGameObject;
- // Allows us to set the walking speed from the Inspector. A default value of 5 is assigned.
- public float walkingSpeed = 5;
- // Allows us to set the running speed from the Inspector. A default value of 10 is assigned.
- public float runningSpeed = 10;
- /* Private variables which can NOT be modified */
- // Private variables used to calculate the current speed and maximum speed
- private float currentSpeed;
- private float maxSpeed;
- // Private variables used in the script to determine which direction the player GameObject is moving
- private float velocityX;
- private float velocityY;
- // FixedUpdate is used to perform physics based actions
- void FixedUpdate()
- {
- // Determine if the Run button is being held down and if so sets the current speed to the runspeed
- // if the Run button is not being held down the current speed defaults to the walking speed
- // Make sure you define a Run button in the Input inspector!
- currentSpeed = (Input.GetButton("Run")) ? runningSpeed : walkingSpeed;
- // Move the player based on the Input Axis either Horizontally or Vertically
- playerGameObject.rigidbody2D.velocity = new Vector2(Mathf.Lerp(0, Input.GetAxis("Horizontal") * currentSpeed, 0.8f),
- Mathf.Lerp(0, Input.GetAxis("Vertical") * currentSpeed, 0.8f));
- // Determine which direction the player is being moved to determine which direction
- // the player GameObject needs to be rotated
- velocityX = Input.GetAxisRaw("Horizontal");
- velocityY = Input.GetAxisRaw("Vertical");
- // Rotate the player GameObject based on which way the player is being moved
- if (velocityX != 0 && velocityY != 0)
- {
- if (velocityX > 0 && velocityY > 0)
- {
- playerGameObject.transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, 135f));
- }
- else if (velocityX < 0 && velocityY < 0)
- {
- playerGameObject.transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, 315f));
- }
- else if (velocityX > 0 && velocityY < 0)
- {
- playerGameObject.transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, 45f));
- }
- else if (velocityX < 0 && velocityY > 0)
- {
- playerGameObject.transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, 225f));
- }
- }
- else if (velocityX != 0 && velocityX > 0)
- {
- playerGameObject.transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, 90f));
- }
- else if (velocityX != 0 && velocityX < 0)
- {
- playerGameObject.transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, 270f));
- }
- else if (velocityY != 0 && velocityY > 0)
- {
- playerGameObject.transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, 180f));
- }
- else if (velocityY != 0 && velocityY < 0)
- {
- playerGameObject.transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, 0f));
- }
- // Make sure that our player isn't moving faster than the running speed we have
- // defined in the Inspector
- if (currentSpeed > runningSpeed)
- {
- currentSpeed = runningSpeed;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment