Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class AssassinMovement : MonoBehaviour
- {
- //This script will recreate Assassin's creed like movement
- public float Speed = 4f;
- public float Rotation = 7f;
- bool PlayerMoving = false;
- Vector3 MoveInput = Vector3.zero;
- Vector3 MovementVector = Vector3.zero;
- Camera MainCam;
- Rigidbody Body;
- void Start()
- {
- //This is the easy way to grab the camera with the main tag
- MainCam = Camera.main;
- //Grab the body so we can use it
- Body = this.GetComponent<Rigidbody>();
- }
- void Update() //0-60 times per second
- {
- //Input should be captured during update, this makes the game feel responsive
- MoveInput = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
- //Because it an Assassin's creed like movement, we want the character to look in the last direction it moved
- PlayerMoving = false;
- if (Input.GetButton("Horizontal") || Input.GetButton("Vertical"))// The || symbol means OR
- {
- //This way the player will only move and rotate while atleast one key is down
- PlayerMoving = true;
- }
- }
- private void FixedUpdate() // 50 (or what ever you set it) times per second, no matter what
- {
- //Physics should be updated during the Fixed step
- //We use the up vector to flatten the camera direction, to make it move straight
- Vector3 MoveDirection = Vector3.ProjectOnPlane(MainCam.transform.forward, Vector3.up);
- //A Quaternion * Vector will return the Vector rotated by the Quaternion, a usefull way to rotate vectors
- Vector3 AdjustedRotation = Quaternion.LookRotation(MoveDirection) * MoveInput;
- //If the player is pressing a movement key move, and move input is not zero
- if (PlayerMoving && MoveInput != Vector3.zero)// && symbol means AND
- {
- //Rotate towards the moving direction
- this.transform.rotation =
- Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(AdjustedRotation), Time.deltaTime * Rotation);
- Body.velocity = this.transform.forward * (Time.deltaTime * Speed);
- }
- else//stand still and don't rotate
- {
- Body.velocity = Vector3.zero;
- }
- }
- }
Add Comment
Please, Sign In to add comment