Observer Movement Script (Free Fly)

Observer Movement Script (Free Fly)

same movement as Unity editor control.

observer

I just tried to use those standard movement script from unity in my project.
and this one support NetworkIdentity (optional)

ObserverMovement.cs

using UnityEngine;
using UnityEngine.Networking;

public class ObserverMovement : MonoBehaviour
{
	[Header("Common")]
	public float m_Speed = 0.5f;
	public float m_HighSpeed = 2f;
	public bool m_AsFlightControl = false;

	[Header("Optional")]
	public NetworkIdentity m_NetworkIdentity;
	public Camera m_Camera;

	private float m_CurrentSpeed = 0f;
	private bool m_ToggleSpeed = false;
	private Vector3 m_LocalTranslate = Vector3.zero;
	private Vector2 m_LocalLook = Vector2.zero;

	void OnValidate()
	{
		if (m_NetworkIdentity == null)
		{
			m_NetworkIdentity = GetComponent<NetworkIdentity>();
		}

		if (m_NetworkIdentity != null && m_Camera == null)
		{
			m_Camera = GetComponentInChildren<Camera>(true);
		}
	}

	void InputUpdate()
	{
		// Mouse look
		if (Input.GetMouseButtonDown(1) && Cursor.lockState != CursorLockMode.Locked)
		{
			Cursor.lockState = CursorLockMode.Locked;
		}
		if (Input.GetMouseButtonUp(1) && Cursor.lockState != CursorLockMode.None)
		{
			Cursor.lockState = CursorLockMode.None;
		}

		if (Cursor.lockState == CursorLockMode.Locked)
		{
			m_LocalLook.x = -Input.GetAxis("Mouse Y");
			m_LocalLook.y = Input.GetAxis("Mouse X");
		}

		// Speed
		if (!m_ToggleSpeed &&
			Input.GetKeyDown(KeyCode.LeftShift) || Input.GetKeyDown(KeyCode.RightShift))
		{
			m_ToggleSpeed = true;
		}
		else if (m_ToggleSpeed &&
			(Input.GetKeyUp(KeyCode.LeftShift) || Input.GetKeyUp(KeyCode.RightShift)) &&
			!(Input.GetKey(KeyCode.LeftShift) && Input.GetKey(KeyCode.RightShift)))
		{
			m_ToggleSpeed = false;
		}
		m_CurrentSpeed = (m_ToggleSpeed) ? m_HighSpeed : m_Speed;

		// Movement
		if (Input.GetAxis("Vertical") != 0)
		{
			m_LocalTranslate += transform.forward * m_CurrentSpeed * Input.GetAxis("Vertical");
		}
		if (Input.GetAxis("Horizontal") != 0)
		{
			m_LocalTranslate += transform.right * m_CurrentSpeed * Input.GetAxis("Horizontal");
		}

		if (Input.GetKey(KeyCode.E) && !Input.GetKey(KeyCode.Q))
		{
			m_LocalTranslate += transform.up * m_CurrentSpeed * 0.5f;
		}
		if (Input.GetKey(KeyCode.Q) && !Input.GetKey(KeyCode.E))
		{
			m_LocalTranslate += -transform.up * m_CurrentSpeed * 0.5f;
		}
	}

	void Update()
	{
		if(m_NetworkIdentity != null && !m_NetworkIdentity.isLocalPlayer)
		{
			if (m_Camera != null)
				m_Camera.enabled = false;
			enabled = false;
			return;
		}

		InputUpdate();

		// Apply
		transform.Translate(m_LocalTranslate, Space.World);
		if (Cursor.lockState == CursorLockMode.Locked)
		{
			transform.Rotate(Vector3.right, m_LocalLook.x, Space.Self);
			transform.Rotate(Vector3.up, m_LocalLook.y, (m_AsFlightControl) ? Space.Self : Space.World);
		}

		// Reset
		m_LocalTranslate = Vector3.zero;
		m_LocalLook = Vector2.zero;
	}
}

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

*

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料