Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help  Grappling hook with tilted camera
#1
I noticed that grappling hook in the grappling hook demo does not seem to work if I tilt the camera. Hook does not connect to the objects on the scene. Huh
Reply
#2
(13-08-2020, 09:51 AM)illustrator Wrote: I noticed that grappling hook in the grappling hook demo does not seem to work if I tilt the camera. Hook does not connect to the objects on the scene. Huh

Hi,

I'm unable to reproduce this. Also, I don't see how camera placement/orientation could affect the physics simulation, as it is not taken into account at all...except for camera culling, which disables the simulation entirely when it is offscreen. To force the simulation to run even when not visible, enable "simulate when invisible" in the solver. Should be enabled by default though:
http://obi.virtualmethodstudio.com/tutor...olver.html


If this is not the cause of your issue, can you share a video of it?
Reply
#3
(13-08-2020, 10:02 AM)josemendez Wrote: Hi,

I'm unable to reproduce this. Also, I don't see how camera placement/orientation could affect the physics simulation, as it is not taken into account at all...except for camera culling, which disables the simulation entirely when it is offscreen. To force the simulation to run even when not visible, enable "simulate when invisible" in the solver. Should be enabled by default though:
http://obi.virtualmethodstudio.com/tutor...olver.html


If this is not the cause of your issue, can you share a video of it?
Here is camera with no tilt. Every click gets hook attached and rope gets attached to same object "depth", I mean not on the back side or front side of the object.
https://youtu.be/2harf1qQujU

and here is camera with tilt. I'm constantly clicking and only some of the clicks get hook attached and also the rope attach position changes from backside of the object to the front side of the object when mouse click position goes higher.
https://youtu.be/UAz45Tcqu-A

What comes to my mind is that is the camera clipping plane or some other camera plane somehow affecting the calculations? See attachment: Red dots are mouse click positions. Green is world up axis. Two thin blue lines are ray casts. When mouse clicks go lower the hook attach point moves away and when clicks are higher the attach point moves towards camera until it does not attach at all because ray does not hit the object. When camera is not tilted all clicks are on the green line and all hooks attach.

I think I should mention that I'm using Cinemachine to adjust the camera angle.


Attached Files Thumbnail(s)
   
Reply
#4
I come back to this thread and try to give better examples. See images.

With tilted camera hook attaches to different points in z-depth if I click lower or higher (I don't click the object..).

When camera is not tilted hook attaches always to same z-depth.

        // Get the mouse position in the scene, in the same XY plane as this object:
        Vector3 mouse = Input.mousePosition;
        mouse.z = transform.position.z - Camera.main.transform.position.z;
        Vector3 mouseInScene = Camera.main.ScreenToWorldPoint(mouse);

Maybe I have misunderstood how this script should work when it calculates the attach point.. Does it affect the screen point calculations if user clicks on the "sky"?


Attached Files Thumbnail(s)
           
Reply
#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