Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help  Grappling hook with tilted camera
#5
Hi,

The raycast calculations assume a fully 2D scene. So the ray starts at the character, and is always contained inside the XY plane relative to the camera, as stated in the code comments. If you wanted a more general calculation that uses a world space XY plane (for a 2.5D game), you could calculate the actual intersection of the mouse ray and the XY plane (untested code), and use that as the endpoint to raycast from the character:

Code:
private void LaunchHook()
    {

        // Get the mouse position in the scene, using the world space XY plane with z = 0:
        var plane = new Plane(Vector3.forward, Vector3.zero);
        var ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        float enter;
        if (plane.Raycast(ray, out enter))
        {
            Vector3 mouseInScene = ray.GetPoint(enter);
            // Get a ray from the character to the mouse:
            ray = new Ray(transform.position, mouseInScene - transform.position);

            // Raycast to see what we hit:
            if (Physics.Raycast(ray, out hookAttachment))
            {

                // We actually hit something, so attach the hook!
                StartCoroutine(AttachHook());
            }
        }

    }

This would allow you to place the camera anywhere in the scene.
Reply


Messages In This Thread
RE: Grappling hook with tilted camera - by josemendez - 20-08-2020, 08:28 PM