Advertisement
ChrisTutorials

PlayerController with AnimationController Parameters\

Jan 9th, 2022
2,885
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.31 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.InputSystem;
  5.  
  6. // NOTE: The movement for this script uses the new InputSystem. The player needs to have a PlayerInput
  7. // component added and the Behaviour should be set to Send Messages so that the OnMove and OnFire methods
  8. // actually trigger
  9.  
  10. public class PlayerController : MonoBehaviour
  11. {
  12.     public float moveSpeed = 1f;
  13.     public float collisionOffset = 0.05f;
  14.     public ContactFilter2D movementFilter;
  15.  
  16.     private Vector2 moveInput;
  17.     private List<RaycastHit2D> castCollisions = new List<RaycastHit2D>();
  18.     private Rigidbody2D rb;
  19.     private Animator animator;
  20.  
  21.     public void Start()
  22.     {
  23.         rb = GetComponent<Rigidbody2D>();
  24.         animator = GetComponent<Animator>();
  25.     }
  26.  
  27.     public void FixedUpdate()
  28.     {
  29.        
  30.         // rb.MovePosition(rb.position + (moveInput * moveSpeed * Time.fixedDeltaTime));
  31.        
  32.         if(moveInput != Vector2.zero){
  33.             // Try to move player in input direction, followed by left right and up down input if failed
  34.             bool success = MovePlayer(moveInput);
  35.            
  36.             if(!success)
  37.             {
  38.                 // Try Left / Right
  39.                 success = MovePlayer(new Vector2(moveInput.x, 0));
  40.  
  41.                 if(!success)
  42.                 {
  43.                     success = MovePlayer(new Vector2(0, moveInput.y));
  44.                 }
  45.             }
  46.  
  47.             animator.SetBool("isMoving", success);
  48.         }
  49.         else{
  50.             animator.SetBool("isMoving", false);
  51.         }
  52.        
  53.  
  54.     }
  55.  
  56.     // Tries to move the player in a direction by casting in that direction by the amount
  57.     // moved plus an offset. If no collisions are found, it moves the players
  58.     // Returns true or false depending on if a move was executed
  59.     public bool MovePlayer(Vector2 direction)
  60.     {
  61.         // Check for potential collisions
  62.         int count = rb.Cast(
  63.             direction, // X and Y values between -1 and 1 that represent the direction from the body to look for collisions
  64.             movementFilter, // The settings that determine where a collision can occur on such as layers to collide with
  65.             castCollisions, // List of collisions to store the found collisions into after the Cast is finished
  66.             moveSpeed * Time.fixedDeltaTime + collisionOffset); // The amount to cast equal to the movement plus an offset
  67.  
  68.         if (count == 0)
  69.         {
  70.             Vector2 moveVector = direction * moveSpeed * Time.fixedDeltaTime;
  71.  
  72.             // No collisions
  73.             rb.MovePosition(rb.position + moveVector);
  74.             return true;
  75.         }
  76.         else
  77.         {
  78.             // Print collisions
  79.             foreach (RaycastHit2D hit in castCollisions)
  80.             {
  81.                 print(hit.ToString());
  82.             }
  83.  
  84.             return false;
  85.         }
  86.     }
  87.    
  88.     public void OnMove(InputValue value)
  89.     {
  90.         moveInput = value.Get<Vector2>();
  91.  
  92.         // Only set the animation direction if the player is trying to move
  93.         if(moveInput != Vector2.zero) {
  94.             animator.SetFloat("XInput", moveInput.x);
  95.             animator.SetFloat("YInput", moveInput.y);
  96.         }
  97.     }
  98.  
  99.     public void OnFire()
  100.     {
  101.         print("Shots fired");
  102.     }
  103. }
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement