Collision with object that would affect different Objects. Help please

Need some help please.

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;
}


}

Why not do what everyone else does when posting codeā€¦?

Many people are not going to read your badly formatted code.

1 Like

@AlexSzar

ā€œ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.

If I try using it in the Update I get the following:

@AlexSzar

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).

No I dont. Just started recently and still trying to get my head around everything.

Harder to do then it looks haha. But thanks

@AlexSzar

ā€œHarder to do then it looks haha. But thanksā€

It is hard because you donā€™t know the basics. Iā€™d find some free C# tutorial, go through that first and then start learning from Unity Learn site.