Hi
I’ve have tried to use the 3d animation script tutorial to understand this in my 2d top down game.
function Start() {
if (!target)
target = GameObject.FindWithTag("Player").transform;
}
function Update ()
{
HandleAnimation();
RotateTowardsPosition(target.position, attackRotateSpeed);
}
function RotateTowardsPosition (targetPos : Vector3, rotateSpeed : float) : float
{
// Compute relative point and get the angle towards it
var relative = transform.InverseTransformPoint(targetPos);
var angle = Mathf.Atan2 (relative.x, relative.z) * Mathf.Rad2Deg;
// Clamp it with the max rotation speed
var maxRotation = rotateSpeed * Time.deltaTime;
var clampedAngle = Mathf.Clamp(angle, -maxRotation, maxRotation);
// Rotate
transform.Rotate(0, clampedAngle, 0);
// Return the current angle
return angle;
}
The problem I am having is the enemies rotate toward the Player Character but the run backwards at him…any ideas?
I do have a handleAnimation section…
I’m not too sure what the problem is here. Are you saying that the enemies rotate correctly towards the player, but when they are facing him, they flip around and approach him walking backwards? If so, can you post the handleAnimation function? If not, can you give some more detail about what should be happening and what is going wrong?
Instead of the Enemy GameObjects turning toward the player they successfully turn their backs to the player and attempt to run toward them by running backward.
Is the model for the enemy oriented correctly in local space? Or is it perhaps rotated 180 degrees from where it should be? (I ask because it sounds like the entities are actually behaving correctly, and it’s just the visual that’s backwards.)
You could try attaching a single ‘box’ game object to the entity, positioned and oriented so that it points down what you believe to be the entity’s forward axis. If, when the enemy is chasing the player, this box points towards the player but the model doesn’t, that would indicate that it’s just a problem with how the model is oriented in local space.
I have rotated the character in the visual space 180 they face the player… but once you press play on the game they rotate away…
Hm, I’m not sure what you mean by that.
Also, I don’t see in your posted code where you’re moving the entities linearly. Can you post that code?
Again, I’d recommend doing some debugging by either watching the ‘local coordinate system’ gizmo in the editor window or attaching some debug visuals to the entities to see whether they’re oriented correctly and the visual is just wrong, or if they’re actually ‘moving backwards’ compared to what you’re expecting.
not to sound like a noob but what is a gizmo?
gizmos are things like light entities that you can see ingame if you press the gizmo button - in this case he’s telling you to watch the co-ordinates and rotation values on the right while the game is running, you can do this by making sure the game doesnt mazimize on play in the editor and selecting the object before you perss play. by doing this you can check if the characters are performing the code you’ve written correctly - and hopefully fix your problem.
also, if you’ve tried rotating the actual model 180 degrees again, it may be a common problem that i had when i first started - try making an invisible box, putting it on the same co-ordinates as the enemy, put all of his heighrachy underneath the cube’s, rotate the model by 180 degrees and apply the script to the cube.
cube trick didn’t work
What I have done is take the animation and enemy movement ideals from the Evac city tutorial and applied the character movement ideals from penelope. the Level is 3d but the characters are 2d.
Have you tried the various other ideas that have been suggested?
i’ve watched the cordinates and i believe it’s rotating from 180 to 230 on the y axis. (x,y,z) if I drag the blue axis (x?) and move the enemy object around it corrects itself to continue to have the back of it’s mesh facing the player.
function RotateTowardsPosition (targetPos : Vector3, rotateSpeed : float) : float
{
// Compute relative point and get the angle towards it
var relative = transform.InverseTransformPoint(targetPos);
var angle = Mathf.Atan2 (relative.x, relative.z) * Mathf.Rad2Deg;
// Clamp it with the max rotation speed
var maxRotation = rotateSpeed * Time.deltaTime;
var clampedAngle = Mathf.Clamp(angle, -maxRotation, maxRotation);
// Rotate
transform.Rotate(0, clampedAngle, 0);
// Return the current angle
return angle;
}
i’ve tried changing the relative.x and relative.z and also trying to replace x with y or z with y etc… these make things worse… changing clamped angle also messes things up
if the enemys were created in a 3d modeling program like 3ds max, then you can open them up in the modeling program and re orientate the local z axis of the object so that they face the other way and re save them with the new axis orientation. it sounds like the axis of the model says that the front of the model is actually the physical/visual back of the model. if you rotate the z axis 180 in 3ds max/Maya ect. then it should be fine
i have had similar problem with doors where it hinges from the center but when i moved the axis piont to the side it rotated correctly from the side.
1 Like
Does it matter that they are flat 2d images? or should the same idea apply?
The same idea applies; if they’re oriented incorrectly in local space, they’ll appear to be oriented incorrectly in the game as well.
I created these in unity and just applied a sprite sheet as done in http://forum.unity3d.com/viewtopic.php?t=33220
Are you sure the sprites are oriented correctly in local space?
(At this point I think I’d probably have to see the project itself to make any further suggestions. Maybe someone else will be able to help though.)
One more quick comment on this. I was just looking over the tutorial you referred to, and there is mention (on page 9) of rotating the entity 180 degrees to compensate for how the sprites are set up. If you’re not doing this, you might take another look at the tutorial, as the last paragraph on page 9 appears to address this issue.