facebook twitter hatena line email

「Unity/3d/キャラクタ移動/前後回転」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
 
行1: 行1:
#転送 [[Unity/3d/キャラクタ移動/前後回転 CharacterController]]
+
<pre>
 +
using System.Collections;
 +
using System.Collections.Generic;
 +
using UnityEngine;
 +
 
 +
public class CharacterControl : MonoBehaviour
 +
{
 +
    float x, z;
 +
    float speed = 0.1f;
 +
    float rotate_speed = 1f;
 +
    GameObject cam;
 +
    Quaternion cameraRot, characterRot;
 +
    float Xsensityvity = 3f, Ysensityvity = 3f;
 +
    bool cursorLock = true;
 +
    float minX = -90f, maxX = 90f;
 +
    void Start()
 +
    {
 +
        cam = GameObject.Find("Main Camera");
 +
        cameraRot = cam.transform.localRotation;
 +
        characterRot = transform.localRotation;
 +
    }
 +
    void Update()
 +
    {
 +
        float xRot = Input.GetAxisRaw("Horizontal") * rotate_speed;
 +
        characterRot *= Quaternion.Euler(0, xRot, 0);
 +
        cameraRot = ClampRotation(cameraRot);
 +
        transform.localRotation = characterRot;
 +
    }
 +
    private void FixedUpdate()
 +
    {
 +
        x = 0;
 +
        z = Input.GetAxisRaw("Vertical") * speed;
 +
        transform.position += cam.transform.forward * z + cam.transform.right * x;
 +
    }
 +
    public Quaternion ClampRotation(Quaternion q)
 +
    {
 +
        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;
 +
    }
 +
}
 +
</pre>
 +
参考:https://www.popii33.com/unity-first-person-camera/

2022年12月11日 (日) 08:25時点における版

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharacterControl : MonoBehaviour
{
    float x, z;
    float speed = 0.1f;
    float rotate_speed = 1f;
    GameObject cam;
    Quaternion cameraRot, characterRot;
    float Xsensityvity = 3f, Ysensityvity = 3f;
    bool cursorLock = true;
    float minX = -90f, maxX = 90f;
    void Start()
    {
        cam = GameObject.Find("Main Camera");
        cameraRot = cam.transform.localRotation;
        characterRot = transform.localRotation;
    }
    void Update()
    {
        float xRot = Input.GetAxisRaw("Horizontal") * rotate_speed;
        characterRot *= Quaternion.Euler(0, xRot, 0);
        cameraRot = ClampRotation(cameraRot);
        transform.localRotation = characterRot;
    }
    private void FixedUpdate()
    {
        x = 0;
        z = Input.GetAxisRaw("Vertical") * speed;
        transform.position += cam.transform.forward * z + cam.transform.right * x;
    }
    public Quaternion ClampRotation(Quaternion q)
    {
        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;
    }
}

参考:https://www.popii33.com/unity-first-person-camera/