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.

where is he facing at?

Discussion in 'Scripting' started by dacloo, Feb 16, 2006.

  1. dacloo

    dacloo

    Joined:
    Jun 30, 2005
    Posts:
    469
    I got this real simple camera script.
    The script follows a rolling sphere.
    I want to find out in which direction the sphere is currently moving,
    and place the camera behind the sphere.

    Code (csharp):
    1.  
    2. var objectToFollow : Transform;
    3. var xDistance = -40;
    4. var yDistance = 20;
    5. var zDistance = 0;
    6.  
    7. function LateUpdate () {
    8.   if (!objectToFollow)
    9.   return;
    10.  
    11.   var followPos = objectToFollow.position;
    12.    transform.position = Vector3(followPos.x + xDistance, followPos.y + yDistance, followPos.z + zDistance);
    13.  
    14.   }
    15.  
    The follow example demonstrates the current situation; the camera does not turn with the direction of the ball.

    Sorry for the newbie questions! I am used to Blitz3D and I am constantly doing things wrong because I am used to another way of thinking.

    Check out the current situation here:
    http://www.stickystudios.com/etc/Unity Player.html

    Tnx
     
  2. dacloo

    dacloo

    Joined:
    Jun 30, 2005
    Posts:
    469
    Since the ball is rolling, I need the direction he is going to,
    place the camera behind the ball with the right rotation values (one or two axes)
     
  3. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    That example crashed safari.
     
  4. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Code (csharp):
    1.  
    2. var rigdbody = objectToFollow.rigidbody;
    3. var direction = Vector3.Normalize(rigdbody.velocity);
    4.  
     
  5. dacloo

    dacloo

    Joined:
    Jun 30, 2005
    Posts:
    469
    awesome. thanks. I didn't know there was something like the Velocity parameter.

    Now that I have a Vector3 returned, how can I convert this to a Quaternion?
    (and then I will rotate the camera so that it looks into the direction of the ball)
     
  6. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
  7. dacloo

    dacloo

    Joined:
    Jun 30, 2005
    Posts:
    469
    hmmm yes I tried to use that function but I dont know what it exactly does (how to place it in a context)??

    In the attachment I have placed my example.

    The ball is in the middle. The red line is the radius in which the camera should orbit around the ball.

    If the ball moves in direction of the green arrow, the camera (blue thingie) lerps behind the ball.


    Sorry for all the questions!
     

    Attached Files:

  8. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    This is the simplistic pseudo code. It doesn't do any smoothing etc of course but should get you started.

    Code (csharp):
    1.  
    2.   var newRotation = Quaternion.LookRotation(target.rigidbody.velocity);
    3.  
    4.   var cameraPos = newRotation * (Vector3.back * distance) + target.position;
    5.  
    6.   transform.position = cameraPos;
    7.   transform.rotation = newRotation;
    8.  
     
  9. dacloo

    dacloo

    Joined:
    Jun 30, 2005
    Posts:
    469
    Hi Joachim,

    That seems to do the trick, somehow! :oops: :D
    http://www.stickystudios.com/etc/Unity Player.html
    Thanks for your patience.

    So velocity is the secret here :)
    However, I am doubting wheter this is the right approach for the camera-handling.

    What I noticed is that when the ball rolls another way, the camera somehow
    should move on the "invisible radius" (lerping) but using this method I only
    get "end results", not in-betweens using lerp.

    This results in the shocking camera movement (also when the ball is a bit in the air and then touches the ground).

    Phew, this is my first Unity project and I'm a bit scared now if its gonna work out.
     
  10. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Yes. You ALWAYS must smooth cameras. It looks horrible otherwise.

    Cameras are usually hard to get right if you want to make them follow an arbitrary rigidbody which can have quite sudden movements. Anyway, this is the basics of smoothing. Now there are many ways to tweak the camera.
    Eg. often you want to take out the .y component of the rigidbody velocity.
    Sometimes it looks better to smooth the direction vector instead of the rotation. Sometimes it is better to use euler angles and smooth those instead of quaternion. It depends on the situation and the best way to find out is to try it all out. There is no camera silver bullet way of doing it.

    Code (csharp):
    1.  
    2. var lastRotation = Quaternion.identity;
    3. var smooth = 5.0;
    4. function Update()
    5. {
    6.   var newRotation = Quaternion.LookRotation(target.rigidbody.velocity);
    7.   lastRotation = Quaternion.Slerp(lastRotation, newRotation, Time.deltaTime * smooth);  
    8.  
    9.   var cameraPos = lastRotation * (Vector3.back * distance) + target.position;
    10.  
    11.   transform.position = cameraPos;
    12.   transform.rotation = newRotation;
    13. }
    14.  
     
  11. dacloo

    dacloo

    Joined:
    Jun 30, 2005
    Posts:
    469
    Thanks Joachim,
    Yes, cameras are very hard to get right. They can make or break a game!
    You used update - shouldn't camera's not be using LateUpdate? Or...?

    Also, you seem to declare lastRotation at the top, but why?
    And, the manual says: "the identity rotation", but what is Quaternion.identity really? It is calculated in the Update() function anyhow (?)

    When I press the down button (cursor down), the camera goes beserk.
    New: http://www.stickystudios.com/etc/Unity Player.html

    Adjusted to use an optional distanceY:
    Code (csharp):
    1.  
    2. var lastRotation = Quaternion.identity;
    3. var objectToFollow : Transform;
    4. var smooth = 5.0;
    5. var distance = 50;
    6. var distanceY = 250;
    7.  
    8. function Update()
    9. {
    10.   var newRotation = Quaternion.LookRotation(objectToFollow.rigidbody.velocity);
    11.   lastRotation = Quaternion.Slerp(lastRotation, newRotation, Time.deltaTime * smooth);  
    12.   var cameraPos = lastRotation * (Vector3.back * distance) + objectToFollow.position;
    13.   transform.position = Vector3(cameraPos.x, cameraPos.y + distanceY, cameraPos.z);
    14.   transform.rotation = newRotation;
    15. }
     
  12. dacloo

    dacloo

    Joined:
    Jun 30, 2005
    Posts:
    469
    IT WORKS!!!!

    The camera wasn't the problem after all. My smooth cam worked together with Joachims tip on velocity.

    The secret is:
    1. dont calculate forces on three axes. Remove the Y for example.
    2. use getAxis all the time, it will smooth out the camera too because the
    object to follow will move lots "smoother".

    I hope this will be of any help for other newbies.