2
\$\begingroup\$

I have this FPS I'm making and I wanted to apply force to objects I shoot at however when I shoot on them, instead of going away from the camera they come towards it.

I don't get it. I could fix the problem by putting a -myTransform.forward but I want to understand the cause of the problem so I can fix it better.

Here's the logic behind the calculation:

void ApplyForce(Vector3 hitPosition, Transform hitTransform)
{
    if ((hitTransform.GetComponent<Rigidbody>() != null))
    {
        hitTransform.GetComponent<Rigidbody>().AddForce(myTransform.forward * forceToApply, ForceMode.Impulse);
    }
}

When I import my model and reset its position relative to my player, it is flipped

\$\endgroup\$
1
\$\begingroup\$

I'm not sure what myTransform refers to, but with your code you are applying force based on that object's +Z axis. If that this is the player, the object will move either towards or away from it. Since you use myTransform.forward, this means the object will move on the +Z axis of the player. Have you checked that the player is looking at the +Z axis? or is it looking at the -Z?

Assuming this is the intended behaviour, everything else in your code seems about right, since you are using object's local axis to apply force, there's a chance one of your object is "flipped". Maybe it's rotated by 180 degrees on the Y axis, maybe it's Z scale is -1 because it's imported model was made that way. That's not necessarily wrong, but it's good to keep an eye one that.

Edit:

If the only problem is that the imported model is imported in the wrong angle, unfortunately Unity (as far as I know) cannot change that. You can open the imported model on a 3D software (like Blender), and edit the model to look in the desired axis. Then export again in your preferred format, and import it on Unity. If you are using Blender, you can take a look how to do that here.

\$\endgroup\$
  • \$\begingroup\$ That's exactly what I thought about everything, but I don't know where to check that. I suspect it's the weapon that I imported. but I don't know where to fix its rotation if it's flipped. \$\endgroup\$ – David Koubi May 1 '18 at 12:55
  • \$\begingroup\$ @DavidKoubi You can check the objects you have on the scene one-by-one (if it's not a big scene). Keep in mind an object with a scale Z of -1, will affect all of its children. Maybe when you imported the model, you rotated something to make it look "correct". To be fair it's not wrong to keep it this way, as long as you keep it consistent (if you upload a new model in the future, remember where the axis are supposed to show) \$\endgroup\$ – TomTsagk May 1 '18 at 12:59
  • \$\begingroup\$ I've added a picture of a newly imported Gun model and what I saw the first time. I did tried to fix it by rotating to the right angle of view but how was I supposed to correct the probleme otherwise? \$\endgroup\$ – David Koubi May 1 '18 at 13:07
  • \$\begingroup\$ @DavidKoubi I edited my answer with more details \$\endgroup\$ – TomTsagk May 1 '18 at 13:14
  • \$\begingroup\$ I just tried to disable all the model related objects and the problem is still here. I'm trying to think that my entire player character is set backward... \$\endgroup\$ – David Koubi May 1 '18 at 13:31
0
\$\begingroup\$

Okay I fixed the problem:

What I did was to just put my imported weapon into an empty object in the right Z axis and then rotate the imported weapon (now child of the empty object) at 180°.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.