Hi All,
I am working on my capstone project for the coursera game development course. In about 6 weeks time I created a 2d platformer which is not perfect, but it works.
When cleaning up my project I decided to collect all my single sprites into a sprite board. This worked pretty well, and I switched the sprites used in my enemy for the ones cut out of the sprite board.
But now my enemies do not move anymore. I checked my code against versions where it was working, checked the setup in Unity, but cannot find anything wrong.
As a last resort, I created an enemy from scratch. But no luck.
Does anybody have any idea where I can look or what else I can check?
Thanks!
You may want to share the code you’re using for movement and more about how you set up the enemy gameobject.
Good point. Please find the code and a inspector screenshot.
void EnemyMovement()
{
if ((myWayPoints.Length != 0) && (_moving))
{
//determine distance between enemy and waypoint
_vx = myWayPoints[_myWaypointIndex].transform.position.x - _transform.position.x;
//move enemy to waypoint until distance reached. then set other waypoint as target
if(Mathf.Abs(_vx) <= 0.05f)
{
_rigidbody.velocity = new Vector2(0, 0);
_myWaypointIndex++;
//flip enemy to look at new waypoint
Flip();
if (_myWaypointIndex >= myWayPoints.Length)
{
if (loopWayPoints)
_myWaypointIndex = 0;
else
_moving = false;
}
_moveTime = Time.time + waitAtWayPoint;
}
else
{
//enemy is moving
_animator.SetBool("Moving", true);
_rigidbody.velocity = new Vector2(_transform.localScale.x * movementSpeed, _rigidbody.velocity.y);
}
}
I don’t think that you changing to spritesheets would have any impact on the character movement, unless you mean movement via animations?
Try throwing some debug statements in there to print out the velocity and to see which sections are being executed.
Those were my thoughts exactly.
i also tried the debug statements and they actually show 3. as the velocity in the x direction and 0 in the y direction. That is why i do not get why the character is not moving. I am certain the code is running. I even checked if it was applying it to the correct rigidbody…
Could it have anything to do with the empty game object I used to link the waypoints and the enemy?
Unfortunately without looking at the project myself I’m not sure what it could be. I would run the game, and look at the components. Make sure the Rigidbody does not get set to Kinematic or constrained. You can use Debug.DrawRay to draw the velocity to see the forces being applied.
Try this in an Update loop on the enemy:
Debug.DrawRay(_rigidbody.position, _rigidbody.velocity, Color.red);
Looking at your screenshot, I can’t find anything out of place. This may just be an exercise in debugging, so start verifying your assumptions.
What’s the hierarchy situation for the enemy? Is the enemy a child of another rigidbody or anything like that?
Out of curiosity, why is the X velocity a factor of the localScale.x?
Is your Animator bool getting set correctly? Does the animator control position at all?
I did find some rotation and z-axis positions, not sure where they came from. I removed them, but not helping.
The velocity * localscale was from one of the scripts we received during training. since scale is 1, it should not make a difference I guess. The Animator does not have any positioning for walking. The sprites do show the correct walking animation. so the bool must be set correctly.
I will continue the debugging. the lines are very interesting and not in the direction I expected. the removal of the z positions I mentioned above did not change the line… so must be something else…