Hello everyone,
I’m currently creating a VR Hockey Goalie game. To give a brief explanation on how I have it set up, when the gaze is on the puck and the user presses a button (or clicks on PC), the game object that is supposed to stop the puck will go to the puck’s end position which is determined when the player shoots it. Here is how I have it set up:
This function determines where the player will shoot the puck on the net:
public Vector3 RandomNetArea()
{
float y = Random.Range(netArea.bounds.min.y - 0.5f, netArea.bounds.max.y + 0.5f);
float z = Random.Range(netArea.bounds.min.z - 0.5f, netArea.bounds.max.z + 0.5f);
Vector3 shootSpot = new Vector3(transform.position.x, y, z);
return shootSpot;
}
This part of the script gets the above function, then trigger’s the puck to move towards the position on the net when the player shoots it:
void Update ()
{
if (movePuck)
{
ShootPuck();
}
}
void ShootPuck()
{
rb.AddForce((shootSpot - transform.position).normalized * speed);
}
void AimPuckAtNet()
{
shootSpot = scoreDetect.RandomNetArea();
Debug.Log(shootSpot);
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Stick")
{
AimPuckAtNet();
ShootPuck();
movePuck = true;
Destroy(gameObject, 6f);
}
if (other.tag == "Net" || other.tag == "Player" || other.tag == "Post")
{
movePuck = false;
rbGravity = true;
}
}
This function is attached to an event trigger on the puck which moves the player’s blocking object to the correct position to block the puck, of course, the farther the puck is away when the user interacts with it, the more accurate the save:
public void SaveThePuck()
{
if (greatPC.puckInCollider)
{
player.transform.position = new Vector3(player.transform.position.x, scoreDetect.RandomNetArea().y, scoreDetect.RandomNetArea().z);
}
else if (goodPC.puckInCollider)
{
player.transform.position = new Vector3(player.transform.position.x, scoreDetect.RandomNetArea().y, scoreDetect.RandomNetArea().z);
player.transform.localScale = goodSize;
}
else if (okayPC.puckInCollider)
{
player.transform.position = new Vector3(player.transform.position.x, scoreDetect.RandomNetArea().y, scoreDetect.RandomNetArea().z);
player.transform.localScale = okaySize;
}
}
My problem is that the player object does not accurately go where the puck is going to end up on the net. I have a feeling this has to do with the way force is added to the puck, but I am not entirely sure. I have tested the RandomNetArea() function to see where the puck is going, while checking the actual puck game object in the editor to see if they match up, and it doesn’t seem like they do. Is there a better way I could add force to the puck so it is accurate, while keeping it realistic? Thanks for your help in advance.