- Home /
Constraints on rotation axis in c#
How do I create a constraint on a certain rotation axis in c#?.....making an object rotate towards the mouse is easy but I cant seem to create the x and z constrains to keep it from spinning in every direction. ....... if I use transform.rotation.z = 0 I get an error in C#.
rotation is a Quaternion. you can't mess with its individual components like the way you're doing it. you can set the rotation like:
transform.rotation = Quaternion.Euler(x, y, z);
this is just like setting the rotation in the inspector
What you mean deformed? - You only want the y
to rotate right?
Answer by vexe · Oct 05, 2013 at 08:45 AM
See this amazing tutorial from UnityGems, covers everything you need about rotations and Quaternions including your restrictions/constraints that you're asking about (the "Rotating Around the Y Axis Only" section)
I know but in javascript when I make an object rotate towards the mouse with the transform.LookAt everything works fine....I just add a constraint on the x and z axis like transform.rotation.z = 0 and everything works fine......... But in c# I cannot set constraints on an individual axis like in javascript so I use tranform.EulerAngles = new Vector3(0,transform.eulerAngles.y,0)....the constraints actually works but the gameobject gets deformed by this and I want to know if there any other ways to set constraints.Bu if I use transform.rotation = Quaternion.Euler(0,transform.rotation.y,0) the gameObject dosent spin anymore it just budges a little from left to right.
using UnityEngine;
using System.Collections;
public class turretRotation : $$anonymous$$onoBehaviour {
RaycastHit hit;
Vector3 targetPos;
void Start ()
{
renderer.material.color = Color.red;
}
void Update ()
{
if(Physics.Raycast(Camera.main.ScreenPointToRay (Input.mousePosition),out hit,100))
{
targetPos = new Vector3 (hit.point.x, 0, hit.point.z);
}
transform.rotation = Quaternion.LookRotation(targetPos - transform.position);
if(transform.eulerAngles.x!=0 || transform.eulerAngles.z!=0)
{
//transform.eulerAngles = new Vector3(0,transform.eulerAngles.y,0 );///the constraints work fine but the gameObject gets deformed
transform.rotation = Quaternion.Euler(transform.rotation.x, transform.rotation.y , transform.rotation.z );//the constraints work as well bu the gameObject barely budges from left to right
}
}
}
I need to know what Im doing wrong.....in javascript is easier but I want to see it in c#
Please do it like in the link I gave you above. rotation
is a quternion you shouldn't access/mess with its components directly like you did (it has four components, xyzw):
transform.rotation = Quaternion.Euler(transform.rotation.x, transform.rotation.y , transform.rotation.z );
Just make a new rotation variable:
var newRotation = Quaternion.LookRotation(targetPos - transform.position).eulerAngles;
set your constraints:
newRotation.z = newRotation.x = 0;
and apply your new rotation:
transform.rotation = Quaternion.Euler(newRotation);
That should work just fine.
Not sure if it would work, but could you try localEulerAngles
ins$$anonymous$$d? - the thing is, eulerAngles
gets locked at 360 dgs, you can't rotate more than that. Also put a print between setting the rotation and the eulers, print (trans.euler.y);
see what's happening.
The linked article is great. Not sure how long it'll work, but mirror at: http://web.archive.org/web/20140702084123/http://unitygems.com/quaternions-rotations-part-1-c/
Your answer
![](https://koobas.hobune.stream/wayback/20220613115335im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Camera global rotation clamping issue 0 Answers
rotate axis of movement 0 Answers