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