Object Aim On Angle Toward Target - Not Accurate

Hello,
I am creating a simple VR Hockey Goalie game. I have three players set up in the formation of a half circle, all facing the goalie (player). The way the code works is that it selects a player to shoot, gets a random location on the net, then the player shoots the puck towards that location. If the player aims the gaze at the puck and clicks (on PC for now), the player object moves to the position and stops the puck from entering the net.

Now for the problem…

This set up works great for the player aiming straight at the net (middle hockey player), however, for the characters aiming at somewhat of an angle, the puck sometimes misses the net. If the player successfully tries to stop the puck, the player object moves, but not to the right position which causes a goal to be scored.

Here is all the code:

netArea is referring to a box collider on the net

    public Vector3 RandomNetArea()
    {
        float x = (netArea.bounds.max.x + 2);
        float y = Random.Range(netArea.bounds.min.y, netArea.bounds.max.y);
        float z = Random.Range(netArea.bounds.min.z, netArea.bounds.max.z);
        Vector3 shootSpot = new Vector3(x, y, z);
        return shootSpot;
    }

I move the puck with two simple lines of code

    void ShootPuck()
    {
        Vector3 velocity = (shootSpot - rb.position).normalized * speed;
        rb.MovePosition(rb.position + velocity * Time.deltaTime);
    }

Here is a visual representation of how I have it set up:

I would appreciate any help you guys can give me!

bump

in your first method, you are stating X + 2, which leads me to concern that x (which is left to right) is not left to right.

the second part, states that you are using MovePosition (which is not really the best way) I would use:

rb.velocity = velocity;

Thanks for getting back to me bigmisterb. In this case, X is the direction going towards the net. I know this is probably not logically correct. I probably should have paid attention to that in the beginning, but this is the first game I am making by myself.

I changed the MovePosition line to rb.velocity = velocity and it works well. Any other ideas as to what could be the issue? Basically, the player is aiming at box collider (net) at an angle, but I’m still not sure how that could be an issue.

You are using the bounding box of the mesh to define the target. The problem is the bounding is bigger than the mesh and does not have a physical significance (it’s a rough approximation).

I’d suggest to use to goal line to generate your random target point, this way your are sure that the puck ends up in the net.

        Vector3 lineStart;
        Vector3 lineEnd;

// ...

        // Generate a random point on the line.
        Vector3 targetPoint = lineStart + Random.value * (lineEnd - lineStart);

        // Get the shooting direction.
        Vector3 shootingDirection = (targetPoint - this.transform.position).normalized;

Thank you for your suggestion, ericbegue. Just to be clear, you’re saying that a box collider positioned inside the net is not going to be an accurate way of determining where to shoot the puck on the net? Just to give a little bit more detail, the box collider is a trigger, and it is scaled to be slightly smaller than the net, just inside the posts. I use an OnTriggerEnter() function to determine if the puck is in the net or not. It seems to be working successfully with that.

Hopefully this picture is clear enough. This is just showing the corner of it. This box collider is referenced in a script so I can get an area within the bounds of it. It works really well with the hockey player facing straight on, so I guess I am a little confused as to why it is not working with the players on an angle.

With the line technique that you mentioned, I can understand how you would be able to get a position on the X axis (or Z in my case), but what about the Y axis?

I assumed the puck was moving only on the ground. But it’s easy to modified this technique to consider the up direction aswell: you just have to add a random offset in the up direction with the goal height as maximum magnitude.

Adjust the box for the width of the puck inside. Even so, if you give a destination of somewhere inside the box, the puck should not go outside. Given your situation though, I would only use the front entry point, not inside the goal as you have it. That way, there is no depth, there is only X(Z) and Y to deal with.

var bounds = netArea.bounds;
bounds.SetMinMax(b.min + puck.bounds.extents, b.max - puck.bounds.extents);
var x = netArea.bounds.center.x;
var y = Random.value * bounds.max.y + bounds.min.y;
var z = Random.value * bounds.max.z + bounds.min.z;