- 把摄像机放在人物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<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);
}
}