ゾンビFPS_角度制限
+ボタン押下でコードを確認できます!
コピペしても正しく動かない場合は
エディタでアタッチし忘れている可能性が高いです。
- 角度制限
- This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
using System.Collections; using System.Collections.Generic; using UnityEngine; public class FPSController : MonoBehaviour { float x, z; float speed = 0.1f; public GameObject cam; Quaternion cameraRot, characterRot; float Xsensityvity = 3f, Ysensityvity = 3f; bool cursorLock = true; float minX = -90f, maxX = 90f; // Start is called before the first frame update void Start() { cameraRot = cam.transform.localRotation; characterRot = transform.localRotation; } // Update is called once per frame void Update() { float xRot = Input.GetAxis("Mouse X") * Ysensityvity; float yRot = Input.GetAxis("Mouse Y") * Xsensityvity; cameraRot *= Quaternion.Euler(-yRot, 0, 0); characterRot *= Quaternion.Euler(0, xRot, 0); cameraRot = ClampRotation(cameraRot); cam.transform.localRotation = cameraRot; transform.localRotation = characterRot; UpdateCursorLock(); } private void FixedUpdate() { x = 0; z = 0; x = Input.GetAxisRaw("Horizontal") * speed; z = Input.GetAxisRaw("Vertical") * speed; //transform.position += new Vector3(x, 0, z); transform.position += cam.transform.forward * z + cam.transform.right * x; } public void UpdateCursorLock() { if (Input.GetKeyDown(KeyCode.Escape)) { cursorLock = false; } else if (Input.GetMouseButton(0)) { cursorLock = true; } if (cursorLock) { Cursor.lockState = CursorLockMode.Locked; } else if (!cursorLock) { Cursor.lockState = CursorLockMode.None; } } public Quaternion ClampRotation(Quaternion q) { //q = x,y,z,w (x,y,zはベクトル(量と向き):wはスカラー(座標とは無関係の量)) q.x /= q.w; q.y /= q.w; q.z /= q.w; q.w = 1f; float angleX = Mathf.Atan(q.x) * Mathf.Rad2Deg * 2f; angleX = Mathf.Clamp(angleX, minX, maxX); q.x = Mathf.Tan(angleX * Mathf.Deg2Rad * 0.5f); return q; } }
他の講座クーポン【93%OFF】
関連記事
クーポン期限2023/01/24 Udemyでは定価24000円になっていますがこのクーポンページからの購入で1800円台で購入することが可能になっています! 30日以内な[…]
Udemyよりお得に講座視聴可能なYouTubeメンバーシップ