Search Unity

Hockey Puck Movement

Discussion in 'Scripting' started by tburns517, May 31, 2016.

  1. tburns517

    tburns517

    Joined:
    Jan 12, 2016
    Posts:
    48
    Hello everyone,

    I trying to create a simple VR Hockey Goalie game. Right now I am working on the script for puck movement. The players will always remain stationary. When it is their turn to shoot they will pick a random spot on the net and shoot there. My net has a box collider, which I am referencing in a script called ScoreDetect attached to the net. This function gets a random location within the box collider (net) bounds.

    Here is the function/code for that. If you see anything wrong please let me know:

    (netArea is referring to the box collider)

    Code (CSharp):
    1.     public Vector3 RandomNetArea()
    2.     {
    3.         float y = Random.Range(netArea.bounds.min.y, netArea.bounds.max.y);
    4.         float z = Random.Range(netArea.bounds.min.z, netArea.bounds.max.z);
    5.         Vector3 shootSpot = new Vector3(transform.position.x, y, z);
    6.         return shootSpot;
    7.     }

    Next is the actual puck movement, which has a script attached to the puck prefab. For the movement function, I am referencing the RandomNetArea() function from the other script and storing the Vector3 that it returns into a new Vector3 called shootSpot as well. I simply get the rigidbody of the puck and add force to it with the location:

    Code (CSharp):
    1.     void ShootPuck()
    2.     {
    3.         Vector3 shootSpot = scoreDetect.RandomNetArea();
    4.         rb.AddForce(shootSpot * speed);
    5.     }

    As of now, the rigidbody of the puck has a mass of 0.5 and uses gravity. The problem is that the puck is always shot straight at the net and does not get off the ground. When I uncheck use gravity the puck does get off the ground a little more, but then it hovers and that isn't very realistic. Is there another way that I should be doing this, or am I missing something? Thanks for all of your help in advance.
     
    Last edited: May 31, 2016
  2. tburns517

    tburns517

    Joined:
    Jan 12, 2016
    Posts:
    48