Advertisement
LeeMace

Player Movement with Physics Engine

Aug 23rd, 2023
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.20 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5.  
  6. //player movement using the physics engine from Ferrone Learning Unity book
  7. public class PlayerBehaviour : MonoBehaviour
  8. {
  9.  
  10.     public float moveSpeed = 10;
  11.     public float rotateSpeed = 75;
  12.     private float horizontalInput;
  13.     private float verticalInput;
  14.  
  15.     private Rigidbody playerRB;
  16.     void Start()
  17.     {
  18.         playerRB = GetComponent<Rigidbody>();
  19.     }
  20.  
  21.     void Update() {
  22.  
  23.         PlayerMovement();
  24.     }
  25.  
  26.     private void PlayerMovement() {
  27.  
  28.         verticalInput = Input.GetAxis("Vertical") * moveSpeed;
  29.         horizontalInput = Input.GetAxis("Horizontal") * rotateSpeed;
  30.        /* transform.Translate(Vector3.forward * verticalInput * Time.deltaTime);
  31.         transform.Rotate(Vector3.up * horizontalInput * Time.deltaTime);*/
  32.     }
  33.  
  34.     private void FixedUpdate() {
  35.        
  36.         Vector3 rotation = Vector3.up * horizontalInput;
  37.         Quaternion angleRot = Quaternion.Euler(rotation * Time.fixedDeltaTime);
  38.         playerRB.MovePosition(transform.position + transform.forward * verticalInput * Time.fixedDeltaTime);
  39.         playerRB.MoveRotation(playerRB.rotation * angleRot);
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement