I'm trying to add a tag check to an if statement but for some reason when I add said tag check it stops working.
void Update ()
{
Vector3 fwd = transform.forward; // Equivalent to the code you had.
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hit;
if (Input.GetMouseButton(0) && Physics.Raycast(ray, out hit, 10f) && isPlayerHolding == false && gameObject.CompareTag("Pickable"))
{
isPlayerHolding = true;
hit.transform.SetParent(Player.transform);
Debug.Log("We hit: " + hit.transform.name);
hit.rigidbody.constraints = RigidbodyConstraints.FreezeRotation;
hit.rigidbody.constraints = RigidbodyConstraints.FreezePosition;
}
if (Input.GetMouseButton(1) && Physics.Raycast(ray, out hit, 10f) && isPlayerHolding == true)
{
hit.transform.parent = null;
isPlayerHolding = false;
hit.rigidbody.constraints = RigidbodyConstraints.None;
}
More specifically this is the bit of code that is holding things up "&& gameObject.CompareTag("Pickable")".
Things I've tried:
- Debugged variables in C# to be sure nothing is missing/null.
- Making sure the tag is setup properly within Unity.
I really can't think of what the problem is. Anyone have any ideas?
hit.rigidbody.contstraints
result in the position constraint overwriting the rotation constraint. If you want to freeze both rotation and position (all axes) then you should use a single assignment withRigidbodyConstraints.FreezeAll
. You can also use bitwise OR operators (the "pipe" character:|
) to combine multiple freeze types for more fine-grained control. \$\endgroup\$