Target size affects Player tilt /rotation / leaning when attacking

Depending on the size of the target, Player (and enemies) lean forward/backward when attacking. Is there a way to just keep them standing straight?

I tested this with several models, for example my own model(brown beastman) and a cube, which had scripts and components to make it proper Target for Enemies and Player.
When changing the height of the cube, models leaned when attacking, and finally stopped attacking when they couldn’t ‘reach’ it anymore (middle point / transform.position).
Maybe I have missed something in some video, or has this been solved (like changing the distance calculation to collider, or locking x-axis somewhere, or something) ?

1 Like

To be honest I don’t remember this happening at all to me when I did the course, so you probably skipped a step or maybe I didn’t try it with bigger enemies, but worry not, your solution is perfect, just add a line of code that prevents the tilt from happening.

1 Like

The ideal solution for this is to make sure that locally, 0,0,0 is at the feet of the character. This takes a bit of work when assembling the character. Once all characters origins are 0,0,0, then transform.lookat should be the equivilant of “point the character’s toes at the opponent’s feet.”

1 Like

Hmm… Yes, I guess changing the origin to the feet of the character might solve it - when they are standing on reasonable level ground. But when another character is clearly on high ground, for example standing on rock, or on top of steep stairs and so on… the tilting problem rises again? This is what happens with the provided models of the course.
Putting the origin to the the bottom/feet works if the ground is level enough, maybe I’ll just try to do that and continue with the course.

There is actually another method, I just wasn’t thinking the other night when I posted the feet=zed method (my main project deals with a flat procedurally generated map, making the feet=zed ideal). This other method requires a bit of math, but fortunately, the Quaternion class does most of that nasty math for us…

                Vector3 lookDirection = target.transform.position - transform.position;
                lookDirection.y = 0;
                Quaternion lookRotation = Quaternion.LookRotation(lookDirection, Vector3.up);
                transform.rotation = lookRotation;

Here’s how this works… first, we calculate the vector from the target to the character. This is a normal mathematical vector, so much in the x direction, so much in the y, so much in the z. Once we have this vector, we zero out the y component, effectively flattening the map, at least that’s what we’re going to tell Quaternion…
So for our Obi Wan Kenobi vs. Annakin example in the illustration above (Always have the high ground!), the vector might be (-2, 3, 1). We’ll change that to (-2, 0, 1)
Now we’ll calculate the new Look Rotation passing this modified lookdirection in and setting the upwards vector to Vector3.up. Quaternion.LookRotation() does all the crazy math that turns -2,0,1 into the correction direction. We set that as the rotation and the character will stay standing upright.
Of course, this is going to look odd as well, as you can see with my unarmed Annakin punching obi-wan’s feet.
image
This can be fixed, by setting up IK chains and modifying the actual pose dynamically to make the fist or sword punch towards the center of the capsule, but… that’s a whole potential course in and of itself.

1 Like

Yes, that might be the solution. Thanks. And yeah, striking up/down depending if the target is higher or lower might get very complicated…

… I also had made error while importing my model. It had its stomach in the 0 point of vertical axis position in Blender. I had to lift it up so the feet were in the 0 of vertical coordinate. (Doing the wrong way first makes me learn the best I suppose)

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms