Problems moving sprite using transform.Translate in C#

Hi All,

I hope this is the appropriate place to ask for help on this, I’ve about pulled my hair out.

I’m putting together a 2D Android tower-defense type game in 4.6.6f2 where the enemies are spawned (instantiated by prefab) on the right side of the screen and move to the left. I had everything working, just looked pretty basic, so I 'm going back and replacing a lot of the placeholder graphics.

I use a public float currentSpeed to set the enemy speed so I can dial in the speed where the feet aren’t slipping, but no matter what I do I can’t get my new enemy to move unless I hit Play, then go to the Animator component in the inspector and toggle Apply Root Motion. It doesn’t matter if I start with it unchecked or checked, my character won’t start moving until I’ve toggled that to whatever value it ISN"T while the game is running. I’ve tried toggling it programattically in my Start() but that doesn’t have the same result, I still need to either turn it off or on in the inspector while it is playing. All other enemies work without having to toggle that value (and all enemies share this script).

All enemies have a Rigidbody2D (set to isKinematic), a BoxCollider2D (isTrigger true), and are moved using my attacker script Update which looks like this:

void Update ()
{       
        // Checking to see if my values equal zero somewhere       
        Debug.Log ("V3:" + Vector3.left + " CS:" + currentSpeed + " dT:" + Time.deltaTime);
        Debug.Log ("Total:" + (Vector3.left * currentSpeed * Time.deltaTime).ToString("F5"));
       
        transform.Translate (Vector3.left * currentSpeed * Time.deltaTime);
       
        if (!currentTarget)
        {
            anim.SetBool("isAttacking", false);
        }
}

It almost seems to be something with the Animator component – I took an older enemy I’d made and started swapping out the sprites and components one at a time to see where it failed. Once i put the animator controller in, it stops moving left during the animation. The animation controller is very simple – has an isAttacking boolean and a default state of Walking with the walk animation. Normally I use a SetSpeed animation event once I’ve dialed in the speed from the public float, but at this time it is disabled so I can just pull from the float. While running I can tell that my Vector3.left * currentSpeed * Time.deltaTime is definitely not coming out to zero (though it is a small number, the actual Vector3 value is like (-0.00674, 0.00000, 0.00000).

Another odd issue is that I cannot put a check in the ‘isAttacking’ animator bool while running the game. All other enemies allow me to check that while running to make sure my attack animation look okay – but on this one enemy I can’t check it. Not like it checks and then unchecks, I literally cannot put a checkmark in the isAttacking bool to set it to true either through the animator window or through code. I have removed and recreated the Animator component and controller a couple times now, no change.

Any suggestions?

Is the checkbox enabled? If so, this sounds like you can set it, but it gets reset immediately. You can try that out by setting the checkbox while the player is paused, then unpause it. If it gets unchecked again, then that’s likely the problem.

Hi Blizzy,

Thanks for the quick reply – you’re totally right, if I pause the game I can then check the isAttacking boolean, it just immediately clears it once I unpause. If I uncheck or remove my attacker script in the inspector, I can then set the boolean. The only thing in my Update that could be doing this is my check for a current target – but commenting that line out (line 11 above) still doesn’t let me set the boolean while playing. I’m wondering if something in my Start is causing problems.

void Start ()
{
        Rigidbody2D myRigidbody = GetComponent<Rigidbody2D>();
       
        if (myRigidbody == null)
            Debug.Log (name + " is missing a rigidbody!");

        myRigidbody.isKinematic = true;

        anim = GetComponent<Animator>();
        if (anim == null)
            Debug.Log ("Could not connect " + name + "'s animator!");

        count++;

        Debug.Log("Attacker.count = " + count);
}

Is there anything obvious I’ve broken here? Or am I barking up the wrong tree? There are 3 different enemies (bone based and sprite based things I’ve grabbed from Google) that are all working fine, is there a chance my animation itself could be causing problems?

Many thanks!

SOLVED! I think. Here is what I did in case anyone else runs into this…

I kept coming back to thinking it was something in the animation – it looks like at some point I unknowingly put a curve and keyframe for the Transform Position position in frame 1 (must have repositioned the enemy with the Record button clicked). I think it was just hanging out there and not moving. Once I deleted that curve entirely, my enemy started walking without having to mess around with the Apply Root Motion checkbox. I can also now put a check in my isAttacking boolean and everything is working fine.

1 Like