unity,第三人稱射擊遊戲的攝像機實現

  1. 把攝像機放在人物GameObject裏面,位置設置在一個合適的位置,前後左右移動,就都會帶者攝像機移動了
  2. 在腳本中得到攝像機,並獲取鼠標x,y軸的偏移量
  3. 鼠標x軸的偏移量,直接用來旋轉人物的y軸,這樣帶者攝像機也會旋轉
  4. 鼠標y軸的偏移量,用來讓相機繞着人物的x軸旋轉
    在這裏插入圖片描述

代碼實現:

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

public class PlayerMove : MonoBehaviour
{
    private Camera mCamera;

    // Start is called before the first frame update
    void Start()
    {
        mCamera = transform.Find("PlayerCamera").GetComponent<Camera>();
    }


    // Update is called once per frame
    void Update()
    {
        float x = Input.GetAxisRaw("Mouse X");
        float y = Input.GetAxisRaw("Mouse Y");
        //鼠標x軸的偏移量,直接用來旋轉人物的y軸
        transform.Rotate(new Vector3(0, x, 0), Space.Self);
		//鼠標y軸的偏移量,用來讓相機繞着人物的x軸旋轉  
        mCamera.transform.RotateAround(transform.position,transform.right,-y);

    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章