一人称の視点(カメラ)の角度制限についての質問です。
現在VR内でキャラクターを動かすスクリプトを作成しています。
下記コードは頭を前後に傾けると前や後ろに進み、頭を左右に傾けると左右に曲がれる動きなのですが、VR酔い防止の為、左右の傾きに角度制限をつけたいと思っています。(現在は地面すれすれまで傾けることができます)
Mathf.Clampなどを試してみましたが上手くいきません。
下記コードへの角度制限の付け方を教えてください。よろしくお願いします。
public class GazeController : MonoBehaviour { new Rigidbody rigidbody; //リジッドボディーを使用 new GameObject camera; //カメラ //初期化 void Start() { this.rigidbody = GetComponent<Rigidbody>(); this.camera = GameObject.Find("Main Camera"); } //定期的に呼ばれる void FixedUpdate() { //Player操作 float x = camera.transform.localEulerAngles.x; float z = camera.transform.localEulerAngles.z; if (x < 180) x += 360; if (z < 180) z += 360; float h = (Math.Min((360f - z), 45f) / 45f) * 1f; float v = (Math.Min((z - 360f), 10f) / 10f) * 5f; //Playerの向きと加速度の指定 if (v != 0f) { this.gameObject.transform.Rotate(0, (v > 0) ? h : -h, 0); } this.rigidbody.velocity = this.gameObject.transform.rotation * new Vector3(0, 0, v); }
追記
using System; using UnityEngine; //Playerへアタッチするスクリプト //MainCameraはPlayerの子要素 public class GazeController : MonoBehaviour { new Rigidbody rigidbody; //リジッドボディーを使用 public new GameObject camera; //カメラ //初期化 void Start() { this.rigidbody = GetComponent<Rigidbody>(); this.camera = GameObject.Find("Main Camera"); } //定期的に呼ばれる void FixedUpdate() { PlayerController(); } private void PlayerController() { //cameraのVector3をlocalupへ取得 var localup = camera.transform.rotation * Vector3.up; var frontangle = Mathf.Atan2(localup.y, localup.z) * Mathf.Rad2Deg; //上下角度 var sideangle = Mathf.Atan2(localup.y, localup.x) * Mathf.Rad2Deg; //左右角度 //取得した数値が360になるように270を足す( Debug.Logで数値を確認) if (frontangle < 180) frontangle += 270; if (sideangle < 180) sideangle += 270; // 最小値を返す関数、最初は(frontangle - 360f)=0からのスタートとする float FB = (Math.Min((frontangle - 360f), 10f) / 10f) * 5f ; //上下角度 float LR = (Math.Min((360f - sideangle), 15f) / 15f) * 1f; //左右角度 Debug.Log("FB:" + FB); //Angle数値の確認用 Debug.Log("LR:" + LR); //Playerの向きと加速度の指定 //前後 this.rigidbody.velocity = this.gameObject.transform.rotation * new Vector3(0, 0, FB); //左右 if (FB != 0f) { this.gameObject.transform.Rotate(0, (FB > 0) ? LR : -LR, 0); } } }
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/09 15:04