unity,第三人称射击游戏的摄像机实现

Githa ·
更新时间:2024-09-21
· 790 次阅读

把摄像机放在人物GameObject里面,位置设置在一个合适的位置,前后左右移动,就都会带者摄像机移动了 在脚本中得到摄像机,并获取鼠标x,y轴的偏移量 鼠标x轴的偏移量,直接用来旋转人物的y轴,这样带者摄像机也会旋转 鼠标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(); } // 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); } }
作者:胜天半子_王二_王半仙



摄像 摄像机 unity

需要 登录 后方可回复, 如果你还没有账号请 注册新账号