- Home /
Collide with a 'switch' gameobject that lowers the speed of another gameobject
Hi!
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();
//Destroy(this.gameObject);
}
}
Thanks in advance!
Answer by Sandy Sandals · Apr 02, 2014 at 12:59 PM
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) {
object.ReduceSpeed(AmountYouWantToReduceTheSpeedBy);
}
Hope that helps! If it doesn't get back to me.
Your answer
![](https://koobas.hobune.stream/wayback/20220613141743im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Click on a collider behind another one 2 Answers
Change speed over time 1 Answer
Rigidbody Falls Through Plane 1 Answer
[Problem] Look at mouse with speedturn 0 Answers
code your own colision system i unity 0 Answers