Advertisement
plattina

3D Basic Move

Jul 3rd, 2019
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.06 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Player : MonoBehaviour
  6. {
  7.     [SerializeField] float _speedMove, _speedRotation;
  8.     private float _moveFoward, _moveRotation;
  9.     private Rigidbody _rigidbody;
  10.  
  11.     // Start is called before the first frame update
  12.     void Start()
  13.     {
  14.         _rigidbody = GetComponent<Rigidbody>();
  15.     }
  16.  
  17.     // Update is called once per frame
  18.     void Update()
  19.     {
  20.         _moveFoward = Input.GetAxis("Vertical");
  21.         _moveRotation = Input.GetAxis("Horizontal");
  22.     }
  23.  
  24.     void FixedUpdate()
  25.     {
  26.         Move();
  27.         Turn();
  28.     }
  29.  
  30.     void Move()
  31.     {
  32.         Vector3 movement = transform.forward * _moveFoward * _speedMove * Time.deltaTime;
  33.         _rigidbody.MovePosition(_rigidbody.position + movement);
  34.     }
  35.  
  36.     void Turn()
  37.     {
  38.         float turn = _moveRotation * _speedRotation * Time.deltaTime;
  39.         Quaternion turnRotation = Quaternion.Euler(0f, turn, 0f);
  40.         _rigidbody.MoveRotation(_rigidbody.rotation * turnRotation);
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement