Horizontal push back not the same as vertical pushback whenever I use add explosion force
I'm coding rocket jumping in an FPS game. Whenever I shoot directly beneath me, I get the proper push-back in the vertical axis. However, when I shoot at something directly in front of me, I don't get the same push back in the horizontal axis as I did in the vertical axis. The push back in the horizontal axis is significantly lower as compared to one in the vertical axis. Here's a video of what I'm experiencing at the moment https://www.youtube.com/watch?v=gh4_rPnI1wI
The code I use for my explosion is below:
using UnityEngine;
using System.Collections;
public class explosion : MonoBehaviour {
public float explosiveForce = 20f;
public float explosiveRadius = 5f;
public float upwardsForce = 5f;
private Collider[] hitColliders;
void Update() {
hitColliders = Physics.OverlapSphere (transform.position, explosiveRadius);
foreach (Collider hit in hitColliders) {
Rigidbody rb = hit.GetComponent<Rigidbody> ();
if (rb != null) {
print ((hit.transform.position - transform.position).normalized);
print ((hit.transform.position - transform.position).normalized * explosiveForce);
// rb.AddExplosionForce(explosiveForce, transform.position, explosiveRadius, upwardsForce, ForceMode.Impulse);
rb.AddForce((hit.transform.position - transform.position).normalized * explosiveForce, ForceMode.Force);
}
}
}
}
I've commented out add explosion for now and am using add force as described. However, I'm still not getting the result I want.
Answer by Geometrical · Oct 10, 2016 at 02:09 AM
Nothing is actually wrong with your code, it's just how physics works. Simply put, when you fire a rocket vertically you create a force that opposes gravity; and when you fire horizontally both the force you create is acting on you, as well as the full, unopposed force of gravity which is pulling you down.
Try detecting which objects might create the horizontal force and apply a special modifier to the force you're creating to increase the horizontal push-back. If this is too vague for you just let me know and I'll try to explain in a better way.
Answer by zimmer550 · Oct 10, 2016 at 09:32 AM
Could it have anything to do with me modifying the rigidbody velocity directly in my player movement script. Another unity user said I should not modify velocity directly but simply add force to the player in the movement script. I'll try with that and then post my results