0
\$\begingroup\$

enter image description hereenter image description here I want to rotate one object around another, so that it matches my current joystick rotation. I know Transform.RotateAround does this but I can't seem to find a way to match it with my joystick rotation.

This is my code so far:

void Update()
{     
    joystick = InputManager.ActiveDevice;

    moveInput= joystick.RightStickX;

    if(currentState != CoreplayerState.stager) {        
        RotateChar();
    }
}


public void RotateChar() {        
    transform.RotateAround(target.position, zAxis, moveInput * rotateSpeed);   
    transform.rotation = target.rotation;
}
\$\endgroup\$
2
  • \$\begingroup\$ I notice at the moment you're only using the x component of the joystick input - so pointing right will rotate counter-clockwise about the z axis, and pointing left will rotate clockwise. Is this what you want, or do you want to rotate to the angle the joystick is currently pointing? (eg. if I point my joystick diagonally up & right, should the object rotate to sit above & to the right of its pivot and stop at that point?) \$\endgroup\$
    – DMGregory
    Dec 3, 2019 at 1:09
  • \$\begingroup\$ Yeah I want it to work exactly as you pointed out, I just did not even try to map the vertical axis on the joystick because I never found a way to match my joystick position so I ended up only using the X axis of my joystick. \$\endgroup\$ Dec 3, 2019 at 1:34

1 Answer 1

0
\$\begingroup\$

We can think of the x, y input on the joystick as just another vector in our game world. Transforming it through the pivot lets you match the plane of rotation to the orientation of the pivot.

How's this work for your use case?

public class JoystickMatch : MonoBehaviour {

    public Transform pivot;
    public float radius = 1f;


   void Update() {
       var joystick = InputManager.ActiveDevice;
       var heading = new Vector2(joystick.RightStickX, joystick.RightStickY);

       if(heading != Vector2.zero) {
           transform.position = pivot.TransformPoint(heading.normalized * radius);
           transform.rotation = pivot.rotation;
       }
   }
}
\$\endgroup\$
10
  • \$\begingroup\$ Sorry I deleted the previous answer accidentally, I guess I'm just very excited, give a second and I will post the screenshot with my issue. \$\endgroup\$ Dec 3, 2019 at 1:56
  • \$\begingroup\$ So the white character is the pivot for the purple character and I want the purple character to be always away from the pivot around the distance of the image \$\endgroup\$ Dec 3, 2019 at 2:01
  • \$\begingroup\$ How does that differ from the behaviour you get with this script or similar? \$\endgroup\$
    – DMGregory
    Dec 3, 2019 at 2:02
  • \$\begingroup\$ I am very ashamed for the lack of explanation, I just edited my opening post with the image of the current position of the purple character with the code you gave me (first image). I really thank you for all your patience and will to help me man. \$\endgroup\$ Dec 3, 2019 at 2:06
  • \$\begingroup\$ Did you try increasing the value of the radius variable? \$\endgroup\$
    – DMGregory
    Dec 3, 2019 at 2:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .