Push player away from target if hes in range
Hi, i want to use a ventilator in my game. When the player is in range, he should be pressed away. It should be possible to walk against the stream, but with more resistance. I tried several ways with MoveTowards and AddForce, but i couldn't get the wanted result. Hope anyone can help! Thanks!
Comment
Answer by Spidlee · Apr 15, 2017 at 06:17 AM
I believe what you want is a constant force pushing the player away while you're in range with the ventilator. So essentially just doing something like the below semi pseudocode on the player should do the trick I'd say.
void OnTriggerStay(Collider other) {
if(other == ventilator)
float relativeDistToVentilator = Vector3.Distance(transform.position, ventilator.position) / maxStreamDistance;
transform.position += ventilator.transform.forward * streamForce * relativeDistToVentilator * Time.deltaTime;
}
stream.transform.forward is just the direction of the stream. Replace it with whatever you need.
Your answer
![](https://koobas.hobune.stream/wayback/20220612114959im_/https://answers.unity.com/themes/thub/images/avi.jpg)