The intentions of this function are to utilize the RigidBody component from a "target" object and clear its force while applying a diagonal force in the direction upwards and away from the side they are facing. I have omitted the portion in which detects the other direction for the sake of simplicity in reading the function as I've been testing this function while the user faces the right-hand direction.
Variables:
- target refers to the other object currently in the scene
- targBody refers to the RigidBody component of the target
object
damage refers to the current damage integer of the object.
The damage used in this instance is from the target object
facingRight refers to a bool in which is true whenever the current object is facing the right-hand direction.
public void Damage() {
targBody.velocity = Vector3.zero;
if(facingRight) {
Vector3 newVel = new Vector3(target.GetComponent().damage/16, target.GetComponent().damage/16, 0);
targBody.AddForce(newVel, ForceMode.Impulse);
print("Stuff");
}
target.GetComponent().damage+=3;
}
For some reason, when I apply the force, the target object doesn't move diagonally. Instead, it moves horizontally before moving vertically; however, for some strange reason the horizontal speed is always greater than the vertical speed as it goes so fast that it nearly teleports!
I'm not sure if this error happens because of a coding problem on my part that I'm just not recognizing or if it's a problem with RigidBody in which I need to use a workaround for.
Anyone have any ideas on what can make this function work the way I want it to?
AddForce
, I am rather inclined to assume that the bug is on your side. Recommend simplifying your use case and testing until you figure out what's causing the issue. \$\endgroup\$