控制物體移動
直接上代碼(改腳本掛載到游戲物體上)文章來源:http://www.zghlxwxcb.cn/news/detail-612759.html
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Move : MonoBehaviour
{
private Vector3 move; //移動方向
private float movespeed = 3f; //移動速度
private float H, V;
// Start is called before the first frame update
void Start()
{
move = new Vector3(0, 0, 0);
}
// Update is called once per frame
void Update()
{
//獲取按鍵信息
H = Input.GetAxis("Horizontal");
V = Input.GetAxis("Vertical");
move.x = H;
move.z = V;
//獲取攝像機(jī)朝向
float y = Camera.main.transform.rotation.eulerAngles.y;
move = Quaternion.Euler(0, y, 0) * move;
if (H != 0 || V != 0)
{
//使物體移動
transform.Translate(move * Time.deltaTime * movespeed, Space.World);
//這一步是使物體移動時始終面向前方
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(0, y, 0), 0.05f);
}
}
}
控制攝像機(jī)移動的代碼
第三人稱攝像機(jī)移動文章來源地址http://www.zghlxwxcb.cn/news/detail-612759.html
到了這里,關(guān)于Unity3D 控制物體移動且自動旋轉(zhuǎn)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!