Search Unity

"Throwing" rotation with mouse movement

Discussion in 'Scripting' started by Jessy, Mar 28, 2008.

  1. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    I would like to be able to implement something akin to horizontal FPS mouse control, such as can be found in the MouseLook.cs script when you set it to use "MouseX". However, I don't want the rotation to stop when I stop moving the mouse. I would like some inertia to factor into this.

    The object to which this script will apply has a rigidbody attached, and trying to work with torque forces did not yield good results with the little effort I put into that so far (MouseLook stabilizes the rotation nicely). I'm just looking for some advice about how to have the turning not end so abruptly.
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    What particular kind of not-good results are you getting?

    The most obvious problem I can see with this is that a light throw won't always counteract a recent heavy throw. You would expect the object to turn left when you throw the mouse left, not just slow the rotation to the right.

    You could fix this by adding a bit of extra torque to the throw, proportional to the current speed of rotation. Basically, you want to add enough torque to undo the object's current rotation and then add the new rotation to it. Rather than calculate the extra torque, you could just make a public variable and tweak it in-game to get the right behaviour.

    (Is the object a camera, by the way?)
     
  3. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    The object is not a camera, actually. The problem with turning MouseLook off is that gravity starts rotating the object. Obviously, this can be overcome, but I'm new at doing it, hence my questioning for general advice of how to go about this. I don't require that the physics engine is involved in the turning; I'm just trying to make this easy on myself and code-efficient.