hi i’m trying to create a level with some energy walls that push the player away.
so far, i have create a wall with a sphere collision trigger, when the player enter the trigger the script is pushing him back, the problem is that i 'm not able to remove all the velocity from the rigidbody component, this mean that my player walks very slow.
i already tried to set the velocity to 0.0f and changing IsKinematic to true, but in both cases when i try to remove the velocity my player return to the point where the velocity of the rigid body was changed for first time (the sphere collision trigger position).
here is the code that i’m using to let the wall push back the player
bool bForce = false;
GameObject wPlayer;
void Start () {
}
void Update () {
if (bForce) {
PushThePlayer();
}
}
void OnTriggerEnter(Collider hit){
if (hit.gameObject.tag == "Player") {
wPlayer=hit.gameObject;
bForce=true;
StartCoroutine("StopForce");
}
}
void PushThePlayer(){
if (wPlayer != null) {
Vector3 offset = new Vector3(wPlayer.transform.position.x-this.gameObject.transform.position.x ,0,wPlayer.transform.position.z-this.gameObject.transform.position.z);
wPlayer.GetComponent<Rigidbody>().velocity+=offset.normalized*(300.0f * Time.deltaTime);
}
}
public IEnumerator StopForce(){
yield return new WaitForSeconds (2);
bForce = false;
wPlayer = null;
}
any ideas how to figure out this ? i’m doing something wrong here ? if there is a way to do this without using rigidbodies to avoid the velocity problem, can someone show me some thread to help me ?
i tried already to search on google, but didn’t found a solution.
thanks in advance for all the help.
ok, so what I am getting from your code is that you want to add the velocity to your model when he enters an area, you add that velocity for 2 seconds.
To do this you are using this:
wPlayer.GetComponent<Rigidbody>().velocity+=offset.normalized*(300.0f * Time.deltaTime);
However, I believe that you simply want to add force to your character instead.
wPlayer.rigidbody.AddForce(offset.normalized * wPlayer.rigidbody.mass * 300);
You could also “punt him”
offset.y = 0.333f;
wPlayer.rigidbody.velocity = offset.normalized * 30;
This would simply set the offset rather than try to add velocity each frame.
Also, when you add velocity, or add force, you want to do this on the FixedUpdate rather than the Update. Update relies on frame rate, FixedUpdate is by physics Calculation.
i have changed the Update for FixedUpdate and tried with AddForce, but is still the same, i have attached a video showing the player behavior after the force is applied, please note that the player can walk fine in some directions but in some it seems to still have some kind of force on the rigidbody.
any idea on how to fix this ? thanks in advance for all the help.
First, test it, comment out this line:
wPlayer.GetComponent<Rigidbody>().velocity+=offset.normalized*(300.0f * Time.deltaTime);
Play it, if there is no force, then the problem is that this line is continuing to be called even though it looks like it’s not…
If this is the case… make this line public:
bool bForce = false;
As you play with this object selected, just look at the B Force variable, if it never becomes unchecked, then change this part…
public IEnumerator StopForce(){
Debug.Log("start stop force")
yield return new WaitForSeconds (2);
Debug.Log("end stop force");
bForce = false;
wPlayer = null;
}
Observe how many times this kicks on, or if it never kicks off.
Localize your problem, NEVER try to figure it out as a whole. Its called Scientific Method.
Again, I would just “punt” the person back. This way you call this once and that is it.
can i use on my player a character controller and a rigidbody ?
i saw in this thread on the forum that both can’t be used.