Question by
PlayZzOfficial · Jul 25, 2021 at 12:32 AM ·
c#inputreleasepress
Press and Release? New InputSystem
Hey guys, I'm trying to figure out how to use press and release, If I press the button I'm executing the interaction, but if I release the button i stop interaction...
and this is how I have my code set up, but if i release the key it wont stop interacting. Could someone explain to me why this isn't working??
void FixedUpdate()
{
RaycastHit frontRay;
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out frontRay, raycastDistance, boxMask))
{
if (frontRay.collider != null && frontRay.collider.gameObject.tag == "pushable" && interactionControl.action.triggered)
{
box = frontRay.collider.gameObject;
box.GetComponent<FixedJoint>().enableCollision = true;
box.GetComponent<FixedJoint>().connectedBody = this.GetComponent<Rigidbody>();
}
else if (interactionControl.action.triggered)
{
box = frontRay.collider.gameObject;
box.GetComponent<FixedJoint>().enableCollision = false;
}
}
}
Comment
Answer by andrew-lukasik · Jul 28, 2021 at 05:53 AM
Input is not the problem, code logic is.
HashSet<FixedJoint> _connectedBodies = new HashSet<FixedJoint>();
void FixedUpdate ()
{
if( _inputAction.triggered )
{
var ray = new Ray( transform.position , transform.TransformDirection(Vector3.forward) );
if( Physics.Raycast( ray , out var hit , _raycastDistance , _layerMask ) )
{
var joint = hit.collider.GetComponent<FixedJoint>();
if( joint!=null && hit.collider.gameObject.tag=="pushable" )
{
joint.enableCollision = true;
joint.connectedBody = this.GetComponent<Rigidbody>();
_connectedBodies.Add( joint );
}
}
}
else if( _connectedBodies.Count!=0 )
{
foreach( var joint in _connectedBodies )
{
joint.enableCollision = false;
}
_connectedBodies.Clear();
}
}