第一人稱玩家移動 攝像機跟隨

來源:互聯網
上載者:User

標籤: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;
}
}

第一人稱玩家移動 攝像機跟隨

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.