I have a grandpa main character in my 2D game, he can move and he rotates to go left or right on the screen. From the Debug.Log it looks like it is working fine, but sometimes the object I pickup gets thrown in the opposite direction I am facing. This only happens about half of the time, and mostly when I am facing left (180 degrees). I have a child of the grandpa object acting as the place where the objects I pick up go when I pick them up. The objects’ colliders don’t touch either. Any ideas about what is happening? Here is my code:
void Update()
{
Debug.Log(grandpa.transform.rotation.eulerAngles.y);
if (holdingSomething == false)
{
// trigger list is the items within my interaction range
if (Input.GetButtonDown("Pickup") && triggerList.Count > 0)
{
Debug.Log("Picking Something up");
// gets the closest item to pick up and sets holdingSomething to true
itemPickup = PickupObject(triggerList, itemPickup);
rb = itemPickup.GetComponent<Rigidbody2D>();
rb.bodyType = RigidbodyType2D.Kinematic;
}
}
else if (triggerList.Count > 0)
{
if (Input.GetButtonDown("Pickup"))
{
rb.bodyType = RigidbodyType2D.Dynamic;
Debug.Log("Dropping Object");
// sets holdingSomething to false
DropObject();
}
else if (Input.GetButtonDown("Interact"))
{
rb.bodyType = RigidbodyType2D.Dynamic;
if (grandpa.transform.rotation.eulerAngles.y < 1)
{
HorizontalForce = Mathf.Abs(HorizontalForce);
}
else if (grandpa.transform.rotation.eulerAngles.y > 1)
{
HorizontalForce = -HorizontalForce;
}
DropObject();
// throwing the object with public variables set in the inspector, I have them at 6 and 6.
rb.AddForce(new Vector2(HorizontalForce, VerticalForce), ForceMode2D.Impulse);
}
else
{
Debug.Log("Holding Object");
itemPickup.transform.position = handSpot.position;
itemPickup.transform.rotation = handSpot.rotation;
}
}
}