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

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

面對方向的隨機敵人移動

面對方向的隨機敵人移動

C#
尚方寶劍之說 2023-04-29 09:56:29
我正在嘗試讓我的敵艦模擬實際的宇宙飛船。因此,船只向前加速,但隨著時間的推移,會朝著不同的方向移動,如所附圖片所示。這需要是一個隨機的面對方向,但它必須平滑地過渡到下一個方向,以停止我當前方法所產生的抖動效果。https://imgur.com/tBslTpI我目前正在嘗試執行我展示的代碼,但它會使敵人對象在每次旋轉之間閃爍并且不平滑。    public float directionChangeTimer = 5f;    public float accelerateSpeed;    public void addRandomDirection()    {        float randomAngleAdd = Random.Range(-5f, 5f);        transform.Rotate(0, 0, randomAngleAdd);    }    public void Update()    {        //Add our Functions        addRandomDirection();    }
查看完整描述

1 回答

?
隔江千里

TA貢獻1906條經驗 獲得超10個贊

目前你在每一幀-5之間旋轉和5度數。

如果你想讓這更順暢,你可以使用協程,例如

public float directionChangeTimer = 5f;


public float anglesPerSecond = 1f;


public float accelerateSpeed;


private void Start()

{

? ? StartCoroutine(RandomRotations);

}


private void IEnumerator RandomRotations()

{

? ? while(true)

? ? {

? ? ? ? // wait for directionChangeTimer seconds

? ? ? ? yield return new WaitForSeconds(directionChangeTimer);


? ? ? ? // get currentRotation

? ? ? ? var startRot = transform.rotation;


? ? ? ? // pick the next random angle

? ? ? ? var randomAngleAdd = Random.Range(-5f, 5f);


? ? ? ? // store already rotated angle

? ? ? ? var alreadyRotated = 0f;


? ? ? ? // over time add the rotation

? ? ? ? do

? ? ? ? {

? ? ? ? ? ? // prevent overshooting

? ? ? ? ? ? var addRotation = Mathf.Min(Mathf.Abs(randomAngleAdd) - alreadyRotated, Time.deltaTime * anglesPerSecond);


? ? ? ? ? ? // rotate a bit in the given direction

? ? ? ? ? ? transform.Rotate(0, 0, addRotation * Mathf.Sign(randomAngleAdd));


? ? ? ? ? ? alreadyRotated += addRotation;


? ? ? ? ? ? yield return null;

? ? ? ? }

? ? ? ? while(alreadyRotated < Mathf.Abs(randomAngleAdd));

? ? }

}


查看完整回答
反對 回復 2023-04-29
  • 1 回答
  • 0 關注
  • 117 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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