Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Tilting a board around with the mouse?

Discussion in 'Scripting' started by amcclay, Aug 9, 2006.

  1. amcclay

    amcclay

    Joined:
    Aug 8, 2006
    Posts:
    143
    Hey again. Ok, i'm on my next problem. I'm trying to do a simple marble rolling-on-a-board type game like many have. I have it working fine using keyboard controls, but I would like to now implement mouse control for a more 'analog' feel when tilting the board around.

    So, I guess the question is - How would I go about tilting an object in accordance with mouse position? So, if I move the mouse to the left, the board will tile left and so on.

    Thanks!
     
  2. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    Use Input.mousePosition.x Input.mousePosition.y
     
  3. Morgan

    Morgan

    Joined:
    May 21, 2006
    Posts:
    1,223
    I was just playing with that myself, already had the script open in Unitron, so (assuming you want a hidden cursor and just want the raw mouse motion) here you go :)

    Attach this to the object you want to tilt, and check its box for Is Kinematic:
    Code (csharp):
    1.  
    2. function Update () {
    3.     transform.rotation.x = transform.rotation.x + .01 * Input.GetAxis("Mouse Y");
    4.     transform.rotation.z = transform.rotation.z - .01 * Input.GetAxis("Mouse X");
    5. }
    6.  
    This is a VERY basic starting point and probably not the best way, but it worked for me. Adding tilt limits could be important, and you may wish to change .01 to another factor to control the tilt speed. Also note that XYZ can be messed up for an imported model--if so, put this script on a GameObject and make the real object a child of that.
     
    BlastOffProductions likes this.
  4. amcclay

    amcclay

    Joined:
    Aug 8, 2006
    Posts:
    143
    Thanks! I tried attaching this to my 'base' model and it didn't seem to respond to any mouse movements. Is there anything special I need to do to 'enable' the mouse as an input device in-game?

    Also, I wasn't originally using my model as a rigid-body. It was basically static geometry with a mesh collider and a ball rolling around inside of it. I was just moving it with transforms. Does it *have* to be a rigid body object?
     
  5. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    You almost never want to mess with the x,y,z, and w of a Quaternion (rotation) directly. You should use transform.Rotate() inside of FixedUpdate() getting in the mouse change like Nicholas mentioned.

    Cheers,
    -Jon
     
  6. amcclay

    amcclay

    Joined:
    Aug 8, 2006
    Posts:
    143
    Ok gotcha. Right now I am using a routine that I found in another thread that works great with the keyboard, but I can't figure out how to accomplish the same effect with the mouse:

    Basically I'm using this formulat:

    And so on for each key..
     
  7. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    What you probably want is to reset the rotation and then turn it based on the mouse position.
     
  8. amcclay

    amcclay

    Joined:
    Aug 8, 2006
    Posts:
    143
    I dunno, can someone post a script of how this would look? Or even how a person would translate ANY object via mouse movement? I'm wondering if I don't have unity configured properly to 'see' my mouse as an input device..
     
  9. amcclay

    amcclay

    Joined:
    Aug 8, 2006
    Posts:
    143
    Update - What Morgan posted worked - I probably had some setting screwed up on the board that was preventing it from responding to the mouse inputs. It is a little squirly with the mouse movements, but it IS functional - thank you SO much!

    Now, any ideas of how I might be able to 'dampen' the movement so it's not so sudden?

    Also, the fact that it moves fast seems to make the ball 'fall through' the board quite often.
     
  10. Morgan

    Morgan

    Joined:
    May 21, 2006
    Posts:
    1,223
    Making the .01 smaller will slow the movement.

    Small, fast objects are NOT good with true physics systems unless you have a really fast computer, it seems. I slowed my game down and made my objects larger until they stopped passing through each other.

    To aarku - thanks for the tip. I was using Update instead of FixedUpdate because I thought the mouse delta was for the last visible update, not for the last FixedUpdate, Is the mouse delta sampled on fixedupdate? (I had the impression mouse delta was unique in this regard--that it's not analogous to other input axes. Is that true?)
     
  11. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Yeah, I was also having a few problems with small scales...seems like the physics are more geared toward human-sized and larger elements. But you can also change the fixed timestep, which helps a lot. I went from .02 to .01, which updates the physics 100 times a second instead of 50. Doesn't seem to have affected the frame rate much at all, though I don't have a huge number of objects going at once either.

    --Eric
     
  12. Morgan

    Morgan

    Joined:
    May 21, 2006
    Posts:
    1,223
    Yes, I was finding that shrinking the time step wasn't much help on slower machines, but that is something you can do.

    Also, you can script your own (simpler) physics in a more conventional way, or as I did with Clockwork 360, script a hybrid with some real physics and some fakery. Fake physics can take more work and be less scientifically accurate, but it can also be very fast and reliable.