Advertisement
ChrisTutorials

player.cs for Unity Kinematic Movement

Dec 26th, 2021
3,559
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.76 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 Player : 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.  
  20.     public void Start()
  21.     {
  22.         rb = GetComponent<Rigidbody2D>();
  23.     }
  24.  
  25.     public void FixedUpdate()
  26.     {
  27.        
  28.         // rb.MovePosition(rb.position + (moveInput * moveSpeed * Time.fixedDeltaTime));
  29.        
  30.        
  31.         // Try to move player in input direction, followed by left right and up down input if failed
  32.         bool success = MovePlayer(moveInput);
  33.        
  34.         if(!success)
  35.         {
  36.             // Try Left / Right
  37.             success = MovePlayer(new Vector2(moveInput.x, 0));
  38.  
  39.             if(!success)
  40.             {
  41.                 success = MovePlayer(new Vector2(0, moveInput.y));
  42.             }
  43.         }
  44.  
  45.     }
  46.  
  47.     // Tries to move the player in a direction by casting in that direction by the amount
  48.     // moved plus an offset. If no collisions are found, it moves the players
  49.     // Returns true or false depending on if a move was executed
  50.     public bool MovePlayer(Vector2 direction)
  51.     {
  52.         // Check for potential collisions
  53.         int count = rb.Cast(
  54.             direction, // X and Y values between -1 and 1 that represent the direction from the body to look for collisions
  55.             movementFilter, // The settings that determine where a collision can occur on such as layers to collide with
  56.             castCollisions, // List of collisions to store the found collisions into after the Cast is finished
  57.             moveSpeed * Time.fixedDeltaTime + collisionOffset); // The amount to cast equal to the movement plus an offset
  58.  
  59.         if (count == 0)
  60.         {
  61.             Vector2 moveVector = direction * moveSpeed * Time.fixedDeltaTime;
  62.  
  63.             // No collisions
  64.             rb.MovePosition(rb.position + moveVector);
  65.             return true;
  66.         }
  67.         else
  68.         {
  69.             // Print collisions
  70.             foreach (RaycastHit2D hit in castCollisions)
  71.             {
  72.                 print(hit.ToString());
  73.             }
  74.  
  75.             return false;
  76.         }
  77.     }
  78.    
  79.     public void OnMove(InputValue value)
  80.     {
  81.         moveInput = value.Get<Vector2>();
  82.     }
  83.  
  84.     public void OnFire()
  85.     {
  86.         print("Shots fired");
  87.     }
  88. }
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement