UnityProgram日記02:Playerと追従カメラ編

ハイ今回もCsコードを乗せていきます。
前回の追従カメラはVRゲームのカメラであることを
すっかり忘れていたのでした。

今回のコードは
デスクトップゲームのPCゲーム用のコードです。

ここから追従カメラ(マウス操作)

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


public class MiruCamera : MonoBehaviour
{
    public GameObject target_obj; //ターゲット用
    public float radius = 5.0f;
    public float cam_height = 8;
    public float angle = 0.0f;
    public float angle_v, angle_h;
    float distance = 1.0f;

    public float rotSpeed = 180f;
    CursorLockMode wanted_mode = CursorLockMode.None;

    // Use this for initialization
    void Start ()
    {
        // 初期動作
        Cursor.lockState = wanted_mode;
    }
    
    // Update is called once per frame
    void Update ()
    {
        Cursors();
        DeskTopModeCamera();
    }


    void DeskTopModeCamera()
    {
        Vector3 target_look_pos = target_obj.transform.position + new Vector3(0,1.5f,0);

        if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
        {
            //なめまわしモード
            angle_v -= Input.GetAxis("Mouse Y") * rotSpeed * Time.deltaTime;
            angle_h += Input.GetAxis("Mouse X") * rotSpeed * Time.deltaTime;
            angle_v = Mathf.Clamp(angle_v, -85, 85);  //ジンバルロック横着回避
        }
        else
        {
            //通常時
            angle_v -= Input.GetAxis("Mouse Y") * rotSpeed * Time.deltaTime;
            angle_h += Input.GetAxis("Mouse X") * rotSpeed * Time.deltaTime;
            angle_v = Mathf.Clamp(angle_v, -30, 60);
        }
        if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
        {
            if (Input.GetAxis("Mouse ScrollWheel") != 0)
            {
                distance -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * 50;
                distance = Mathf.Clamp(distance, 0.2f, 2.0f);
            }
        }

        Vector3 rot_dir = Quaternion.Euler(angle_v, angle_h, 0f) * Vector3.back * distance;
        this.transform.position = (target_look_pos + radius * rot_dir);

        //カメラをターゲットの方へ向かせる
        transform.LookAt(target_look_pos);
    }

    void Cursors()
    {
        // カーソルをウィンドウから出さない
        Cursor.lockState = wanted_mode = CursorLockMode.Confined;
    }
}

ここからPlayer

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

[System.Serializable]
public class PlayerValiavle
{
    //インスペクターへ表示
    public bool is_cheack_ground = true;
    public int distance = 5;
    public float jump_power = 6.0f;
    public GameObject foot_col_L, foot_col_R;
    public Camera main_cam;
    public float walk_speed = 2;
    public float gravity = 20.0f;

    public CharacterController c_controller;
    public bool p_slider;

    public Vector3 player_direction = Vector3.zero;
    public Vector3 move_direction = Vector3.zero;
    public Vector3 slide_vector;
}

public class Player : MonoBehaviour
{

    [SerializeField]
    PlayerValiavle pv = new PlayerValiavle();

    // Use this for initialization
    void Start()
    {
        pv.c_controller = GetComponent();
    }

    // Update is called once per frame
    void Update()
    {
        PlayerMove();
        PlayerJump();

    }

    void PlayerMove()
    {
        //カメラのTransform取得
        pv.main_cam = Camera.main;
        Transform cam_transform = pv.main_cam.transform;

        //カメラの向きをワールドに変換
        //カメラの正面がいつも前という事になる
        Vector3 forward = cam_transform.TransformDirection(Vector3.forward);
        //yを削除
        forward.y = 0;
        //カメラの右ベクトルを作成
        Vector3 right = new Vector3(forward.z, 0, -forward.x);

        float v, h;
        //移動値を取得
        v = Input.GetAxisRaw("Vertical");
        h = Input.GetAxisRaw("Horizontal");

        //入力をカメラの向きに直す
        pv.player_direction = h * right + v * forward;

        //入力がある程度あればその方向にプレイヤーを向ける
        if (pv.player_direction.magnitude > 0.1f)
        {
            transform.rotation = Quaternion.LookRotation(new Vector3(pv.player_direction.x, pv.player_direction.y, pv.player_direction.z));
        }

        //Playerに移動値を渡す
        this.transform.position += pv.player_direction * Time.deltaTime * pv.walk_speed;
    }

    void PlayerJump()
    {
        RaycastHit hit;
        int mul_ray_range = 1;
        int layer_mask = 1 << LayerMask.NameToLayer("Stage");
        int ray_length = 1;

        //空中の状態か確認後、地面に向かってレイを飛ばす。
        if (Physics.Raycast(this.transform.position + new Vector3(0, 1, 0), Vector3.down * mul_ray_range, out hit, ray_length, layer_mask))
        {
            Debug.Log("RayHit");
            pv.is_cheack_ground = true;
        }
        else
        {
            Debug.Log("RayUnHit");
            pv.is_cheack_ground = false;
        }
        Debug.DrawRay(this.transform.position + new Vector3(0, 1, 0), Vector3.down * mul_ray_range, Color.red, ray_length);
        

        //地面についているか
        if (pv.is_cheack_ground)
        {
            //移動値の初期化
            pv.move_direction.y = 0;

            if (Input.GetKey(KeyCode.Space))
            {
                pv.move_direction.y = pv.jump_power;
            }
        }
        else
        {
            //地面についていなければ引く
            pv.move_direction.y -= pv.gravity * Time.deltaTime;
        }

        //y方向の値を加える
        pv.player_direction += new Vector3(0, pv.move_direction.y, 0);

        //Playerに移動値を渡す
        this.transform.position += pv.player_direction * Time.deltaTime * pv.walk_speed;
    }
}