マウスでカメラをコントロールする
![54f935bc a0e5 4db4 870e 2c325301e506](https://codegenius.org/uploads/slide/image/2902/54f935bc-a0e5-4db4-870e-2c325301e506.jpeg)
![95c506e2 75dd 46e7 ac94 7b5cd77fceab](https://codegenius.org/uploads/slide/image/2903/95c506e2-75dd-46e7-ac94-7b5cd77fceab.jpeg)
マウス操作でカメラの向きを変える
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// ネームスペースの設定
namespace FPS{
public class CameraController : MonoBehaviour {
[Range(0.1f, 10f)]
public float lookSensitivity = 5f;
[Range(0.1f, 1f)]
public float lookSmooth = 0.1f;
public Vector2 MinMaxAngle = new Vector2 (-65, 65);
private float yRot;
private float xRot;
private float currentYRot;
private float currentXRot;
private float yRotVelocity;
private float xRotVelocity;
void Update () {
yRot += Input.GetAxis ("Mouse X") * lookSensitivity; // マウスの横移動
xRot -= Input.GetAxis ("Mouse Y") * lookSensitivity; // マウスの縦移動
// (ポイント)「Clamp」の意味と使い方をネットで調べよう!
xRot = Mathf.Clamp(xRot, MinMaxAngle.x, MinMaxAngle.y); // 上下の角度移動の最大、最小
// (ポイント)「SmoothDamp」の意味と使い方をネットで調べよう!
currentXRot = Mathf.SmoothDamp (currentXRot, xRot, ref xRotVelocity, lookSmooth);
currentYRot = Mathf.SmoothDamp (currentYRot, yRot, ref yRotVelocity, lookSmooth);
transform.rotation = Quaternion.Euler (currentXRot, currentYRot, 0);
}
}
}
![Bc29bc4d 342a 4242 be63 d7507c6b2f5a](https://codegenius.org/uploads/slide/image/2904/bc29bc4d-342a-4242-be63-d7507c6b2f5a.jpeg)
![70da6502 4d13 498a 9c2a 8ca03ecab3a2](https://codegenius.org/uploads/slide/image/2905/70da6502-4d13-498a-9c2a-8ca03ecab3a2.jpeg)
EscapeCombat
他のコースを見る![54f935bc a0e5 4db4 870e 2c325301e506](https://codegenius.org/uploads/slide/image/2902/54f935bc-a0e5-4db4-870e-2c325301e506.jpeg)
![95c506e2 75dd 46e7 ac94 7b5cd77fceab](https://codegenius.org/uploads/slide/image/2903/95c506e2-75dd-46e7-ac94-7b5cd77fceab.jpeg)
マウス操作でカメラの向きを変える
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// ネームスペースの設定
namespace FPS{
public class CameraController : MonoBehaviour {
[Range(0.1f, 10f)]
public float lookSensitivity = 5f;
[Range(0.1f, 1f)]
public float lookSmooth = 0.1f;
public Vector2 MinMaxAngle = new Vector2 (-65, 65);
private float yRot;
private float xRot;
private float currentYRot;
private float currentXRot;
private float yRotVelocity;
private float xRotVelocity;
void Update () {
yRot += Input.GetAxis ("Mouse X") * lookSensitivity; // マウスの横移動
xRot -= Input.GetAxis ("Mouse Y") * lookSensitivity; // マウスの縦移動
// (ポイント)「Clamp」の意味と使い方をネットで調べよう!
xRot = Mathf.Clamp(xRot, MinMaxAngle.x, MinMaxAngle.y); // 上下の角度移動の最大、最小
// (ポイント)「SmoothDamp」の意味と使い方をネットで調べよう!
currentXRot = Mathf.SmoothDamp (currentXRot, xRot, ref xRotVelocity, lookSmooth);
currentYRot = Mathf.SmoothDamp (currentYRot, yRot, ref yRotVelocity, lookSmooth);
transform.rotation = Quaternion.Euler (currentXRot, currentYRot, 0);
}
}
}
![Bc29bc4d 342a 4242 be63 d7507c6b2f5a](https://codegenius.org/uploads/slide/image/2904/bc29bc4d-342a-4242-be63-d7507c6b2f5a.jpeg)
![70da6502 4d13 498a 9c2a 8ca03ecab3a2](https://codegenius.org/uploads/slide/image/2905/70da6502-4d13-498a-9c2a-8ca03ecab3a2.jpeg)
マウスでカメラをコントロールする