unityでカメラの向きにキャラクターを向けたい

実現したいこと

カメラの向きにキャラクターを向けたい

前提

unityでキャラクター操作について勉強しています。
キャラクターの移動とマウスによる視点操作はできたのですが別々のスクリプトで作成しており、カメラの向きに合わせてキャラクターの向きも同じ方向に向くよう連動させるにはどうしたらいいですか?

発生している問題・エラーメッセージ

なし

該当のソースコード

public class Move : MonoBehaviour
{
[SerializeField]
private float magnitude; // スピードの大きさ
[SerializeField]
private float MaxMagnitude; // 加速上限
[SerializeField]
private float jumpPower; //ジャンプの大きさ
[SerializeField]
private float G; //重力
private Rigidbody rb;
private bool isGround = true;
private Vector3 moveVelocity;
private Vector3 XmoveVelocity;
private Vector3 ZmoveVelocity;
private Vector3 moveDirection;
private Vector3 ZmoveDirection;

// Start is called before the first frame update void Awake() { rb = GetComponent<Rigidbody>(); moveDirection = new Vector3(1.0f,0.0f,1.0f); moveDirection.Normalize(); //正規化 XmoveVelocity = new Vector3(0.0f,0.0f,1.0f); ZmoveVelocity = new Vector3(1.0f,0.0f,0.0f); Physics.gravity = new Vector3(0.0f, -G, 0.0f); XmoveVelocity = (moveDirection - XmoveVelocity) * magnitude; // ベクトルを取得 ZmoveVelocity = (moveDirection - ZmoveVelocity) * magnitude; } void Update() { Jump(); } // Update is called once per frame void FixedUpdate() { //移動 if(magnitude < MaxMagnitude){ if(Input.GetKey(KeyCode.W)){ rb.AddRelativeForce(ZmoveVelocity, ForceMode.Force);// ローカル座標の向きに力を加える } if(Input.GetKey(KeyCode.S)){ rb.AddRelativeForce(ZmoveVelocity * -1.0f, ForceMode.Force); } if(Input.GetKey(KeyCode.D)){ rb.AddRelativeForce(XmoveVelocity, ForceMode.Force); } if(Input.GetKey(KeyCode.A)){ rb.AddRelativeForce(XmoveVelocity * -1.0f, ForceMode.Force); } } } // オブジェクトの空中でのジャンプ防止 public void OnCollisionEnter(Collision other){ // オブジェクトがStageについているときの処理 if(other.gameObject.name == "Stage"){ isGround = true; } } public void OnCollisionExit(Collision other){// オブジェクトが地面から離れた時の処理 if(other.gameObject.name == "Stage"){ isGround = false; } } //ジャンプ public void Jump(){ if(isGround == true){ if(Input.GetKeyDown(KeyCode.Space)){ rb.AddForce(transform.up * jumpPower, ForceMode.Impulse); } } }

}

public class CameraControl : MonoBehaviour
{
// Start is called before the first frame update
[SerializeField]
private float limit;
public GameObject Player;
private float rotX, rotY;

// Update is called once per frame void Update() { // マウスのX座標とY座標の取得 rotX = Input.GetAxis("Mouse X"); rotY = Input.GetAxis("Mouse Y"); RotXY(rotX, rotY, limit); // カメラの回転 } public void RotXY(float rotX, float rotY, float limit){ float maxLimit =limit; // X軸回転の上限と下限を指定 float minLimit = 360 - maxLimit; Vector3 localAngle = transform.localEulerAngles; // オイラー角のローカルアングル localAngle.x += -rotY; //マウスY座標の移動量をx軸の回転に計算("Mouse Y"の取得する値は-0.05なのでマイナスをかける) // 回転する角度の上限と下限をつける処理 if(localAngle.x > maxLimit && localAngle.x < 180){// 上限を超えた範囲(0~360度のため180度より上と下の範囲の条件をつける) localAngle.x = maxLimit; } if(localAngle.x < minLimit && localAngle.x > 180){ localAngle.x = minLimit; } transform.localEulerAngles = localAngle; // Y軸の回転はワールド座標を基準にすることでX軸の回転軸と混在しないようにする Vector3 angle = transform.eulerAngles; angle.y += rotX; transform.eulerAngles = angle; }

}

試したこと

RotXY関数のangleをVector3型の戻り値として取得できるか試したができなかった

補足情報(FW/ツールのバージョンなど)

コメントを投稿

0 コメント