Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Smooth weapon movement

Discussion in 'Scripting' started by donjuan, Sep 4, 2008.

  1. donjuan

    donjuan

    Joined:
    Dec 21, 2007
    Posts:
    67
    Hi, this should be easy but I can't make it work :( . I'm trying to make the movement of some parented fps weapons less rigid, using the Slerp function. It works well for a while but then my beautiful smoothy movement gets rigid again. Apparently I need to reset the rotation somehow but I haven't any clue on how to do so.
    This is the code I inserted in the Update function:

    Code (csharp):
    1.  
    2. this.transform.parent = null;
    3.    
    4. var rotation = Quaternion.Slerp(transform.rotation,GameObject.Find("Player/Main Camera/Weapons").transform.rotation,Time.time*0.01);
    5.    
    6. transform.rotation = rotation;
    7.  
     
  2. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    you don't want to use Time.time.

    By doing that you are saying "Every frame, move me towards the weapons transform an amount that is directly proportional to the time that the game has been running"

    You want to use Time.deltaTime. That says "Every frame, move me towards the weapons transform an amount that is directly proportional to the time that this frame took"
     
  3. donjuan

    donjuan

    Joined:
    Dec 21, 2007
    Posts:
    67
    Many thanks for the quick response.
    I tried changing Time.time for Time.deltaTime but there is no rotation at all. The transform remains with its original rotation...
     
  4. careyagimon

    careyagimon

    Joined:
    Dec 20, 2007
    Posts:
    209
    Time.deltaTime is much much smaller than Time.time. You will need to change your coefficient to reflect this.
     
  5. donjuan

    donjuan

    Joined:
    Dec 21, 2007
    Posts:
    67
    Yep, this was the problem :oops:. I increased the coefficient and deltaTime worked as expected. Thanks a lot