- Home /
How to push a player in a certain direction?
I am making a couple of levels that have a wind particle effect that, when the player jumps, the wind's direction will push the player in that direction. Different rooms will have different directions so it need to be able to tell which way the wind is blowing and push the player in that direction. I got a player with rigidbody that can jump, but that is about all I know how to do.
Answer by UnityCoach · Oct 31, 2018 at 08:40 AM
You can use a trigger collider, to which you add something like :
[SerializeField] Vector3 _dir;
[SerializeField] float _strength;
OnTriggerStay (Collider other)
{
if (!other.attachedRigidbody)
return;
other.attachedRigidbody.AddForce (_dir * _strength, ForceMode.Force);
}
The Constant Force component does just that otherwise.
Answer by A_Lego · Oct 31, 2018 at 05:39 AM
It's actually pretty simple, you would just constantly add force in which ever direction the "wind" is "blowing".
private Rigidbody rb;
private Vector3 ThisLevelsWindDir;
private float ForceOfTheWind;
void Start(){
rb = player.GetComponent<Rigidbody>();
}
void Update(){
rb.AddForce (ThisLevelsWindDir*ForceOfTheWind);
if(Input.GetKey (KeyCode.D)){
rb.AddForce (Vector3.right*speed);
}
}
Wouldn't this just push players to the right or am I misunderstanding it?
Note, forces shouldn't be applied within Update, but FixedUpdate.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Force required to throw an object. 1 Answer
Smooth dashing issues 0 Answers
Distribute terrain in zones 3 Answers
Dual conveyor to turn object 0 Answers