1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | using UnityEngine; using System.Collections; using UnityEngine.Networking; public class TankMovement : NetworkBehaviour { public float moveSpeed; public float turnSpeed; private Rigidbody rb; private float movementInputValue; private float turnInputValue; // ★ private GameObject tankBody; public override void OnStartLocalPlayer(){ // ★ tankBody = transform.FindChild( "Body" ).gameObject; tankBody.GetComponent<MeshRenderer>().material.color = Color.red; } void Start () { rb = GetComponent<Rigidbody>(); } void Update () { movementInputValue = Input.GetAxis( "Vertical" ); turnInputValue = Input.GetAxis( "Horizontal" ); } void FixedUpdate(){ if (!isLocalPlayer) return ; Move(); Turn(); } void Move(){ Vector3 movement = transform.forward * movementInputValue * moveSpeed * Time.deltaTime; rb.MovePosition(rb.position + movement); } void Turn(){ float turn = turnInputValue * turnSpeed * Time.deltaTime; Quaternion turnRotation = Quaternion.Euler(0f, turn, 0f); rb.MoveRotation(rb.rotation * turnRotation); } } |