當(dāng)給一個目標(biāo)點,如果目標(biāo)直接去目標(biāo)點我們可以直接讓position指向目標(biāo)點的position。
如果是轉(zhuǎn)換輸入呢?
舉例:例如一個人物動畫里有兩個參數(shù)X和Y,X(- 1 ,1) 表示向左走和向右走,Y (-1 , 1) 向后和向前走。
如果我給一個目標(biāo)點,如何計算應(yīng)該給動畫什么樣的數(shù)值就可以呢。
其實很簡單,我們首先計算目標(biāo)點的向量。當(dāng)自己人物有旋轉(zhuǎn)的時候會發(fā)生變化。這時候可以使用 transform.InverseTransformDirection(dir)函數(shù)來將世界向量轉(zhuǎn)成局部向量就可以了。
我們看看結(jié)果:
綠色球在前方的時候,計算出了 Z = 1
旋轉(zhuǎn)自己后,目標(biāo)改到前方,依舊是Z =1
測試代碼如下:文章來源:http://www.zghlxwxcb.cn/news/detail-597830.html
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestPoints : MonoBehaviour
{
public Transform target;
string showtxt;
GUIStyle fontStyle = new GUIStyle();
private void Awake()
{
fontStyle.normal.background = null; //設(shè)置背景填充
fontStyle.normal.textColor = new Color(1, 0, 0); //設(shè)置字體顏色
fontStyle.fontSize = 36; //字體大小
}
// Update is called once per frame
void Update()
{
Vector3 at = target.position;
at.y = transform.position.y;
Vector3 dir = (at - transform.position).normalized;
Debug.DrawRay(transform.position, dir, Color.red, 1f);
Vector3 moveInput = transform.InverseTransformDirection(dir);
showtxt = moveInput.ToString("F2");
}
void OnGUI()
{
GUI.Label(new Rect(200, 200, 680, 50), showtxt, fontStyle);
}
}
下面的GIF是轉(zhuǎn)一周的Input數(shù)值變化。文章來源地址http://www.zghlxwxcb.cn/news/detail-597830.html
到了這里,關(guān)于Unity根據(jù)目標(biāo)點的位置計算Input輸入的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!