Search Unity

Surface Tracker Script Problems

Discussion in 'Scripting' started by marty, Jun 9, 2005.

  1. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    I'm tryin to write a script which I would like to use to have one object "track" the surface of another, based on user navigation.

    The script is attached to the tracking object and allows that object to track a terrain object, which has a sphereCollider attached to it. Imagine an ant walking around on the surface of an orange - it should work something like that.

    [original code snippet deleted to save space, see latest version below]
     
  2. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    You've got that right with the coordinates - you are casting the ray downwards in worldspace... What you want is to transform the ray FROM your ant's local space into worldspace...

    Try sth likt this:

    replace "if (Physics.Raycast (transform.localPosition, Vector3.down, hit))"

    with:

    if (Physics.Raycast (transform.localPosition, Transform.TransformDirection (Vector3.down), hit))

    That will make the transformcomponent rotate the down vector from localspace into worldspace....

    Hope this helps...

    Nicholas
     
  3. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Thanks, Nicholas!

    Your solution has my tracking working perfectly, as long as I disable rotational control by the player. Which is a great improvement - your raycast solution did the trick!

    The thing is, when I re-enable rotating the player, the rotation seems to still need some help. Apparently, the tracking object isn't quiet rotating about its Y-axis, which is what I need.

    Do I need to translate the rotation of my object from local space to worldspace as well? And if so, how do I do that? I'm still learning my way around the Unity 3D math API.

    Any ideas?
     
  4. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    The simplest way is to create a child transform and rotate that.

    So you just do all translation and tilting of the rotation as you do already.

    Then you add a transfrom child to it and attach the MouseLook script to the child.
     
  5. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Thanks for getting back, Joachim, buy I'm not sure I follow you.

    How do I make a child transfrom in Unity? Can I use a non-rendering dummy object maybe? Can you make dummies in Unity?

    Also, I'm using the keyboard to control the player, so I'm not sure how a Mouse-movement based script would help?
     
  6. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    I see, didn't get that you were using a keyboard control.

    To create a non-rendering dummy just use the: GameObject-> Create Empty menu command.

    Any game object in unity can be parented. Just use the hierarchy view and drag one game object onto another.

    So the whole solution is:

    - Create a dummy
    - Make the ant with graphics a child of the dummy.
    - Attach the script you wrote to the dummy. The script shouldn't take care of the rotation around the y axis.

    Code (csharp):
    1.  
    2. // cast a ray down from the player object
    3. var hit : RaycastHit;
    4. if (Physics.Raycast (transform.localPosition, transform.TransformDirection (Vector3.down), hit)) {
    5. // if (Physics.Raycast (transform.localPosition, Vector3.down, hit)) {
    6. // put the player object where the ray hit the terrain object
    7. transform.localPosition = hit.point;
    8. // orient the player object with the terrain surface normal
    9. // only change the X and Z axes, since the Y-axis is rotated by the player
    10. transform.localRotation = Quaternion.FromToRotation (Vector3.up, hit.normal);
    11. }
    12.  

    - A new script like this:
    Code (csharp):
    1.  
    2. function Update ()
    3. {
    4.    transform.RotateAroundLocal (Vector3.up, Input.GetAxis ("Horizontal"):
    5. }
    6.  
    attach that to the child of the dummy, which in your case probably also contains the ant's mesh.

    It is also possible to write a script that doesn't require the dummy. But this seemed simpler. Tell me if you need it without a dummy.
     
  7. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    I'd love to eliminate the processing overhead of using a dummy object. :wink:

    When you get a chance, I'd love to see that non-dummy example!
     
  8. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    There is no measurable performance overhead in using dummy objects....
     
  9. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    Yeah one big thing with optimization is that you should really make sure you are spending your time in the right place. I've never attached Shark or OpenGL Profiler to Unity but I've been finding with Unity just do whatever works the best for you, the developer, and the vast majority of the head scratching has already been figured out by our OTEE friends.

    That said, keeping model polygon counts as low as you can get away with and making sure models are "smooth" shaded instead of flat shaded helps on the speed of the graphics end.

    Cheers,
    -Jon
     
  10. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    The big optimization tip is to use as few meshes as possible. We currently have some pending optimization work in this area, but with all decent 3D engines that is the limiting factor. You can push large meshes with no prob, but the object count is the limiting factor. Not empty count or any such things, but how many meshes we have to submit to the GFX card.
     
  11. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    I've always heard it put this way: "better to have 1 1000-polygon mesh than 1000 1-polygon meshes".
     
  12. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    Ah must be doing the whole Display List thing. What about for objects that are not visible on the screen? You are doing frustrum culling so they are not drawn, right? Would it be worth my time to enable/disable static background objects that are far away in the distance in my side-scroller?

    Cheers
    -Jon
     
  13. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    Not display lists (they take forever to upload 1st time), but we do everything possible to speed up geometry...

    We do full frustum culling, so for your the side scroller, the only reason to disable faraway objects would be to avoid some AI overhead. I'm currently doing sth like it in my top-down shooter.
     
  14. Browdaddy96

    Browdaddy96

    Joined:
    Aug 27, 2015
    Posts:
    82
    man this is an old thread