- Home /
Lock rotation of player
I'm currently making a 2D game as a beginner and I made a spinning platform. But when it's rotating the player's rotation (z-axis) also changes because it's a child of the platform. I need this when I use moving platforms. Now I want to lock the z-axis of the rotation of the player. I already tried it in 3 different ways, but none of them seems to be working. Does anybody know how to do this?
These are the three ways I tried:
// 1
PlayerTrans.transform.Rotate(
PlayerTrans.transform.rotation.x,
PlayerTrans.transform.rotation.y,
0);
// 2
PlayerTrans.transform.Rotate(
PlayerTrans.transform.rotation.x,
PlayerTrans.transform.rotation.y,
0,
Space.Self);
// 3
PlayerTrans.transform.localRotation = Quaternion.Euler(new Vector3(
PlayerTrans.transform.localEulerAngles.x,
PlayerTrans.transform.localEulerAngles.y,
0f));
and this is, what my code looks like for staying on the moving platforms. I used raycasting for this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Raycasting : MonoBehaviour
{
// Start is called before the first frame update
Transform PlayerTrans;
public float RayCastRange = 3;
void Start()
{
PlayerTrans = transform.parent;
}
// Update is called once per frame
void Update()
{
RaycastHit2D PlattformCheck = Physics2D.Raycast(transform.position, -Vector2.up, RayCastRange);
if (PlattformCheck.collider != null)
{
if (PlattformCheck.collider.gameObject.tag == "Platform")
{
PlayerTrans.transform.SetParent(PlattformCheck.collider.gameObject.transform);
}
else
{
PlayerTrans.transform.SetParent(null);
}
}
else
{
PlayerTrans.transform.SetParent(null);
}
}
}
Answer by kevinhart1001001 · May 05, 2021 at 09:42 PM
Hello, if you are using a Rigidbody, you can easily lock the z.rotation (image)
,Hello, i guess you can disable rotation, if you use a Rigidbody (Disable Rotation on Z-Axis)
That would only mean that he disables rotation applied by the physics system I think. So I don't think it would help with freezing the rotation that is being applied to it through it's parent.
Answer by The-Peaceful · May 05, 2021 at 11:07 AM
I think all you need to do is set the global eulerAngles like this:
Vector3 eulers = PlayerTrans.eulerAngles;
PlayerTrans.eulerAngles = new Vector3(eulers.x, eulers.y, 0f);
Though this would mean that you set the rotation around your Z-axis to 0 which you might not want to do. So maybe then go and create a variable storing the global angle around the Z-axis and use that instead:
float zRotation = 0f; //Add the rotation value onto this variable when changing the Z-Rotation
void Update() {
Vector3 eulers = PlayerTrans.eulerAngles;
PlayerTrans.eulerAngles = new Vector3(eulers.x, eulers.y, zRotation);
}
And to answer why the methods you've tried didn't work:
transform.Rotate:
Rotates given degrees over given angles
if you rotate 0 over the Z-axis you won't rotate it at all
Also rotation is not a value in degrees! The rotation is stored in Quaternions, to get angles in degrees you have to use EulerAngles
transform.localRotation = Quaternion.Euler(...... validt option, but you take the local (relative to it's parent) eulerAngles so 0 degrees on the Z-axis is equal the degrees that it's parent has on that axis (so it will still rotate if it's parent rotates)
Hope that helps, if not let me know :D