So I have a SpeedBoost object and when the player collides with it I want to increase the speed on incoming objects, however at the moment thatI trigger the SpeedBoost it would only affect the speed of the SpeedBoost object.
The following movement script has been attached to each object that I want to increase speed on.
Is there any way that when I collide with the SpeedBoost it would make the other GameObjects be triggered and have their speed increased?
Thanks
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
public class Move : MonoBehaviour
{
private float boostTimer;
private bool boost;
public int speed = 3;
public float maxboost = 10;
void Start()
{
if (boost)
{
boostTimer += Time.deltaTime;
if (boostTimer >= maxboost) // this would stop the boost
{
speed = 3;
boostTimer = 0;
boost = false;
}
}
}
void OnTriggerEnter2D(Collider2D coll)
{
if (coll.tag == "Player")
{
boost = true; // boost settings
speed = 20;
Destroy(gameObject, 11);
}
}
void Update()
{
transform.position += Vector3.left * speed * Time.deltaTime;
}
}
āIs there any way that when I collide with the SpeedBoost it would make the other GameObjects be triggered and have their speed increased?ā
I donāt quite get your logic. So basically you seem to have parts there, but logic is off.
What do you expect to happen with that code in Start? It will only run once.
You have a boolean state āboostā but you donāt actually use (except in Start). Shouldnāt you use it in Update perhaps?
Edit: also, if this script is attached to moving object / player, this doesnāt make sense:
if (coll.tag == "Player")
But if Move is attached to player, why should you be check if you collided with player? Shouldnāt you instead check if you collided with boost object? And if we follow this logic, that Destroy is going to destroy this GameObject i.e. moving object, not the boost object.
Sorry, I donāt try to sound harsh, but do you actually understand C# language basics? Iām asking because it looks like you have added code without much logic to it.
I think you have two choices, either make player/moving object sense the boost object, and let it manage the momentary faster movement. Alternatively, make the boost object activate moving objectās speed up (call some method in moving object).