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

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

實現 UI 元素后玩家隨機改變位置

實現 UI 元素后玩家隨機改變位置

C#
鳳凰求蠱 2023-12-17 10:42:19
所以我一直在關注在Unity中制作2D游戲的教程(我是一個完全的新手,這是我第一次接觸編程),我想給游戲添加一個功能(粗體)。 “心臟系統”我添加的工作正常(空心的數量等于玩家受到的傷害),但它導致我的玩家以一種奇怪的方式改變了他的位置。您可以看到,設置了邊界(maxHeight = 3,2,minHeight = -3,2),以及他的運動值(Yincrement = 3.2),但是,在按向上或向下箭頭鍵后,他似乎發生了變化Y 位置大約 4.67。 這是播放器腳本:using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.SceneManagement;using UnityEngine.UI;public class Player : MonoBehaviour{    private Vector2 targetPos;    public float Yincrement;    public float speed;    public float maxHeight;    public float minHeigth;    public int health = 3;    public int numOfHearts;    public Image[] hearts;    public Sprite heartFull;    public Sprite heartEmpty;    public GameObject effect;    public Image healthDisplay;    private void Update()    {        for (int i = 0; i < hearts.Length; i++)        {            if (i < health)            {                hearts[i].sprite = heartFull;            }            else            {                hearts[i].sprite = heartEmpty;                if (i < numOfHearts)                {                    hearts[i].enabled = true;                }                else                {                    hearts[i].enabled = false;                }            }            if (health <= 0)            {                SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);            }        }    }}
查看完整描述

1 回答

?
森林海

TA貢獻2011條經驗 獲得超2個贊

主要問題

主要問題是您的for 循環。它只能用于更新 UI - 不能多次調用您的移動和場景重新加載!您應該提前關閉for循環。


for (int i = 0; i < hearts.Length; i++)

{

? ? // you can also reduce these to single lines

? ? hearts[i].enabled = i < numOfHearts;


? ? if(i < numOfHearts) hearts[i].sprite = i < health ? heartFull : heartEmpty;? ??

} // <-- ALREADY CLOSE IT HERE


if(health <= 0) ...


...

位置夾緊

你對位置的夾緊極不安全!想象一下當前位置是?3.1?仍然是?< maxHeight,因此您添加?Yincrement?一次,結果是最大可能高度 < /span>6.3!這不是你想要的。

您應該直接鉗位targetPosition,而不是鉗位當前transform.position。例如,您可以使用?Mathf.Clamp?確保?targetPos.y?始終保持在給定范圍內。

此外,由于這兩種情況都執行非常相似的操作,因此我會使用簡單的?int?變量來設置方向,將其減少為僅一次移動:

...


transform.position = Vector2.MoveTowards(transform.position, targetPos, speed * Time.deltaTime);


var move = 0;

var targetPosY = targePos.y;


if (Input.GetKeyDown(KeyCode.UpArrow) && targetPosY < maxHeight)

{

? ? move = 1;

}

else if (Input.GetKeyDown(KeyCode.DownArrow) && targetPosY > minHeigth)

{

? ? move = -1;

}


// Handle the movement according to the direction

// in one single code block for both directions

if(move != 0)

{

? ? Instantiate(effect, transform.position, Quaternion.identity);


? ? // increase/decrease the targetPosY according to the move direction

? ? targetPosY += move * Yincrement;

? ? // Now make sure it is within the range

? ? targetPosY = Mathf.Clamp(targetPosY, minHeight, maxHeight);


? ? // finally assign the new target position

? ? targetPos = new Vector2(transform.position.x, targetPosY);

}


查看完整回答
反對 回復 2023-12-17
  • 1 回答
  • 0 關注
  • 145 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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