我有這個 Recoil 腳本,唯一的問題是代碼將我的槍移動到所有軸上,但我只想在Z-axis上移動我的槍。我找不到任何解決方案。謝謝!這是代碼:
using UnityEngine;
using System.Collections;
public class Recoil : MonoBehaviour {
public Vector3 hipPos;
public Vector3 zoomPos;
public float speed = 4f;
void Update () {
if(Input.GetMouseButton(0)) {
transform.localPosition = Vector3.Lerp(transform.localPosition, zoomPos, Time.deltaTime * speed);
transform.localPosition = Vector3.Lerp(transform.localPosition, hipPos, Time.deltaTime * speed);
}
else {
transform.localPosition = Vector3.Lerp(transform.localPosition, hipPos, Time.deltaTime * speed);
}
}
}
uj5u.com熱心網友回復:
讓槍移動到所需的 z 位置,但保持 x 和 y 位置相同。
using UnityEngine;
using System.Collections;
public class Recoil : MonoBehaviour {
public Vector3 hipPos;
public Vector3 zoomPos;
public float speed = 4f;
private Vector3 localPosition
void Update () {
if(Input.GetMouseButton(0)) {
transform.localPosition = Vector3.Lerp(transform.localPosition, new Vector3(transform.localPosition.x, transform.localPosition.y, zoomPos.z), Time.deltaTime * speed);
transform.localPosition = Vector3.Lerp(transform.localPosition, new Vector3(transform.localPosition.x, transform.localPosition.y, hipPos.z) , Time.deltaTime * speed);
}
else {
transform.localPosition = Vector3.Lerp(transform.localPosition, new Vector3(transform.localPosition.x, transform.localPosition.y, hipPos.z), Time.deltaTime * speed);
}
}
}
要了解 lerp 的作業原理,請參閱此處的 Unity 檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/488286.html