亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Unity - 將對象移動到點

Unity - 將對象移動到點

C#
臨摹微笑 2022-01-15 19:30:43
我正在使用下面的代碼將球移動到確定的點。但是,球正在“傳送”到那里,我怎樣才能將球滾動到該點?void Update(){            if (Input.GetMouseButtonDown(0) && EventSystem.current.currentSelectedGameObject != ButtonDiminuir && EventSystem.current.currentSelectedGameObject != ButtonAumentar &&       EventSystem.current.currentSelectedGameObject != BarraForca) {                    transform.position = Vector3.Lerp(transform.position, new Vector3(transform.position.x, transform.position.y, -9.0424f), 2 * Time.deltaTime);                    Anim.Play("Kick_Up");            }}
查看完整描述

2 回答

?
翻過高山走不出你

TA貢獻1875條經驗 獲得超3個贊

你可以這樣做Vector3.Lerp:


Vector3 startPosition;

Vector3 endPosition;

var speed = 10.0;


transform.position = Vector3.Lerp(startPosition, endPosition, speed * Time.deltaTime);

或使用 Vector3.MoveTowards


// The step size is equal to speed times frame time.

float step = speed * Time.deltaTime;


// Move our position a step closer to the target.

transform.position = Vector3.MoveTowards(transform.position, target.position, step);


查看完整回答
反對 回復 2022-01-15
?
蕪湖不蕪

TA貢獻1796條經驗 獲得超7個贊

如果要滾動球,請AddForce()改用transform.position.


首先,將Rigidbody和添加Sphere Collider到您的球類游戲對象。


然后試試這段代碼:


public Vector3 targetPoint;

public float forceAmount;


...


void Update()

{

    Vector3 force = ((targetPoint - transform.position).normalized * forceAmount * Time.smoothDeltaTime);

    GetComponent<Rigidbody>().AddForce(force);

}

另外,如果你想球在到達目標點后立即停止,你可以設置GetComponent<Rigidbody>().velocity為 0 時targetPoint - transform.position = 0


我希望它對你有幫助。


查看完整回答
反對 回復 2022-01-15
  • 2 回答
  • 0 關注
  • 226 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號