Home Unity

Scripting in Unity 3D

polycounter lvl 8
Offline / Send Message
Psyminator polycounter lvl 8
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

  • monster
    Offline / Send Message
    monster polycounter
    The CameraLookAt class works fine for me with a box. Just make sure your file is named CameraLookAt.cs.

    LookAtUnity.gif
  • pangaea
    Offline / Send Message
    pangaea polycounter lvl 5
    The name of the file shouldn't matter.

    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]
  • Tiles
    Offline / Send Message
    Tiles interpolator
    The name of the file shouldn't matter.

    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.
  • Psyminator
    Offline / Send Message
    Psyminator polycounter lvl 8
    Thank you all for the answers :)
    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.
  • Psyminator
    Offline / Send Message
    Psyminator polycounter lvl 8
    The axis of the pivot point must be wrong some how. Is there anyway to make it right?
  • Tiles
    Offline / Send Message
    Tiles interpolator
    To change the pivot orienation is something for your 3D software. But you could make an empty inside of the target object. And orieantate that one to your needs. Then target at this empty instead of the parent.
  • Psyminator
    Offline / Send Message
    Psyminator polycounter lvl 8
    I understand. The problem is the rotation offset seem to be almost at random. Or in some unique relation to scene global space. I have no idea how to adjust it to point in the right direction... Could always take a guess between 360x360x360 degrees, plus four decimals. Seem like a futile assignment.
  • Tiles
    Offline / Send Message
    Tiles interpolator
    Hmm. This doesn't sound like a pivot problem really then. Could you upload an example project?
  • Vailias
    Offline / Send Message
    Vailias polycounter lvl 18
    From what I recall the "lookat" always points the "forward" axis toward the object being looked at. This is its +z axis. (since unity is a Y = up system)
    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).
  • Psyminator
    Offline / Send Message
    Psyminator polycounter lvl 8
    I'm currently working as an intern for a company in Gotheburg, Sweden. Much om my material is highly confidential. Though this specific model has already been published on the market. Maybe there is a way I can take some screenshots without getting in trouble...

    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.
  • Psyminator
    Offline / Send Message
    Psyminator polycounter lvl 8
    Vailias wrote: »
    From what I recall the "lookat" always points the "forward" axis toward the object being looked at. This is its +z axis. (since unity is a Y = up system)
    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.

    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).

    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 :)
  • Richard Kain
    Offline / Send Message
    Richard Kain polycounter lvl 18
    Psyminator wrote: »
    I'm currently working as an intern for a company in Gotheburg, Sweden.

    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.
  • Vailias
    Offline / Send Message
    Vailias polycounter lvl 18
    Psymenator:

    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.
    using UnityEngine;
    using System.Collections;
    [ExecuteInEditMode]
    public class Test_LookAt : MonoBehaviour {
    
    	public Transform Target;
    	public Vector3 UpVector = Vector3.up;
    	
    	void LateUpdate () {
    	
    		if (Target != null) {
    			transform.LookAt(Target, UpVector.normalized);
    		}
    	}
    }
    
  • Psyminator
    Offline / Send Message
    Psyminator polycounter lvl 8
    Wow! Thank you for the help :)
    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
  • Vailias
    Offline / Send Message
    Vailias polycounter lvl 18
    Did you try adjusting the up vector away from the default value to see how it changes the rotation axis?

    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)
  • Psyminator
    Offline / Send Message
    Psyminator polycounter lvl 8
    I see... I tried to apply the script and I changed the up vector some, and the others as well just to check what's happening. But every time I play the scene it looks the same either way.

    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?
  • Vailias
    Offline / Send Message
    Vailias polycounter lvl 18
    Not sure exactly what the issue is in your case, however, I threw together a little debug info helper script today that should aid you in seeing what the axes are for your objects.
    This way you can see what is pointing at what.

    Get it here.

    The results look like this:
    DebugInfoResult.jpg

    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.
  • Psyminator
    Offline / Send Message
    Psyminator polycounter lvl 8
    Interesting... I'm not still sure of what's the problem. Too bad I have lots of other assignments for my internship for the moment. Don't get very much time over for this specific task.
    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.
  • Vailias
    Offline / Send Message
    Vailias polycounter lvl 18
    *deleted. wrong thread.*
Sign In or Register to comment.