Guest User

Top Down Controller Unity

a guest
Dec 7th, 2014
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.94 KB | None | 0 0
  1. // This script is used to control the players movement on the X and Y axes.
  2. using UnityEngine;
  3. using System.Collections;
  4.  
  5. public class PlayerController2d2 : MonoBehaviour {
  6.  
  7.     /* Public variables which can be modified */
  8.  
  9.     // Allows us to set the player GameObject from the Inspector
  10.     public GameObject playerGameObject;
  11.     // Allows us to set the walking speed from the Inspector. A default value of 5 is assigned.
  12.     public float walkingSpeed = 5;
  13.     // Allows us to set the running speed from the Inspector. A default value of 10 is assigned.
  14.     public float runningSpeed = 10;
  15.    
  16.    
  17.     /* Private variables which can NOT be modified */
  18.  
  19.     // Private variables used to calculate the current speed and maximum speed
  20.     private float currentSpeed;
  21.     private float maxSpeed;
  22.     // Private variables used in the script to determine which direction the player GameObject is moving
  23.     private float velocityX;
  24.     private float velocityY;
  25.  
  26.    
  27.     // FixedUpdate is used to perform physics based actions
  28.     void FixedUpdate()
  29.     {
  30.                
  31.         // Determine if the Run button is being held down and if so sets the current speed to the runspeed
  32.         // if the Run button is not being held down the current speed defaults to the walking speed
  33.         // Make sure you define a Run button in the Input inspector!
  34.         currentSpeed = (Input.GetButton("Run")) ? runningSpeed : walkingSpeed;
  35.        
  36.  
  37.         // Move the player based on the Input Axis either Horizontally or Vertically
  38.         playerGameObject.rigidbody2D.velocity = new Vector2(Mathf.Lerp(0, Input.GetAxis("Horizontal") * currentSpeed, 0.8f),
  39.                                            Mathf.Lerp(0, Input.GetAxis("Vertical") * currentSpeed, 0.8f));
  40.  
  41.  
  42.             // Determine which direction the player is being moved to determine which direction
  43.             // the player GameObject needs to be rotated
  44.             velocityX = Input.GetAxisRaw("Horizontal");
  45.             velocityY = Input.GetAxisRaw("Vertical");
  46.  
  47.             // Rotate the player GameObject based on which way the player is being moved
  48.             if (velocityX != 0 && velocityY != 0)
  49.             {
  50.                 if (velocityX > 0 && velocityY > 0)
  51.                 {
  52.                     playerGameObject.transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, 135f));
  53.                 }
  54.                 else if (velocityX < 0 && velocityY < 0)
  55.                 {
  56.                     playerGameObject.transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, 315f));
  57.                 }
  58.                 else if (velocityX > 0 && velocityY < 0)
  59.                 {
  60.                     playerGameObject.transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, 45f));
  61.                 }
  62.                 else if (velocityX < 0 && velocityY > 0)
  63.                 {
  64.                     playerGameObject.transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, 225f));
  65.                 }
  66.             }
  67.             else if (velocityX != 0 && velocityX > 0)
  68.             {
  69.                 playerGameObject.transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, 90f));
  70.             }
  71.             else if (velocityX != 0 && velocityX < 0)
  72.             {
  73.                 playerGameObject.transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, 270f));
  74.             }
  75.             else if (velocityY != 0 && velocityY > 0)
  76.             {
  77.                 playerGameObject.transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, 180f));
  78.             }
  79.             else if (velocityY != 0 && velocityY < 0)
  80.             {
  81.                 playerGameObject.transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, 0f));
  82.             }
  83.  
  84.             // Make sure that our player isn't moving faster than the running speed we have
  85.             // defined in the Inspector
  86.             if (currentSpeed > runningSpeed)
  87.             {
  88.                 currentSpeed = runningSpeed;
  89.             }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment