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);

    }
}

猜你喜欢

转载自blog.csdn.net/qq_40666620/article/details/105214505
今日推荐