0
\$\begingroup\$

This is what happens now: https://www.dropbox.com/s/91u32eifzl3f8c5/Unity_Xx8GuSCuM5.mp4?dl=0

As you can see, it's not going towards the mouse position, sometimes vaguely.

Basically, to get the direction I want the ball to go in which is: Ball --------> Mouse Position, I'm casting a ray from the camera to the ground, based on the mouse position, and then substracting the intersection of that ray with the ground from the ball's position and storing that in a vector3 variable.

pointToHit = pointToLook - ball.transform.position;

My code:


    float rayLength;
    Vector3 mousePos;
    Vector3 pointToLook;
    Vector3 pointToHit;
    public GameObject ball;


    private void RotatePlayerToMousePos()
    {
        //Casts a ray from camera to the ground then stores the intersection of the ray with the ground in pointToLook
        cameraRay = mainCamera.ScreenPointToRay(mousePos);
        Plane groundPlane = new Plane(Vector3.up, Vector3.zero);

        if (groundPlane.Raycast(cameraRay, out rayLength))
        {
            pointToLook = cameraRay.GetPoint(rayLength);
        }

        //Rotates player to the positioned where the ray intersects with the ground
        transform.LookAt(new Vector3(pointToLook.x, transform.position.y, pointToLook.z));

    }

 private void HitBallToMousePos()
    {
        ball.transform.position = attackPointOrigin.transform.position;

        pointToHit = pointToLook - ball.transform.position;
        ball.GetComponent<Rigidbody>().velocity = pointToHit - ball.transform.position;

        

        //Maintains velocity in all directions.
        if (pointToHit.x > 0 && ball.GetComponent<Rigidbody>().velocity.x < 0)
        {
            ball.GetComponent<Rigidbody>().velocity = new Vector3(
                ball.GetComponent<Rigidbody>().velocity.x * -1,
                ball.GetComponent<Rigidbody>().velocity.y,
                ball.GetComponent<Rigidbody>().velocity.z);
        }
        else if (pointToHit.x < 0 && ball.GetComponent<Rigidbody>().velocity.x > 0)
        {
            ball.GetComponent<Rigidbody>().velocity = new Vector3(
                ball.GetComponent<Rigidbody>().velocity.x * -1,
                ball.GetComponent<Rigidbody>().velocity.y,
                ball.GetComponent<Rigidbody>().velocity.z);
        }

        if (pointToHit.z > 0 && ball.GetComponent<Rigidbody>().velocity.z < 0)
        {
            ball.GetComponent<Rigidbody>().velocity = new Vector3(
                ball.GetComponent<Rigidbody>().velocity.x,
                ball.GetComponent<Rigidbody>().velocity.y,
                ball.GetComponent<Rigidbody>().velocity.z * -1);
        }
        else if (pointToHit.z < 0 && ball.GetComponent<Rigidbody>().velocity.z > 0)
        {
            ball.GetComponent<Rigidbody>().velocity = new Vector3(
                ball.GetComponent<Rigidbody>().velocity.x,
                ball.GetComponent<Rigidbody>().velocity.y,
                ball.GetComponent<Rigidbody>().velocity.z * -1);
        }

        //Adds force to ball from hit
        ball.GetComponent<Rigidbody>().AddForce(pointToHit.normalized * hitForce, ForceMode.Impulse);
    }

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

This is a classic mix-up of points/positions and displacements/offsets/directions.

This is a point: a specific position in the world.

pointToLook = cameraRay.GetPoint(rayLength);

This is NOT a point. It is a displacement, a difference between two positions. It is not itself a position in the world, but an offset you'd apply to get from one position to another.

pointToHit = pointToLook - ball.transform.position;

So, name it something else, like:

offsetToMouse = pointToLook - ball.transform.position;

Now that we've renamed the variable, errors like this become easier to spot:

ball.GetComponent<Rigidbody>().velocity = offsetToMouse - ball.transform.position;

Uh oh. We're subtraction a position from something that isn't a position! This doesn't make any geometric sense. (In some game libraries, positions in space and offsets are even different data types so that errors like this will cause a type error and refuse to compile).

It looks like you forgot that you'd already subtracted the ball's position, so you subtracted it again. Effectively now you're computing the velocity you'd need if the ball were twice as far away from the origin as it actually is.

So what you really want is just this:

ball.GetComponent<Rigidbody>().velocity = offsetToMouse;

This computes the velocity the ball would need to reach the mouse in 1.0 seconds.

Or, if you want to use an impulse, you can use:

ball.GetComponent<Rigidbody>().AddForce(offsetToMouse.normalized * hitImpulse, ForceMode.Impulse);

Do not both set a velocity and add an impulse, or they'll stack in a way that will be harder for you to reason about. So pick one method or the other. (And if your variable represents an impulse, definitely don't call it a force).

And don't use any of the code you had in those if statements checking the signs of each component. You don't need it.

\$\endgroup\$
4
  • \$\begingroup\$ Ok, ball.GetComponent<Rigidbody>().velocity = offsetToMouse - ball.transform.position; works using addforce doesn't. This makes the ball go to the mouse position but if I multiply it stops working. I want the fly at high speeds. Am I not supposed to multiply vectors? \$\endgroup\$ Commented Mar 19, 2022 at 21:06
  • \$\begingroup\$ Depends what you're multiplying them by. You'll want to edit your question to show more context. \$\endgroup\$
    – DMGregory
    Commented Mar 19, 2022 at 21:17
  • \$\begingroup\$ Just by the float hitImpulse ball.GetComponent<Rigidbody>().AddForce(offsetToMouse.normalized * hitImpulse, ForceMode.Impulse); I guess now that I think about it, I'm not trying to make the ball just go to the mouse position but rather go in the direction of the mouse and multiply it's speed or maintain it. \$\endgroup\$ Commented Mar 19, 2022 at 21:50
  • \$\begingroup\$ If you want to maintain a specific speed in the direction of the mouse, setting the velocity is a good solution. \$\endgroup\$
    – DMGregory
    Commented Mar 19, 2022 at 22:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .