- Home /
Question by
Jeezker · Oct 26, 2021 at 04:31 PM ·
rotation axis
Lock transform rotation on Y axis
Hello,
I have a script where you can climb up walls and ceilings with a guy 2D.
But sometimes he deviates off the course.
How can i lock his rotation? The scripts is pretty complicated.
private Quaternion GetMagneticRotation()
{
closestSphereCastHit = null;
if (Physics.SphereCast(transform.position, collider.radius, -transform.up + transform.forward, out RaycastHit hitForward, SPHERECAST_DISTANCE, RayCastLayerMask))
closestSphereCastHit = hitForward;
if (Physics.SphereCast(transform.position, collider.radius, -transform.up, out RaycastHit hitDown, SPHERECAST_DISTANCE, RayCastLayerMask))
{
if (!closestSphereCastHit.HasValue || closestSphereCastHit.Value.distance > hitDown.distance)
closestSphereCastHit = hitDown;
}
if (Physics.SphereCast(transform.position, collider.radius, -transform.up - transform.forward, out RaycastHit hitBack, SPHERECAST_DISTANCE, RayCastLayerMask))
{
if (!closestSphereCastHit.HasValue || closestSphereCastHit.Value.distance > hitBack.distance)
closestSphereCastHit = hitBack;
}
var normal = closestSphereCastHit?.normal ?? Vector3.zero;
var lookRot = Quaternion.LookRotation(Vector3.Cross(transform.right, normal), normal);
return lookRot;
}
private void MagneticRotate()
{
var stickedRotation = GetMagneticRotation();
if (Time.time > lastTimeWooshed + 0.5f && stickedRotation != lastStickedRotation && Time.time > 1f)
{
WooshSound.Play();
lastTimeWooshed = Time.time;
}
var desRot = Quaternion.Slerp(transform.rotation, stickedRotation, Time.fixedDeltaTime * currentMagneticRotationSpeed);
transform.rotation = desRot;
//transform.Rotate(new Vector3(0f, 1f, 0f), 1f);
lastStickedRotation = stickedRotation;
}
Comment
Answer by YagirX · Oct 26, 2021 at 10:18 PM
trasform.rotation = desRot;
////////////
transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, 0, transform.localEulerAngles.z);
Hi, id doesnt work well.... :( When he goes up the wall he spins crazy
Your answer

Follow this Question
Related Questions
Pointing Object in the Direction it is going 1 Answer
Rotate a transform's Quaternion by a Vector3 axis 0 Answers
How to use Look-at using only 2 axis to rotate. 0 Answers
Odd rotation on the wrong axis 0 Answers
Animated Sprite Rotating Around It's Own Axis Instead Of Parent's Using Script. Details Provided. 0 Answers