- Home /
I need help with rigidbody and collision or something idk
The way I have my player set up is an empty game object as the main gameobject and it has a movement script using transform.Translate, a sprite for the player as a child of that gameobject, a centerpoint as a child to the gameobject with a bow sprite and fire point attached to it to shoot. And to that center point i have a script that makes it rotate towards the mouse (script from brackeys tutorial). but the problem is that when the player collides with something, the sprite and the empty gameobject stays, but the center and its children keep going because the center has a regidbody attached to it to do the rotation of the center point. So im wondering if there is a replacement option for the rigidbody.rotate so that they stay when the player collides with walls and enemies e.t.c. Here is the rotation script:
public class ArcherAttack : MonoBehaviour
{
public Camera cam;
public Rigidbody2D rb;
Vector2 mousePos;
void Update()
{
MousePosition();
}
void FixedUpdate()
{
FireRotation();
}
void MousePosition()
{
mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
}
void FireRotation()
{
Vector2 lookDir = mousePos - rb.position;
float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg - 90f;
rb.rotation = angle;
}
}
Answer by bbudian0 · Jan 07, 2021 at 08:35 PM
It’s most likely that you’re using transform.Translate(). If you have a rigidbody attached and you’re trying to utilize collision I’d recommend using the rigidbody.AddForce() or set the rigidbody.velocity in order to move your sprite correctly.
transform.Translate() doesn’t take rigidbody collision into account.
Not a problem! Let me know if you have any issues.
Not sure I follow the rotation bit but if your character is moving on its own after collision mess around with setting the constraints of the rigidbody. I typically check all rotational axis and usually the y position.
But that is dependent on your use case.
Answer by Derek_Brouwer · Jan 07, 2021 at 08:30 PM
There are 2 options I can see here. First thing to know is that you don't need a rigidbody to handle the rotation of a object. Rigidbodies are unity's physics system, and all it's doing is modifying the rotation on the transform which already exists.
So what I would do is simply set the rotation on the transform of your center point: transform.eulerangles = new Vector3 (0, 0, angle); //eulerangles are a nicer way of working with rotations using a Vector3 instead of a Quaternion.
Doing that you should be able to remove the rigidbody component (and whatever collider you likely have on the bow).
If you wanna keep the rigidbody for whatever reason, simply setting it to isKinematic to true (in the inspector) should disable collisions and physics on it, but allow the rotation and other physics systems to still me modified by code.
That would be the simplest solution but seems messy to me, if you have no need for the physics.
I tried setting the rigidbody to kinematic, but then i couldn’t rotate the bow whilst i was moving the player. But i will definitly give your first suggestion a try! Thank you very much.