Hi all!
I need some help with a simple LookAt script in Unity. I want just this rectangular cube to always point at a certain object. But it's not very easy for a new beginner. I'm quite experienced with graphics in Unity. Just not very good at scripting. And I need this for a job really bad.
I started out by using this script from Unity's tutorial. But I think there's some problem with defining classes of some sort. This one is made for cameras.And I need it for simple object like geometry .fbx. But I don't know what. It just can't be applied to any object:
using UnityEngine;
using System.Collections;
public class CameraLookAt : MonoBehaviour
{
public Transform target;
void Update ()
{
transform.LookAt(target);
}
}
Then I tried to copy the script direct from Unity's scripting API page. But it won't do any good:
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public Transform target;
void Update() {
transform.LookAt(target);
}
}
I would love any answer I can get. This is really important for me. Thinking maybe I should get serious with scripting and get some kind of course for it...
Thanks in advance.
Replies
OP, can you have a look at these tutorials http://catlikecoding.com/unity/tutorials/ in particular, if you understood the first clock tutorial you should easily be able to do what you are asking.
Just do the catlike coding tutorials above and then look at this rogue like tutorial.
[ame]https://www.youtube.com/watch?v=Fdcnt2-Jf4w[/ame]
Mh, it does matter. It's c#, and in c# the name of the file has to match to the class that it contains. Monster hinted already at it.
It worked fine when I changed the name of the class to 'jointLookAt' and then the same for the script name. Because it's for rigging these bones and joints in different sorts of machines.
The problem still is that it seem to point -90° at aside. Not directly towards the actually target. Though it moves as if it were right together with the motion in the animation.
Also of note what I just found out, the axis of your OBJECT is preserved on import, even if it is pointing the right way in the scene. This may be overruled in hierarchies, but be sure to check which axis is pointing at what.
Also look at this: http://docs.unity3d.com/ScriptReference/Transform.LookAt.html
If you change your lookat class to have a parameter for up vector, you can change its rotational behavior. Useful if what you're rotating has a different up vector than the world up (y).
I always try to keep the pivot in local not global coordinates when applying the script. Then when I hit play and the animation kicks in, it all points in wrong direction. It's really weird. Though from early experience when animating it may have something to do with the parent object's local coordinates disturbing the direction of the child object's direction? Maybe, I'm not sure.
Sounds like a really good hint. The problem I'm experiencing may have something to do with preserved axis coordination from the import.
The up vector should always be the same from Maya to Unity. Though maybe it can be helpful. I'm not that good at scripting though.
Thanks for the answers
Ah, I love Gotheburg. One of the few cities I've visited outside of the U.S. A really great city for touring around on foot. Lower buildings, and lots of great parks. Very pretty architecture. A more laid-back feel compared to Stockholm.
As to Unity, you are going to want to keep your transformations in local space. You are also going to most likely want to alter the eulerAngles of your game object's transformation component. (a Vector3 value) How those eulerAngles will be calculated should be based on the relative positions of your rotated object and the object it is supposed to be pointing at.
I would also advise exposing your target game object as a public variable in your script, and assigning it in the editor. You're probably doing this already, but I just find it to be convenient.
I did something similar for a sprite class I wrote and needed billboard support for. It was easier for me because all I had to do was copy the eulerAngles from the currently selected camera. But it worked. With a few more calculations I'm sure it could work for you as well.
Here's one way to implement what I was talking about.
This can of course be altered and extended, but the upvector option should help illustrate how the "lookat" routine works, and you can adjust things and see how they change.
I tried it. Unfortunately it didn't do it. I think there have to be something wrong with the parent axis making the child point completely out of order.
Check out this site for a detailed description if you want.
http://cargocollective.com/albertakesson/ISSUE
Just wanted to check that since the script was supposed to be an illustration of the concept rather than specifically solve your issue.
Second: What is your hierarchy? Are the side piston bits separate objects?
If so, you can try the old trick of maintaining their transform while resetting the movement axis by creating empty game objects, parenting those to the body of the dumper at the point where the hinges would go, then parenting the pistons to those game objects. Then, rather than having the pistons look at each other, have their parents look at each other. (you should likely adjust the parents' rotations to be roughly pointing at each other before adding the other objects as children)
All object in the hierarchy are linked together. So there is a chain of parent/child relations all the way. Only I'm pretty insecure about how this might affect what happening. It seem probable this might have something to do about it.
The whole hierarchy has been put together in Maya. Perhaps is's required to be reassembled in Unity? At least to check what's going on?
This way you can see what is pointing at what.
Get it here.
The results look like this:
Bright axes are local.
Darker axes are world.
Yellow is collision primitives.
Magenta (not pictured) are triggers.
Grey is the current object bounds.
All individually toggleable.
I'll post the source if you need it.
I really hope I can solve the problem. I love making realtime graphics. It's just all this stuff about scripting and so on...
But I thank you very much for all the help and good will. This community is even better than Unity's own in many ways when it comes to asking questions. People seem to be more open for solutions.