Collide with a 'switch' gameobject that lowers the speed of another gameobject


Me and some friends are doing a simple platformer for a school project! We’ve a simple time system (it counts up) in the game and a switch that lowers the time when you collide with it. The thing is, we also want the switch to lower the speed of another gameobject in the game when you collide with the switch (this gameobject “chases” you through the game).

How do I do this? I’ve been trying for hours now…

The code for the object that chases you looks like this:

	public float speed = 2f;
	void Update () {
		transform.Translate(Vector3.up * speed * Time.deltaTime);

Do I create a new script that handles the change in speed and put it in the ‘switch’ object or…?

In the time script we’ve the following bit of code that both the GUI Text object and the switch have:

void OnTriggerEnter2D(Collider2D col)
		if(col.gameObject.tag == "Player")
			levelTimer -= 0.5f;
			timerTex.text = levelTimer.ToString();

Thanks in advance!

Hi there!

So what you could do is make a method in the objects class that reduces the speed. That script might look like this;

public void ReduceSpeed(float speedReduction){
	speed -= speedReduction;

Then at the switch you would have;

void OnTriggerEnter2D(Collider2D col) {

Hope that helps! If it doesn’t get back to me.