MysteryGM

Unity_Assassin'sCreedLikeMovement

Jul 14th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.40 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class AssassinMovement : MonoBehaviour
  6. {
  7.     //This script will recreate Assassin's creed like movement
  8.     public float Speed = 4f;
  9.     public float Rotation = 7f;
  10.  
  11.     bool PlayerMoving = false;
  12.     Vector3 MoveInput = Vector3.zero;
  13.     Vector3 MovementVector = Vector3.zero;
  14.     Camera MainCam;
  15.     Rigidbody Body;
  16.  
  17.     void Start()
  18.     {
  19.         //This is the easy way to grab the camera with the main tag
  20.         MainCam = Camera.main;
  21.         //Grab the body so we can use it
  22.         Body = this.GetComponent<Rigidbody>();
  23.     }
  24.  
  25.  
  26.     void Update() //0-60 times per second
  27.     {
  28.         //Input should be captured during update, this makes the game feel responsive
  29.         MoveInput = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
  30.         //Because it an Assassin's creed like movement, we want the character to look in the last direction it moved
  31.         PlayerMoving = false;
  32.         if (Input.GetButton("Horizontal") || Input.GetButton("Vertical"))// The || symbol means OR
  33.         {
  34.             //This way the player will only move and rotate while atleast one key is down
  35.             PlayerMoving = true;
  36.         }
  37.     }
  38.  
  39.     private void FixedUpdate() // 50 (or what ever you set it) times per second, no matter what
  40.     {
  41.         //Physics should be updated during the Fixed step
  42.  
  43.         //We use the up vector to flatten the camera direction, to make it move straight
  44.         Vector3 MoveDirection = Vector3.ProjectOnPlane(MainCam.transform.forward, Vector3.up);
  45.         //A Quaternion * Vector will return the Vector rotated by the Quaternion, a usefull way to rotate vectors
  46.         Vector3 AdjustedRotation = Quaternion.LookRotation(MoveDirection) * MoveInput;
  47.  
  48.         //If the player is pressing a movement key move, and move input is not zero
  49.         if (PlayerMoving && MoveInput != Vector3.zero)// && symbol means AND
  50.         {
  51.             //Rotate towards the moving direction
  52.             this.transform.rotation =
  53.                 Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(AdjustedRotation), Time.deltaTime * Rotation);
  54.  
  55.             Body.velocity = this.transform.forward * (Time.deltaTime * Speed);
  56.         }
  57.         else//stand still and don't rotate
  58.         {
  59.             Body.velocity = Vector3.zero;
  60.         }
  61.  
  62.        
  63.     }
  64. }
Add Comment
Please, Sign In to add comment