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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question Making joint aim in the same direction as the main camera, regardless of joint axis

Discussion in 'Scripting' started by dylanpdx, Mar 23, 2021.

  1. dylanpdx

    dylanpdx

    Joined:
    Nov 6, 2015
    Posts:
    5
    Hello

    I'm trying to make a ConfigurableJoint aim in the same direction the camera is aiming in regardless of it's axis or rotation.

    My setup is a main rigidbody with a joint connecting it and another rigidbody.


    The joint can be configured in any number of ways, i.e It can be acting as a hinge, or it can be acting as a wheel type joint, depending on what the axis is configured as.


    I want to make it so that the joint, no matter how it's configured or how the "main body" is rotated, always tries to match it's forward rotation to the camera's forward rotation.

    I've tried some simple math code like
    joint.TargetRotation = Quaternion.LookRotation(Camera.main.transform.forward,joint.axis)

    but it's not giving me the result I want, and I assume it's not going to be as simple as that :p

    Any ideas on how best to approach this? I'm not a math expert so I feel like that's what i'm missing.

    Thanks in advance!
     
  2. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,009
    Physx implementation in unity3d is very poor and configurable joint is best example of it.
    Let me tell you - you will probably not be able to pull it off.

    TargetRotation is in joint space, joint space is in physx right-handed coordinates, to make things worst you have to take into account that you can set primary and secondary axis for a configurable joint.
     
  3. dylanpdx

    dylanpdx

    Joined:
    Nov 6, 2015
    Posts:
    5
    Ugh, that's unfortunate :/

    Any ideas for a workaround, or at least how to do it without a joint and instead using transform.rotation?
     
  4. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,009
    With transforms you can probably do it using LookAt method.