Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Player is not always looking directly at Pointer (Unity3D 2019.3.13f1)

Player is not always looking directly at Pointer (Unity3D 2019.3.13f1)

Hi, I am currently prototyping a 2.5D Top down Dungeon Crawler. I have set it up, so the player rotates to look at the pointer. In some cases it still seems that it still tries to rotate towards the pointer. However, the pointer is sometimes either offset, or not recorded correctly.

The bug occurs in 2 instances:

1)   When the player transition between rooms in the dungeon

2)   When the player uses the grapple gun a few times

In the embedded video the scenarios are shown:


I am using the new input system, which may be a source of this.

Below is the code I use to get the mouse position in world space.


For testing purposes to see what feels best I created two separate methods to set player rotation. The bug however occurs in both methods so I assume the issue lies the ‘GetRelativeCursorPosition()’ function.

The method used in video to highlight the bug:


Other method (used to smoothly rotate player)


I then call it in the update function when player is not grappling, so the player character looks in the direction they are grappling


 


Best Answers

  • MouledouxMouledoux Member
    edited June 30 Accepted Answer

    Instead of raycasting, try using "ScreenToWorldPoint" https://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html

    Just set the return value to be at the same level as your player. Either Y or Z, looks like Y from the gif. Alternatively, you could use the distance of the player from the camera to get that. Just pass it as the Z value when you call ScreenToWorldPoint.


    Edit: also, why not use LookAt instead of setting the rotation directly?

  • MouledouxMouledoux Member
    Accepted Answer

    You can actually still use LookAt if you give it "-Vector3.forward" I think it's backwards. You can give LookAt a second Vector3 argument that tells it which way is 'up' in your case, up would be backwards.


    I suspect with the raycast. Specifically that ray GetPoint function. Normally you would out the raycast hit, and get the point from there. Either that, or when you set the rotation directly. Unity advises not to directly set things like that, especially rotation, because quaternions are not as simple as vectors.

Answers

  • I tried and it worked thanks a lot.

    This is the new code

    Initially I tried using transform.lookat(), but it always ended up rotating the player along the x axis and making it look straight up at the camera.

    But with your suggestions it works perfectly

    Again thanks a lot.

    Do you have any suggestions to why my initial method did not work consistently?

  • Alright that's very helpful thank you

Sign In or Register to comment.