0
\$\begingroup\$

I have a player and an enemy square for my 2D game (technically it's a cube, but it's rendered as a 2D character). I have a RigidBody2D attached to the player and the enemy each, along with a BoxCollider2D, with trigger ticked on both of them. However, when I untick trigger the motion gets multiplied drastically. It appears to be that the trigger being ticked is causing the entire thing to be updated more quickly but I'm not sure. Can someone help?

FIX I needed to turn off Auto Mass in the Rigidbody2D. Thanks @DMGregory

Enemy Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class enemyController : MonoBehaviour
{

    [Header("Refrences")]
    public GameObject player;
    public GameObject graphicsGameobject;

    [Header("Speeds")]
    public int movementSpeed;
    public float fireRate;

    // Private Refrences
    new private Rigidbody2D rigidbody;

    // Start is called once
    void Start() {
        rigidbody = GetComponent<Rigidbody2D>();
    }

    ////////////
    // MOTION //
    ////////////

    // Return position to face player
    Vector2 getMouseVector()
    {
        // Get mouse pixel position, convert it to world position, subtract
        // from current position vector to get angle, and return as negative Vector2
        // Why negative?  Only way I could get it to work.
        Vector2 myPosition = transform.position; // V3 converted to V2, that's why this line exsists
        return -(myPosition - new Vector2(
            player.transform.position.x,
            player.transform.position.y
        ));
    }

    // Move the player's RigidBody to chase player
    void moveEnemy() {
        Transform ptrans = player.transform;
        if (ptrans.position.y > transform.position.y) // Up
            { rigidbody.AddForce(transform.up * movementSpeed * Time.deltaTime); }
        if (ptrans.position.x > transform.position.x) // Left
            { rigidbody.AddForce(transform.right* movementSpeed * Time.deltaTime); }
        if (ptrans.position.y < transform.position.y) // Down
            { rigidbody.AddForce(transform.up * -movementSpeed * Time.deltaTime); }
        if (ptrans.position.x < transform.position.x ) // Right
            { rigidbody.AddForce(transform.right * -movementSpeed * Time.deltaTime); }
    }


    ////////////
    // UPDATE //
    ////////////

    void Update() {
        graphicsGameobject.transform.up = getMouseVector();
        moveEnemy();
    }

}
\$\endgroup\$
4
  • \$\begingroup\$ Are you calculating the mass of the body using its colliders? Trigger colliders might not be counted for this purpose, which could change how the inertia of the body is initialized. \$\endgroup\$
    – DMGregory
    Jan 14, 2020 at 19:30
  • \$\begingroup\$ I’m calculating mass with the Rigidbody2D, and i could be with the Collider also. I’ll check. Thanks! \$\endgroup\$ Jan 14, 2020 at 19:43
  • \$\begingroup\$ It was the auto calculated mass in the Rogidbody2D. Thank you @DMGreogory! \$\endgroup\$ Jan 14, 2020 at 20:09
  • \$\begingroup\$ Sweet! Please post that as an answer so it's easy for future users to find. \$\endgroup\$
    – DMGregory
    Jan 14, 2020 at 20:36

1 Answer 1

1
\$\begingroup\$

What I’m describing was caused by the Rigidbody2D auto-calculate mass. Just uncheck that box and it should work fine!

\$\endgroup\$

You must log in to answer this question.

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