Advertisement
Munchy2007

PlayerNetworkMove

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