- Home /
c# modify only one axis of a quaternion
I know that in Js you can directly modify one axis of a quaternion or Vector3, in c# you cant. So usually you just do something like:
void Update(){
transform.rotation = new Quaternion.Euler(transform.rotation.eulerAngles.x,10,transform.rotation.eulerAngles.x);
}
or something but the problem is that that still restricts another script from rotating the same object since it is constantly setting that objects rotation to the current rotation (Atleast that is the behavior I am getting). I am probably just overlooking something simple or being downright stupid.
Answer by SirCrazyNugget · Jul 19, 2014 at 03:04 AM
If you only want to set one of the axes why not just create a public float property which can be accessed by any other script
public float rotY {
get { return transform.rotation.eulerAngles.y; }
set {
Vector3 v = transform.rotation.eulerAngles;
transform.rotation = Quaternion.Euler (v.x, value, v.z);
}
}
(Or something similar to).Then you can access it by
OtherGameObject.rotY = 45;
I cant seem to access the property. it says that rotY is not a member of gameobject EDIT. I fixed it by using
gameObject.GetComponent<Simple$$anonymous$$ouseRotator>().rotX = 0;
However, now I get the error "Object reference not set to an instance of an object" at line 23
Create a script for the object being rotated
//Rotatee.cs
using UnityEngine;
using System.Collections;
public class Rotatee : $$anonymous$$onoBehaviour {
public float rotY {
get { return transform.rotation.eulerAngles.y; }
set {
Vector3 v = transform.rotation.eulerAngles;
transform.rotation = Quaternion.Euler (v.x, value, v.z);
}
}
}
Create a script for the object in charge of the rotating
//Rotater.cs
using UnityEngine;
using System.Collections;
public class Rotater : $$anonymous$$onoBehaviour {
[Range(0, 360)]
public float rotY;
public Rotatee rotatee;
void Update(){
rotatee.rotY = rotY;
}
}
Assign the objects as required, place the rotatee in the rotater.roratee slot and play with the slider.
Answer by larswik · Sep 04, 2017 at 06:14 AM
I know this is old but after 2 of my 3 day holiday I dedicated to working on my game I could not get my particle I used as shrapnel to explode in the same direction the canon ball was heading as it arc'd. Code that finally worked below. Thanks you very much!
transform.rotation = Quaternion.LookRotation(testRigid.velocity);
Vector3 v = transform.rotation.eulerAngles;
transform.rotation = Quaternion.Euler (v.x, v.y, 90);
Your answer
Follow this Question
Related Questions
Unity Rotate Raycast on Quaternion 1 Answer
Camera viewport transformation from one world to the rotated world. 1 Answer
Perpendicular Vector3 0 Answers
Instantiate GameObject towards player 0 Answers
Transform a Vector by a Quaternion 1 Answer