1
\$\begingroup\$

I am new to Unity. I Setup a character in unity2D with a rigidbody. I had the scale off my sprites set to 100 pixels per unit. At this scale, my rigidbody character stood on top of platforms and could not pass through walls containing box colliders. I decided, that to make things easier, I would change all sprites to 1 pixel per unit. After doing so, I scaled everything (variables, sizes, etc.) by 100. I confirmed that the box colliders on both the caharcters, floor, and walls, are all in the same places, relatively. The box colliders on the floors still work, but the wall box colliders behave differently. When I walk into a wall, the wall slowly pushes me in a direction (depending on whether or not my character has walked through half the wall or not). The wall should be stopping my motion completely.

The code is very simple, but this is what I use to move:

if (Input.GetKey (KeyCode.D)) {
        transform.Translate (Vector2.right*moveOffset*Time.deltaTime);
        transform.localScale = new Vector3(1,1,1);
        anim.SetBool("moving",true);
    }

Do note that the moveOffset variable, was scaled by 100 after the change. Thanks in advanced!

\$\endgroup\$
  • \$\begingroup\$ When you use rigidbodies, then you should no longer manipulate the transform directly. It confuses the physics engine and can lead to all kinds of weird behavior. Only use the methods of rigidbody to move it. \$\endgroup\$ – Philipp Sep 17 '16 at 20:28
  • \$\begingroup\$ 1. Don't use a scale of 1 pixel per unit. Unity's engine systems (including physics) are optimized for most interactions happening on a scale of a few units to dozens — not hundreds and thousands. Making your content this large will tend to degrade performance and cause strange behaviour. 2. If you're using physics, use Rigidbody2D.MovePosition or velocity, instead of mucking with the transform. Try fixing these issues and edit the question if you're still observing unexpected behaviour. \$\endgroup\$ – DMGregory Sep 17 '16 at 20:36
1
\$\begingroup\$

You are modifying the Transform of the gameObject. When using physics, changing the transform of the object is a big NO-NO (unless it's to perform a teleport or something like that).

If you want to move a gameObject with physics behaviour you should do so by adding forces to the rigidbody, or changing it's velocity.

Also, when applying changes to physic objects, you should be putting your code in void FixedUpdate() instead, and use Time.fixedDeltaTime (FixedUpdate() runs at a constant rate, unlike Update() )

Try this:

void FixedUpdate()
{
    if (Input.GetKey (KeyCode.D)) {
        GetComponent<Rigidbody2D>().velocity = new Vector2(speed,0);
        anim.SetBool("moving",true);
    }
}

Keep in mind that even after releasing the key, the object will keep moving, being slowed down by the ´linear drag´.

\$\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.