Hi everyone,
Im new to the community but not to Unity. I’ve made several little games before and know my way around the engine. Im currently making an Air Hockey game in JavaScript and I am having some issues with the puck bouncing back off the edges of the table. I’m trying to accomplish this without using rigidbody physics as it seems to be unreliable at faster speeds.
Currently I have four boxes set up, one on each edge of the table spanning that edges length. Each box check to see when the puck is within range of it and then calls this function which, I thought, would return the new direction the puck should be traveling. But it isn’t. Any help or suggestions would be greatly appreciated. Thanks ![]()
function CompareAngle(other:Transform)
{
var bumpBackDirct:Vector3;
var forward = this.transform.TransformDirection(Vector3.forward);
var toOther = other.position - this.transform.position;
var impactAngle = Vector3.Dot(forward, toOther);
print(impactAngle);
var puckCurDir = puckCntrlScript.GetMoveDirct(); //puck's current direction
var returnDirct:Vector3;
if(isFrontBumper)
{
bumpBackDirct = this.transform.forward;
returnDirct = (bumpBackDirct * impactAngle)+ Vector3(puckCurDir.x,0,0);
}
else if(isBackBumper)
{
bumpBackDirct = -this.transform.forward;
returnDirct = (bumpBackDirct * impactAngle)+ Vector3(puckCurDir.x,0,0);
}
else if(isLeftBumper)
{
bumpBackDirct = this.transform.right;
returnDirct = (bumpBackDirct * impactAngle)+ Vector3(0,0,puckCurDir.z);
}
else if(isRightBumper)
{
bumpBackDirct = -this.transform.right;
returnDirct = (bumpBackDirct * impactAngle)+ Vector3(0,0,puckCurDir.z);
}
return returnDirct;
}