admin管理员组文章数量:1421182
My code has no errors but when I move my character in to the power-up it is not destroyed. The speed boost does not appear to work either when i move the player over the power-up item. I'm really not sure what the problem is but it might lie in the OnTriggerEnter I think. The tag has the same name on the power-up item so really cant figure out what's wrong.
private float speed;
private float boostTimer;
private bool boosting;
private bool moving;
void Start()
{
moving = false;
transform.Translate(1, 1, 1);
speed = 5;
boostTimer = 0;
}
if (boosting)
{
boostTimer += Time.deltaTime;
if (boostTimer >= 3)
{
speed = 5;
boostTimer = 0;
boosting = false;
}
}
void OnTriggerEnter(Collider other)
{
if(other.tag == "SpeedBoost")
{
boosting = true;
speed = 10;
Destroy(other.gameObject);
}
}
if (moving)
{
this.transform.Translate(new Vector3(Time.deltaTime * speed, 0, 0));
}
My code has no errors but when I move my character in to the power-up it is not destroyed. The speed boost does not appear to work either when i move the player over the power-up item. I'm really not sure what the problem is but it might lie in the OnTriggerEnter I think. The tag has the same name on the power-up item so really cant figure out what's wrong.
private float speed;
private float boostTimer;
private bool boosting;
private bool moving;
void Start()
{
moving = false;
transform.Translate(1, 1, 1);
speed = 5;
boostTimer = 0;
}
if (boosting)
{
boostTimer += Time.deltaTime;
if (boostTimer >= 3)
{
speed = 5;
boostTimer = 0;
boosting = false;
}
}
void OnTriggerEnter(Collider other)
{
if(other.tag == "SpeedBoost")
{
boosting = true;
speed = 10;
Destroy(other.gameObject);
}
}
if (moving)
{
this.transform.Translate(new Vector3(Time.deltaTime * speed, 0, 0));
}
Share
Improve this question
edited Jan 17 at 22:14
derHugo
91.3k9 gold badges91 silver badges135 bronze badges
asked Jan 17 at 18:42
stratutravstratutrav
11 bronze badge
5
|
1 Answer
Reset to default -1The code you've put here doesn't make sense because the if statements are just sitting in the class, but maybe you are implying that those if statements are in the Update method, not that this is your actual code.
Here's an example of how it would work in a 2D game. Is Trigger has to be checked to use the on trigger. You can also attach the debugger to unity and put a break point inside on trigger method to see if it gets called at all. Also have you made sure the powerup has a collider?
public class Player : MonoBehaviour
{
private float _speed = 4.5f;
private bool _isSpeedPowerupActive = false;
//other code or a Start() method
void Update()
{
// define a new Vector3 called "direction" here if the player is moving
if (_isSpeedPowerupActive)
{
transform.Translate(direction * 2 * _speed * Time.deltaTime);
}
else
{
transform.Translate(direction * _speed * Time.deltaTime);
}
}
IEnumerator ExpirePowerup(float duration)
{
yield return new WaitForSeconds(duration);
_isSpeedPowerupActive = false;
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Powerup")
{
_isSpeedPowerupActive = true;
StartCoroutine(ExpirePowerup(5.0f)));
Destroy(other.gameObject);
}
}
}
本文标签: cSpeed boost powerup code not working Powerup item not being consumedStack Overflow
版权声明:本文标题:c# - Speed boost power-up code not working. Power-up item not being consumed - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745348183a2654597.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
visual-studio
),collider
isA Websocket signalling server written in the go language for WebRTC
-> absolutely unrelated,move
Usually refers to move semantics; consider using that tag instead. Move semantics is a programming language feature that allows a copy operation to be replaced by a more efficient "move"...
-> absolutely unrelated,triggers
are rules that perform actions or invoke functions in response to events such as matching conditions or database changes
-> absolutely unrelated – derHugo Commented Jan 17 at 22:14OnTriggerEnter
is nested as a local function within theUpdate
method so Unity's messaging system doesn't know about it so it isn't invoked by the physics .. it has to be on class level scope – derHugo Commented Jan 17 at 22:15Transform
which can break physics and collision detection .. you should rather move through aRigidbody
– derHugo Commented Jan 18 at 8:42