DOLookAt in 2D
« on: June 19, 2015, 10:48:41 AM »
Hello, maybe an expert can help me out.  :o

I am trying to get DOLookAt to work in 2D, I tried a bunch of combinations but cannot get it to work. I have a gun turret in a top down shooter type game and it is placed at the very bottom of the screen. There is gun barrel on the turret pointing to the top of the screen. When the user clicks on the screen I would like the turret to rotate on the Z axis and point towards where the user clicked, so it looks like the gun will shoot in that direction. I am using the transform for the rotation, there no rigidbody attached to the object, not sure if that matters.

I am not sure which parameter values to enter to get this to work, the constraints seem to work on all but the Z axis which is messing up a 2D view. I have attached an example of what the screen looks like and drew the x axis and y axis.

« Last Edit: June 19, 2015, 11:00:58 AM by jprocha101 »

Re: DOLookAt in 2D
« Reply #1 on: June 19, 2015, 05:45:26 PM »
Have you tried it yet with a rigidbody attached though?  From the DOTween doco:

Rigidbody
These shortcuts use rigidbody's MovePosition/MoveRotation methods in the background, to correctly animate things related to physics objects.


If you're worried about physics making your objects fall about, make sure to check Is Kinemetic true in the rigidbody settings.

The other thing I would suggest is making sure your Z value in the "towards" Vector3 is the same as that of the plane on which your object is sitting.

Re: DOLookAt in 2D
« Reply #2 on: June 21, 2015, 10:05:20 AM »
Have you tried it yet with a rigidbody attached though?  From the DOTween doco:

Rigidbody
These shortcuts use rigidbody's MovePosition/MoveRotation methods in the background, to correctly animate things related to physics objects.


If you're worried about physics making your objects fall about, make sure to check Is Kinemetic true in the rigidbody settings.

The other thing I would suggest is making sure your Z value in the "towards" Vector3 is the same as that of the plane on which your object is sitting.

@cynicalwanderer 
Thank you for helping me out. I was able to use Rotate instead. I just calculated the rotation using Mathf.Atan2 and did the tween using that result. I found some other people using other assets and those asset's version of LookAt and they all seem to have issues with 2D. It ends up rotating on 2 axis at some point, usually when the target is directly above or below the rotating object. But 2D should (usually) rotate along the z-axis only.  8)

public void GoToPoint(Vector3 targetPos)
    {
        transform.DOKill();

        var dir = targetPos - transform.position;
        var angle = Mathf.Atan2(dir.y, dir.x)*Mathf.Rad2Deg;
        var rot = Quaternion.AngleAxis(angle, Vector3.forward);

        transform.DORotate(rot.eulerAngles, 100).SetEase(Ease.Linear).SetSpeedBased()
            .OnComplete(() => transform.DOMove(targetPos, 10).SetEase(Ease.Linear).SetSpeedBased());
    }

Re: DOLookAt in 2D
« Reply #3 on: June 23, 2015, 11:01:29 AM »
Cool, that's a clever solution.  Glad it worked out for you.