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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

FPS Gun moving (kinda...)

Discussion in 'General Discussion' started by FakeBocha, Mar 12, 2019.

  1. FakeBocha

    FakeBocha

    Joined:
    Aug 8, 2017
    Posts:
    23
    Hi!
    My question is a bit weird... I'm trying to understand how to make the gun move a little bit left or right when the player is rotating... (and how the heck does that feature is called??)

    Do I need animation? Or just using rotating the mesh a little bit?

    Thanks for the help!
     
  2. N1warhead

    N1warhead

    Joined:
    Mar 12, 2014
    Posts:
    3,884
    There's plenty of ways to do it, but I'd think the far simplest in terms of working for nearly all situations is to lerp/slerp it.
    That way if you change your settings in any way, you can just adjust the smoothing value and bam can self-correct it on the fly.

    I highly recommend learning about Lerp or Slerp or maybe even SmoothDamp.


    You see, you could do an animation, and it would work - but that would require blending animations and so forth for all directions, etc.

    Would be easier to spit out <20 lines of code to just make it work in all cases and can change the values on the fly if need-be.

    I can't give you a how-to guide on it, but this information should help you though.
     
  3. FakeBocha

    FakeBocha

    Joined:
    Aug 8, 2017
    Posts:
    23
    Thats what i thought to do! Umm but my question is 'how to do it the best way?'
     
  4. N1warhead

    N1warhead

    Joined:
    Mar 12, 2014
    Posts:
    3,884
    I suppose you could send out a Ray from the camera and get the hit.point and then have the gun lerp its rotation to that point.

    Or just use the cameras direct transform rotation and somehow make the gun lerp its rotation with it.
     
  5. Braineeee

    Braineeee

    Joined:
    Nov 9, 2014
    Posts:
    1,211
    No! Use Vector3.MoveTowards() or for rotations: Quaternion.RotateTowards()! :)
     
  6. N1warhead

    N1warhead

    Joined:
    Mar 12, 2014
    Posts:
    3,884
    Is that Smooth though? Never used them. Honestly forgot they existed lol.
    But yeah if it isn't smooth like lerp, etc then it may not be what he wants.
     
  7. FakeBocha

    FakeBocha

    Joined:
    Aug 8, 2017
    Posts:
    23
    Thanks for all the help! I probably use the slerp...
     
  8. Braineeee

    Braineeee

    Joined:
    Nov 9, 2014
    Posts:
    1,211
    Check the docoumentation, they are definitely smooth. They require 3 args: 1. the value to be take as input, 2. the target value, and 3. a max delta value. I think these function are very useful! Slerp and Lerp are not really meant for the purposes most people use them for, like you the use case you cited to solve the OP's problem.
     
    xVergilx likes this.
  9. N1warhead

    N1warhead

    Joined:
    Mar 12, 2014
    Posts:
    3,884
    I suppose that's debatable lol. For example - I use the tree placement tool for terrain for practically everything - grass, trees, shrubs, etc. Just because its made for trees don't mean you can't use it for something else and is better and easier than placing individually by hand.

    The point I was trying to make with that above is that people use things that work for them.
    I don't see any problems using lerp for smooth movements, etc.

    The only thing I suppose it really boils down to is performance per the two (lerp vs RotateTowards) if they both offer a smoothing function.

    But I still say use what you want so long as it achieves the result you want. Now if you got 30,000 objects doing this that's when performance really matters. If something takes an extra 0.01 millisecond it's not a big deal for one item.

    (Note: I'm not defending Lerp). I'm only saying to use which ever it is you want, so long as its the result you are after.
     
    Braineeee likes this.