Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

camera following a parent without inheriting rotation?

Discussion in 'Scripting' started by jdams, Oct 25, 2010.

  1. jdams

    jdams

    Joined:
    Sep 13, 2010
    Posts:
    64
    I have a unique issue. Maybe someone out there can help. I have a maze game that involves a player navigating a 3d maze which is wrapped around a sphere. Imagine Super Mario Galaxy meets that David Bowe movie Labyrinth. My issues comes in the form of an overhead camera. When I place the camera as a child of the moving player it rotates with the player. But this causes it to appear as though the map is moving and not the player. I would like the camera to stay constantly positioned above the player but without mimicing the rotation. Is that possible?

    I have already tried the transform.LookAt(); from the scripting ref, as well as trying to disconnect the parent to child relationship.Niether was successful. I believe the problem is that the player rotates to stay aligned to the planet's surface but rotates on it's local X axis to manuever. Can I lock out just the x axis in the parent to child relationship?

    If you need examples or whatever let me know and thank you for you help.
     
  2. nalim

    nalim

    Joined:
    Apr 13, 2010
    Posts:
    118
    this is really simple in the standard assets there is a SmoothFollow.js this javascript you have to give at the camera anf then give it a target
    thats the player and this nice script has made the axis as variable so you can say how high and far away the camera haves to stay.
    i think this will solve it. say it if you dont understand, if it didnt work or you want to know more about it.
    i will really like to help you ! if this is not excactly what you want then i think i misunderstand it and i will try to help you again.
    ,nalim
     
  3. Akta

    Akta

    Joined:
    Oct 15, 2010
    Posts:
    119
    I'd write a script for the camera where basically you position it according to the target object position. In that way you avoid the rotation due to the parenting :)
     
  4. jdams

    jdams

    Joined:
    Sep 13, 2010
    Posts:
    64
    To Nalim - I have attempted to us that script but it does not work. When my player passes over the "equator" of the planet it flips upside down, and the controls seem reversed. To akta - how exactly could I set my camera to always align to my player's Y axis, and not follow it's rotation? Thanks for your quick replies and again for your help.
     
  5. Akta

    Akta

    Joined:
    Oct 15, 2010
    Posts:
    119
    something like this:

    Code (csharp):
    1.  
    2.  
    3. var targetObject : GameObject;
    4. var offsetX : float = 10.0;
    5.  
    6. function Update() {
    7.      transform.position.x = targetObject.transform.position.x - offsetX;
    8. }
    9.  
    10.  
    if you apply this script to your camera and add a targetObject, the camera x position will move according to the targetObject x minus the offsetX. The offset is needed, otherwise the camera will have the exact position as the target object. Hope you got the idea :)
     
  6. boomcrash

    boomcrash

    Joined:
    Aug 31, 2010
    Posts:
    72
    This works really well to lock the camera above the player object, but want I move East or West it does really odd things. Any suggestions on what player controls should be used?