Search Unity

Question Rigidbody constrain along custom angle?

Discussion in 'Editor & General Support' started by Flamacore, Jun 8, 2021.

  1. Flamacore

    Flamacore

    Joined:
    Dec 17, 2013
    Posts:
    140
    I have a question. Am a Unity veteran (10+ years, countless B2B and B2C games, many very unique situations solved etc.) but also a math newbie (music graduate :) ). So my question is this; Let's say I have a rigidbody and let's say I'm moving it along a custom direction with a key press like
    AddForce(new Vector3(0.9f,0,0.4f))
    . Camera follows the character wiht smooth movement and rotates a little depending on the situation too. So Camera.main.transform.right is not an option (unfortunately).

    There should be no single way whatsoever for the character to move along in any other direction. But as its a Rigidbody, it slightly changes position upon obstacle collision. Think of a 3D platformer.

    Main problem here is; I'm not looking at the world at a 90 degree angle. It's a custom angle so I can't simply use a (X) position constraint as the character moves in both positions.

    Would like to avoid colliders at sides as its very, very, veeery inconsistent even if I go hardcore on collision detection. But if that's the only option, I'll have to work something out obviously.

    Any help is appreciated highly. I have a feeling that this can be solved very easily by using math but as I've said, not a pro at that :)
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Do you mean constraining the Rigidbody to only move freely in a single arbitrary-tilted plane?
     
  3. Flamacore

    Flamacore

    Joined:
    Dec 17, 2013
    Posts:
    140
    EDIT: Oh wait, now I get it, and yeah I think that's what I want to do.

    Not exactly sure what you mean by that but I can try to clarify a bit more I guess?

    Put a camera on a terrain, rotate it on the Y axis a little on the right. Put a character and move the character ONLY in a custom axis like 0.9f, 0, 0.4f and not allow any other movement whatsoever while still keeping it a rigidbody. Itshould not move into any other direction even if it hits a collision and tries to force-move. I'll demonstrate with a visual when I have the time but can't right now unfortunately.
     
    Last edited: Jun 8, 2021
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    As you know, clicking freeze position on X,Y or Z will do what you think it will.

    Making your own arbitrary plane to lock out an arbitrary axis can be done with a ConfigurableJoint and capping one of its motions, then rotating it how you like.

    See enclosed. Tilt the plane in the editor.

    There are no colliders in scene at all. Put one on the sphere, put other colliders in and see the interactions happen, all while the sphere stays locked to the tilted plane.
     

    Attached Files:

  5. Flamacore

    Flamacore

    Joined:
    Dec 17, 2013
    Posts:
    140
    Wow, even a unitypackage. Thanks a lot, checking it right now. Will return with results.
     
  6. Flamacore

    Flamacore

    Joined:
    Dec 17, 2013
    Posts:
    140
    Never used a configuration joint before but it seems to cut it. Learning never ends :) Thanks a lot, it seems like what I need for my project. If anything goes wrong, I'll check back.
     
    Kurt-Dekker likes this.