码迷,mamicode.com
首页 > 移动开发 > 详细

第一人称玩家移动 摄像机跟随

时间:2018-05-30 10:54:47      阅读:561      评论:0      收藏:0      [点我收藏+]

标签:new   sys   self   ati   设置   amt   inpu   视角   eric   

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

public class PlayerPlay : MonoBehaviour
{
public float MoveSpeed;
private Vector3 dir;

//第一人称视角旋转
private Transform camTrans;
private Vector3 camAng;
public float camHeight; //可以在unity测试中调整摄像机高度


// Use this for initialization
void Start()
{
//第一人称视角旋转
//初始化相机的位置
camTrans = Camera.main.transform;
Vector3 startPos = transform.position;
startPos.y += camHeight;
startPos.z += 1.3f;
camTrans.position = startPos;
camTrans.rotation = transform.rotation;
camAng = camTrans.eulerAngles;
}
void Update()
{
PlayerMove();
Rotate();
}
//玩家的移动
private void PlayerMove()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
dir = new Vector3(h, 0, v);
transform.Translate(dir * Time.deltaTime * MoveSpeed, Space.Self);
}
private void Rotate()
{
//相机随鼠标旋转
float y = Input.GetAxis("Mouse X");
float x = Input.GetAxis("Mouse Y");
camAng.x -= x;
camAng.y += y * 2.5f;
camTrans.eulerAngles = camAng;
//设置物体与相机的Y旋转方向一致
camTrans.position = new Vector3(this.transform.position.x, camTrans.position.y, this.transform.position.z);
float camy = camAng.y;
this.transform.eulerAngles = new Vector3(this.transform.eulerAngles.x, camy, this.transform.eulerAngles.z);
//更新摄像机位置
Vector3 startPos = transform.position;
startPos.y += camHeight;
camTrans.position = startPos;
}
}

第一人称玩家移动 摄像机跟随

标签:new   sys   self   ati   设置   amt   inpu   视角   eric   

原文地址:https://www.cnblogs.com/ywmqq005/p/9109362.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!