0
\$\begingroup\$

I'm trying to get my 2D top down shooter to use -some- physics.

I want the player and enemies not be able to exist in the same position, in other words, use collisions.

My first attempt was to use a CharacterController, but I could not get my other game objects to collide with my player, even though I had a RigidBody2D and a BoxCollider2D on them. Obviously, due to using a CharacterController, I was not able to add these components on my player.

Going one step back, I removed the CharacterController, and rather opted to go the Rigidbody2D route. This worked partially. I now get collisions on my player when he walks into other gameobjects (with RB2D and BC2D on them), but there seems to be a "rebound" anomaly occurring; my player moves around after collision.

I've tried playing around with the settings on the RigidBody2D on the player, but it does not seem to have any effect.

This is my PlayerMovement script :

void Update()
{
    HandleRotation();
    HandleMovement();
}

void HandleRotation()
{
    var mousePos = Input.mousePosition;
    mousePos.z = 5.23f;

    var objectPos = Camera.main.WorldToScreenPoint(transform.position);
    mousePos.x -= objectPos.x;
    mousePos.y -= objectPos.y;

    float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
    transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
}

void HandleMovement()
{
    float horizontal = Input.GetAxisRaw(Constants.Axis.Horizontal);
    float vertical = Input.GetAxisRaw(Constants.Axis.Vertical);

    Vector3 direction = new Vector3(horizontal, vertical, 0f).normalized;
    
    if (direction.magnitude >= 0.1f)
    {
        var speed = (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
            ? mobileController.runningSpeed
            : mobileController.walkingSpeed;
        var moveVector = (direction * speed) + transform.position;
        mobileController.rigidbody2d.MovePosition(moveVector);
        
        var camPosition = new Vector3(mobileController.transform.position.x, mobileController.transform.position.y, -10);
        cam.position = camPosition;

    }
}

The settings on my Player :

enter image description here

I would like my player to stop dead in his tracks when colliding with another RigidBody2D. Is this possible? And if so, how would I go about doing that?

\$\endgroup\$
2
  • \$\begingroup\$ How have you configured the physics material on your player's collider, and how are you moving your player/enemies in script? \$\endgroup\$
    – DMGregory
    Jan 9, 2021 at 13:20
  • \$\begingroup\$ @DMGregory, thanks again for replying! Please see the edits on my post \$\endgroup\$
    – WynDiesel
    Jan 9, 2021 at 13:24

2 Answers 2

0
\$\begingroup\$

Resolved by setting the "Freeze Rotation" on the Z axis in the RigidBody2D component

enter image description here

\$\endgroup\$
1
  • 1
    \$\begingroup\$ In future, you might consider using more specific terms for the symptoms like "rotates" or "spins" or "twists" instead of "moves around" — that can help identify the problem and lead you to solutions like this even faster. Animated examples are useful when you can't find a way to describe the specific symptoms in words. \$\endgroup\$
    – DMGregory
    Jan 9, 2021 at 14:30
0
\$\begingroup\$

To prevent things from bouncing, what you want are "inelastic" collisions.

In Unity all collisions below a configurable Velocity Threshold are inelastic. You can change that velocity threshold inside your Physics2D settings. (menu: Edit > Project Settings > Physics 2D).

See also https://answers.unity.com/questions/1393846/rigidbody-sticks-to-walls-or-how-the-hll-do-you-fo.html

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .