- Home /
Transform Position X/Y increasing by no reason
So yeah my X and Y coordinates of the transform component are moving by their own when my script is supposed to be rotating and object around other only...
I set the value of rotateSpeed inside the inspector, its currently 4.5
public class CoreOrbitMovement : MonoBehaviour
{
[Header("RotateAround")]
private Rigidbody2D myRigidBody;
public float rotateSpeed;
private float moveInput;
private Vector3 zAxis = new Vector3 (0,0,1);
public Transform target;
void Start()
{
myRigidBody= GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
moveInput= Input.GetAxisRaw("RightStickHorizontal");
RotateChar();
}
public void RotateChar() {
transform.RotateAround(target.position, zAxis, moveInput * rotateSpeed);
transform.rotation = target.rotation;
}
}
Answer by gulle909 · Aug 06, 2019 at 09:36 PM
The function "Rotate Around" is used for moving the GameObject around another GameObject. Im not quite sure what you are trying to achieve, but if you want the GameObject to rotate around itself on the z-axis, you can use this bit of code instead:
public void RotateChar() {
transform.Rotate(Vector3.forward * rotateSpeed * moveInput * Time.deltaTime, Space.Self);
}
Answer by RurouniKen · Aug 09, 2019 at 04:09 PM
Thanks for taking your time to help me @gulle909.
I'm trying to make an object rotate around another in the Z axis every time I rotate my right joystick. So far I have only made it work whenever I push my joystick to the right or to the left. So is it better to use Transform.Rotate instead of Transform.RotateAround?
But when it's only around the Z axis, why, and how, would you want to rotate it when the joystick gets pushed in any direction?
I'm making a 2D game, thats why I want to rotate an object around another on the Z axis with the motion of my right joystick so If I rotate my joystick to the right (like doing a half circle motion) my object rotates in that motion around the other.