playMaker

Author Topic: LookAt other object in 2d  (Read 10386 times)

Tibo

  • Playmaker Newbie
  • *
  • Posts: 5
LookAt other object in 2d
« on: April 22, 2014, 03:48:00 PM »
Hello,

I'm quite new to unity and Playmaker, but I have already used visual scripting softs.
My problem currently is that I am trying to have an object look at another object that will be moving in the scene.

I've seen some similar questions in the forum, but there were more for looking at mouse cursors.
I tried adapting those scripts, but I am not such a good coder, and get overwhelmed very quickly.

So I was wondering if someone would have a ready made script that I could use in playmaker for that, or if someone could point me towards another solution.

Thanks for your help.

Cheers.

Breadman

  • Full Member
  • ***
  • Posts: 185
  • Derp
Re: LookAt other object in 2d
« Reply #1 on: April 23, 2014, 07:06:46 PM »
You might not need a script. Try adding the "look at" action to an FSM attached to the object that does the looking. Set the target of the action to your other object.

Tibo

  • Playmaker Newbie
  • *
  • Posts: 5
Re: LookAt other object in 2d
« Reply #2 on: April 24, 2014, 01:16:34 PM »
I tried that, but my object then rotates on x and y axis when I need it to rotate only on the z axis and have the other 2 locked.

That's why I was trying to adapt some of the other solutions I found, but failed miserably :)

Breadman

  • Full Member
  • ***
  • Posts: 185
  • Derp
Re: LookAt other object in 2d
« Reply #3 on: April 24, 2014, 03:01:22 PM »
Attach a rigidbody, and use the "constrain" checkboxes in the inspector, to stop it from rotating in the non-desired axes.

Alternatively, use the "set rotation" action and set the axes you want to 0, and check the "every frame" box.

sebaslive

  • Hero Member
  • *****
  • Posts: 572
    • Frame Tale Studios
Re: LookAt other object in 2d
« Reply #4 on: April 24, 2014, 05:17:00 PM »
Hey, check out this reply from another page, it might help.

http://hutonggames.com/playmakerforum/index.php?topic=6634.msg32381#msg32381
All my VR games use Steam VR Playmaker Toolkit: http://u3d.as/u20
And Oculus Touch Playmaker Toolkit: http://u3d.as/QT5


Follow me @sebasRez

collidernyc

  • Playmaker Newbie
  • *
  • Posts: 9
Re: LookAt other object in 2d
« Reply #5 on: July 01, 2014, 11:39:37 PM »
Hi all. I needed some LookAt 2D eyeballs and was surprised there was no dedicated FSM. It didn't take very long to work up a Unity C# script, but I figured it would be useful to have this in PlayMaker, so I hacked together an FSM. It seems to work pretty well, but this is my first attempt at one of these, so let me know if there is anything wacky I'm doing.
It should be pretty straightforward... choose the object you want to do the looking, choose the target, set a rotation offset if you need it (by default 0 is for an object facing right). You can set the offset to any amount (have objects look away from the target, etc). There is also a debug draw (very handy).
Hope this is useful for someone :)

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2013. All rights reserved.

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Transform)]
[Tooltip("Rotates a Game Object on it's z axis so its forward vector points at a Target.")]
public class LookAt2D : FsmStateAction
{
[RequiredField]
[Tooltip("The GameObject to rotate.")]
public FsmOwnerDefault gameObject;

[Tooltip("The GameObject to Look At.")]
public FsmGameObject targetObject;

[Tooltip("Set the GameObject starting offset. In degrees. 0 if your object is facing right, 180 if facing left etc...")]
public FsmFloat rotationOffset;

[Title("Draw Debug Line")]
[Tooltip("Draw a debug line from the GameObject to the Target.")]
public FsmBool debug;

[Tooltip("Color to use for the debug line.")]
public FsmColor debugLineColor;

[Tooltip("Repeat every frame.")]
public bool everyFrame = true;

private GameObject go;
private GameObject goTarget;
private Vector3 lookAtPos;



public override void Reset()
{
gameObject = null;
targetObject = null;
debug = false;
debugLineColor = Color.green;
everyFrame = true;
}

public override void OnEnter()
{
DoLookAt();

if (!everyFrame)
{
Finish();
}
}

public override void OnUpdate()
{
DoLookAt();
}

void DoLookAt()
{
go = Fsm.GetOwnerDefaultTarget(gameObject);
goTarget = targetObject.Value;

if (go == null || targetObject == null)
{
return;
}

Vector3 diff = goTarget.transform.position - go.transform.position;
diff.Normalize();

float rot_z = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;
go.transform.rotation = Quaternion.Euler(0f, 0f, rot_z - rotationOffset.Value);

if (debug.Value)
{
Debug.DrawLine(go.transform.position, goTarget.transform.position, debugLineColor.Value);
}

}

}
}

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: LookAt other object in 2d
« Reply #6 on: July 02, 2014, 01:37:26 PM »
Hi,

 Indeed thanks for that. I have already adpated your code and will be featured in the next package for Unity 2d. I created two variants, once using a gameobject as the target like you did and one where you can define a vector2 or vector3 ( kept the offset, very clever! ), and the debug line is indeed very handy.

 Keep up!! If you want to participate more and publish directly to the ecosystem let me know. I am completly overloaded at the moment :)

 Bye,

 Jean

collidernyc

  • Playmaker Newbie
  • *
  • Posts: 9
Re: LookAt other object in 2d
« Reply #7 on: July 02, 2014, 05:51:51 PM »
Wow, didn't expect that, thanks Jean! I actually had vector3 in there, but never hooked them up since I didn't need them - guess I could have saved you a few minutes work :) I'm still barely scratching the surface of PM (I only started using it a few days ago after a long hiatus - I'm still going through tutorials here) but I'm more than happy to post anything I think is useful as I move along.

As far as publishing directly... I seriously don't think you want me doing that :) I'm more of an artist than programmer, though at this point I probably know more than I think I do.

Maybe this should be in a separate thread - but I was wondering if there was an easy way to convert vector3 to vector2 (Ray Cast 2D takes vector2 for position). AFAIK there is no obvious way, but I could be missing something. If not, that was the next thing I was going to quickly tackle...

collidernyc

  • Playmaker Newbie
  • *
  • Posts: 9
Re: LookAt other object in 2d
« Reply #8 on: July 02, 2014, 07:27:24 PM »
Forget that last part about v3 to v2 - was super easy to whip up the FSM.

http://hutonggames.com/playmakerforum/index.php?topic=7769.0

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: LookAt other object in 2d
« Reply #9 on: July 03, 2014, 12:54:19 AM »
Hi,

 again, ahead of the game, your vector3 to vector2 is a very handy action and will make it to to the Unity 2d package.

 It's not about how much you know, creating custom actions is not difficult, it's about finding what action could make fsm building easier while being generic enough.


 Bye,

 Jean

collidernyc

  • Playmaker Newbie
  • *
  • Posts: 9
Re: LookAt other object in 2d
« Reply #10 on: July 03, 2014, 06:42:23 PM »
Jean, I just figured out what you meant by Ecosystem! Sure, sign me up. I had no idea that it was a separate, easy way to download custom actions... it looks fantastic! Up until now I had been scrounging around the wiki and the forum (the forum has been super wonky for searches the last couple days). I'll have to make sure to check these before I write any more :)
Is there a way in the browser to view all, or view new... or do you have to do a specific search each time?
I'm slowly realizing how much time you're putting into all of your projects here (ecosystem, array maker, forum dude, etc...). Thank you for all the hard work!

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: LookAt other object in 2d
« Reply #11 on: July 05, 2014, 04:48:25 AM »
Hi,

 I am travelling atm, can you get back to me the second half of July, I will have more time to introduce you on how you can help us out and contribute for the ecosystem.

The ecosystem aims at solving exactly that. It's very rought at the moment, being a first draft basically, so things will move forward and new features will be added. You need to know what you want within the ecosystem by filtering the search, free browsing is not really implemented. You can search with an empty search string, but it will only return the first "page" of results ( a github restrictions).

Bye,

 Jean

WelchComp

  • Playmaker Newbie
  • *
  • Posts: 3
Re: LookAt other object in 2d
« Reply #12 on: July 26, 2014, 05:09:43 PM »
This snippet of code worked beautifully, thank you!

I'm now trying to find a similar solution for having my player in a top down shooter look in the direction Im moving. Seems to be a lot of info on how to do it in a 3D game but all of the solutions I've seen cause that rotate-on-the-wrong-axis-problem that you fixed here.

Any tips on a fix for this?
« Last Edit: July 26, 2014, 05:11:21 PM by WelchComp »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: LookAt other object in 2d
« Reply #13 on: July 30, 2014, 08:04:10 AM »
Hi,

 I have added "LookAt2dGameObject" and "SmoothLookat2d" on the ecosystem. Have you played with that already?There is also the LookAt2d that should be actually enough to get your player to look where you want in 2d.

Bye,

 Jean

RC

  • Full Member
  • ***
  • Posts: 148
Re: LookAt other object in 2d
« Reply #14 on: December 01, 2014, 09:32:38 AM »
Hi, I'm sorry to have to bring this back up, but I can't seem to get this code to working right for me.

So this is my problem, my enemy is facing right so I set the degree to 0. It's looks right fine, but looking left , it's flipped upside down.

I was looking through the code and I was wondering how do I change Z axis to Y axis for rotation.

Any input would be much appreciated. Thank you!
« Last Edit: December 01, 2014, 10:58:32 AM by rongconcrx »