Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

adding Momentum to orbiting camera

Discussion in 'Scripting' started by mediashock, Mar 10, 2011.

  1. mediashock

    mediashock

    Joined:
    Apr 15, 2009
    Posts:
    65
    I am struggling with this a bit..from the script below, anyone know how i can add some momentum to the orbiting camera?

    right now when I drag it around it works ok, but when i let go of the mouse I would like it to continue orbiting for a second longer and slow down.

    Code (csharp):
    1. if (Input.GetMouseButton(0)){
    2.                
    3.  
    4.     if (target) {
    5.        
    6.         x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
    7.         y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
    8.        
    9.      
    10.      
    11.            
    12.         y = ClampAngle(y, yMinLimit, yMaxLimit);
    13.         velocity = Input.GetAxis("Mouse X") * xSpeed;
    14.        
    15.          
    16.     }
    17.    
    18. }
    19.  
    20.  
    21.  
    22.        
    23.         distanceCurrent = Mathf.Clamp(distanceCurrent, distanceMin, distanceMax);
    24.    
    25.         rotation = Quaternion.Euler(y, x, 0);
    26.         position = rotation * Vector3(0.0, 0.0, -distanceCurrent ) + target.position;
    27.        
    28.        
    29.        transform.rotation = rotation;
    30.        transform.position = position;
    31.        
     
  2. pretender

    pretender

    Joined:
    Mar 6, 2010
    Posts:
    865
    i am interested in this too...
     
  3. mediashock

    mediashock

    Joined:
    Apr 15, 2009
    Posts:
    65
    hmm. spent few hours and got unacceptable results..:(
     
  4. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    I've attached a simple variable momentum script to this post. It gives much the same effect as the smoothing applied by Input.GetAxis but you can apply it to any input value so you can use it in cases where GetAxis isn't available. Also, it is a bit more flexible than the input axes in that you can set the minimum and maximum values to be anything you want and also set the attack and decay values independently. Sustain and release are not currently implemented ;-)

    For the camera momentum, you should pass in the values obtained from the mouse movement and use the value returned by GetValue as the amount to move the camera. If you do this on each frame (including frames where there is no mouse movement) you should get the right effect, although you may need to play with the attack and decay values to get the behaviour you want.
     

    Attached Files:

  5. mediashock

    mediashock

    Joined:
    Apr 15, 2009
    Posts:
    65
    thanks, i'll give this a try
     
  6. megadouwe

    megadouwe

    Joined:
    Mar 27, 2011
    Posts:
    15
    Hi Andeeee,

    It's me again! :D

    This might be a silly question, but I've found this post with your file attached to it. But what do I do with this file? Do I just import this file in my Unity Project? And create a js file like you've described in your post at http://forum.unity3d.com/threads/83039-2d-topdown-iPhone-game-movement?

    I've allready tried to connect the js file to the object I want to move, however it gives me the next error:

    "The name Momentifier does not donate a valid type".

    Can you please help me on the right way?

    Thnx!

    Douwe
     
  7. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    @megadouwe: since this is a C# file, you need to put it in the Plugins folder to make the Momentifier class available to JS scripts. Otherwise, the usage is the same.
     
  8. megadouwe

    megadouwe

    Joined:
    Mar 27, 2011
    Posts:
    15
    Aha! Thnx Andeeee! Will give it a try!!!