Advertisement
Munchy2007

PlayerNetworkMove - NoNetworkBits

Feb 12th, 2016
4,442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.91 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class PlayerNetworkMove : MonoBehaviour {
  5.     Transform trans; // Used to cache the transform
  6.     Rigidbody body; // Used to cache the RigidBody
  7.     float hInput;
  8.     float vInput;
  9.     float rotationSpeed = 90f;
  10.  
  11.     void Awake()
  12.     {
  13.         trans = transform;
  14.         body = GetComponent<Rigidbody>();
  15.     }
  16.  
  17.     // Update is called once per frame
  18.     void Update () {
  19.  
  20.         hInput = Input.GetAxis("Horizontal");
  21.         vInput = Input.GetAxis("Vertical");
  22.     }
  23.  
  24.     void FixedUpdate()
  25.     {
  26.         // Remove unwanted forces resulting from collisions
  27.         body.velocity = Vector3.zero;
  28.         body.angularVelocity = Vector3.zero;
  29.  
  30.         // Handle rotation
  31.         var rot = body.rotation.eulerAngles;
  32.         rot.y += hInput * rotationSpeed * Time.deltaTime;
  33.         body.MoveRotation(Quaternion.Euler(rot));
  34.  
  35.         // Handle forward movement
  36.         body.MovePosition(body.position + vInput * trans.forward * 5f * Time.deltaTime);
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement