Questions in topic: "euler angles"
http://answers.unity.com/questions/topics/single/169196.html
The latest questions for the topic "euler angles"MagicLeap controller
http://answers.unity.com/questions/1836009/magicleap-controller.html
I'm running under the update method a PickUp script. I've tried using a parent gameObject that it's positionend where I want it, but when I move the prefab inside the empy gameobject the origin of my gameobject matches the one with my prefab. So, I'm trying to build an offset for the position and rotation. The position works fine, but I can't figure out a way to correct the rotation.
Thank you!
transform.position = controller.transform.position +Poscorrection;
transform.rotation = controller.transform.rotation + Quaternion.(0.0f,90.0f,-30.0f) ;augmented-realityanglesquaternionseuler anglesmagicWed, 12 May 2021 22:03:25 GMTjaredlanHow to calculate correct angles for PID?
http://answers.unity.com/questions/1833890/how-to-calculate-correct-angles-for-pid.html
Hello.
<br><br>
I'm working on flying mechanic for space craft and I use PID to calculate it's movement.<br>
I have a target which I'd like my ship to look to.
So, here is my implementation, most part of it is taken from [this great tutorial about PID][1].<br>
Also, it may be worth it to take a look at [PID implementation for Unity][2]
<br><br>
// Target rotation between ship and its target
Quaternion targetRotation = Quaternion.LookRotation(transform.position - Target.position);
// The "error" between current angle and desired (to look at the target)
float xAngleError = Mathf.DeltaAngle(transform.rotation.eulerAngles.x, targetRotation.eulerAngles.x);
// Calculate correction via PID
float xTorqueCorrection = xAxisPIDController.GetOutput(xAngleError, Time.fixedDeltaTime);
// Apply to rigid body
_rigidbody.AddRelativeTorque(xTorqueCorrection * Vector3.right, ForceMode.Force);
<br>
And how it looks:
<br><br>
![Demonstration of PID][3]
<br>
So, there are a few problems I have, but I think all of them are because of angles calculation (Even tutorial's author mentioned it).
When target is directly above the ship, it starts to spin.
<br><br>
![Problem with angle][4]
<br>
I think it happens because of the way I calculate errors. It works great for all the Z and Y, but not for X.
<br><br>
Please, help me find the correct way to calculate errors for PID.
[1]: https://www.youtube.com/watch?v=roxhVW3KeRY
[2]: https://github.com/Tvtig/UnityPIDController/blob/master/Assets/Scripts/PID.cs
[3]: https://vk.com/doc40928941_593089635?hash=6924d669d3f99f4f53&dl=8bdd11e87200225a59&wnd=1&module=public
[4]: https://vk.com/doc40928941_593089975?hash=be6c875458d037222a&dl=b44a7a902865b1e824&wnd=1&module=publicrotationphysicsmathcalculationeuler anglesSun, 02 May 2021 20:46:49 GMTmishakozlov74rotate game object with eurle angles ,
http://answers.unity.com/questions/1817995/rotate-game-object-with-eurle-angles.html
im trying to rotate my game object with eurle angles and get more tha 1 position using only one getkey for my video game
if (Input.GetKey(KeyCode.RightArrow))
{
GetComponent<Transform>().eulerAngles = new Vector3(0, 110, 0);
FindObjectOfType<audioManager>().play("slide");
}
im try to rotate this other positions (0,250,0)
(0,360,0)
(0,630,0)
using only one Input.GetKey(KeyCode.RightArrow)),im triying to use euler angles to rotate my game object using only one getkey and try to get more than one position in the same getkey
if (Input.GetKey(KeyCode.RightArrow))
{
GetComponent<Transform>().eulerAngles = new Vector3(0, 110, 0);
FindObjectOfType<audioManager>().play("slide");
}euler anglesFri, 26 Feb 2021 20:32:00 GMTKiksRoHow can I get my first person plane camera to go upside down?
http://answers.unity.com/questions/1812193/how-can-i-get-my-first-person-plane-camera-to-go-u.html
Hi there, first question on here. I'm messing around with the Programming intro tutorials and after I completed the plane game challenge I decided to have a little fun and try to implement a first person camera. I used code for a third person camera I had used before and modified it to place the camera on the nose of the aircraft, but when I rotate the plane above fully up or fully down, the camera starts returning to the horizon again without going upside down.
Here's the code for my camera controller:
public class FollowPlayerX : MonoBehaviour
{
public GameObject plane;
private Vector3 sideOffset = new Vector3(50, 0, 0);
private Vector3 frontOffset = new Vector3(0, 1.58f, 0.61f);
private Vector3 sideRotation = new Vector3(0, 270, 0);
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKey("space")) {
float desiredAngle = plane.transform.eulerAngles.x;
Quaternion rotation = Quaternion.Euler(desiredAngle, 0, 0);
transform.SetPositionAndRotation(plane.transform.position + (rotation * frontOffset), rotation);
} else {
transform.SetPositionAndRotation(plane.transform.position + sideOffset, Quaternion.Euler(sideRotation));
}
}
}
and [here's a link to the game][1]. Just hold Space and either Up or Down until the plane goes into a loop.
I've tried adding +90 to the angle of rotation when plane.transform.eulerAngles.x goes above 0.5 or below -0.5 but that doesn't work. I've tried multiplying the final rotation by * -1 when it passes those thresholds as well but no luck either. Any advice you could give would be appreciated. If you need more information, let me know.
[1]: https://play.unity.com/mg/other/fly-my-guycamerarotationeuler anglesSun, 07 Feb 2021 18:48:27 GMTJaffinchHow to properly reference a Euler?
http://answers.unity.com/questions/1798255/how-to-properly-reference-a-euler.html
I am trying to make a basic camera movement portion of my script though it is working terribly and no Rotation numbers seem match up with what it is displaying in the Inspector and what is being printed out by the console..
I am trying to make the camera have a roll around effect over the top of the object (as seen in the video), though what is happening is I cannot seem to match up the numbers in the Inspector my GameObjects transfrom is set to Rotation: x:180, y:180, z:180 though it states that the First Euler angle in the console I am being told that the Angle is infact 0° not 180°
Video of my Intentions for this code: https://youtu.be/6Zoa-uSiVHY
The aim is persay the cameras rotation is 0,0,0 (y rotation is not used for this purpose) if I push the [W] key the camera tilts forward on the x rotation until it hits its maximum, the maximum should be 45° and the minimum being 0° so If I hit that 45° limit, I now can no longer move forward no matter if Im pressing [W] or not, now I should be able to press [S] to start moving backward back toward 0°.
The same goes for Moving Left and right, though the difference is that 0° is the Middle ground, +50° is tilted all the way to the right and -50° is tilted all the way to the left.
So long story short how I i make a Minimum and Maximum that the rotations can play around and be contained in? as the way my code currently works is
Right(+z): Stop at +50° which in Euler Terms is 50° and in Unity Transform terms is 230
Left(-z): (Should) Stop at -50° which in Euler Terms is 310° and in Unity Transfrom Terms is 130
Forward (+x): (Should) Stop at +45° which in Euler Terms is 330° and in Unity Transform terms is 210
Backward(-x): (Should) Stop at 0° which in Euler Terms is 0° and in Unity Transform Terms is 180
Code:
void moveLeft()
{
Debug.Log("z Rotation is Currently: " + cameraTrans.eulerAngles.z);
if (cameraTrans.eulerAngles.z < 310 || cameraTrans.eulerAngles.z > 50)
{
cameraTrans.Rotate(0, 0, -Time.deltaTime * moveSpeed);
currentXAngle = cameraTrans.eulerAngles.x;
currentZAngle = cameraTrans.eulerAngles.z;
}
}
void moveRight()
{
Debug.Log("z Rotation is Currently: " + cameraTrans.eulerAngles.z);
if (cameraTrans.eulerAngles.z < 50 || cameraTrans.eulerAngles.z > 310)
{
cameraTrans.Rotate(0, 0, Time.deltaTime * moveSpeed);
currentXAngle = cameraTrans.eulerAngles.x;
currentZAngle = cameraTrans.eulerAngles.z;
}
}
void moveForward()
{
Debug.Log("x Rotation is Currently: " + cameraTrans.eulerAngles.x);
if (cameraTrans.eulerAngles.x > minXRot)
{
cameraTrans.Rotate(-Time.deltaTime * moveSpeed, 0, 0);
currentXAngle = cameraTrans.eulerAngles.x;
currentZAngle = cameraTrans.eulerAngles.z;
}
}
void moveBackward()
{
Debug.Log("x Rotation is Currently: " + cameraTrans.eulerAngles.x);
if (cameraTrans.eulerAngles.x < maxXRot)
{
cameraTrans.Rotate(Time.deltaTime * moveSpeed, 0, 0);
currentXAngle = cameraTrans.eulerAngles.x;
currentZAngle = cameraTrans.eulerAngles.z;
}
}
Please understand I have been stuck on this for over 2 days now, trying all different types of numbers variations and code, so my code is super messy now and probably to some does not make any sense
(for today I have been ignoring to work on the forward and backward functions.. it is broken beyond beleif and am simply trying to work out it's left and right movement before I bombard myself with 4 different angles to try get right at once.)
Current Code at work Video:
https://youtu.be/o6fjq-dJupArotationrotateeulerangleseulereuler anglesSun, 20 Dec 2020 06:45:20 GMTFuryFight3rRotating player with Quaternion and RotateTowards
http://answers.unity.com/questions/1775345/rotating-player-with-quaternion-and-rotatetowards.html
My 3D character is a Cow and can be pushed away by a collision, in which i'll apply a Force and Torque so that the character can be knocked back approprially. When the character enters collision, it goes into a 'Ball' position, in which it just rolls around like a ball. This i'm achieving already. However, now I wanted to rotate the Cow in such way that it is able to transition into the normal/walkable position. For example if the Cow is in the Ball position, with its belly up, facing forward in the World, i just want to rotate around the Z axis so that the belly is facing down, and now I can transition into normal/walkable position. I am already rotating the player, but for some reason, after rotation, the character ends up always facing forward in world coordinates but that's not what I wanted. I want that, if the character is facing to the right with the belly up, it will still face that direction but with the belly down. How can i achieve this? I think im close but can't get there.
private IEnumerator StandUp() {
float yRot = rb.rotation.y;
Quaternion targetRotation = Quaternion.Euler(0, yRot, 0);
Vector3 direction = targetRotation * Vector3.forward;
while (Vector3.Angle(transform.forward, direction) > 1) {
rb.rotation = Quaternion.RotateTowards(rb.rotation, targetRotation, 8f);
yield return new WaitForSeconds(0.00f);
}
{ ... } // animator controller -> set normal/walking state
}rotationquaternioncharacter movementrotatetowardseuler anglesSat, 26 Sep 2020 14:45:03 GMTSerge144Rotate Bezier Curve around object following mouse position
http://answers.unity.com/questions/1758852/rotate-bezier-curve-around-object-following-mouse.html
I am trying to rotate my object towards my mouse position on a radius position which has a Line renderer component as well as a custom bezier curve script that is getting 3 `Vector3` points `p0`, `p1`, and `p2` to trace the curve. (It also take a many vertexes to make the curve smoother).
Here is my Bezier Curve code :
public class BezierCurve : MonoBehaviour
{
public Vector3 p0;
public Vector3 p1;
public Vector3 p2;
public LineRenderer lineRenderer;
[Range(1, 64)]
public int vertexCount = 12;
private void Update()
{
List<Vector3> pointList = new List<Vector3>();
for (float ratio = 0f / vertexCount; ratio <= 1; ratio += 1.0f / vertexCount)
{
Vector3 tangentLineVertex1 = Vector3.Lerp(p0, p1, ratio);
Vector3 tangentLineVertex2 = Vector3.Lerp(p1, p2, ratio);
Vector3 bezierPoint = Vector3.Lerp(tangentLineVertex1, tangentLineVertex2, ratio);
pointList.Add(bezierPoint);
}
lineRenderer.positionCount = pointList.Count;
lineRenderer.SetPositions(pointList.ToArray());
}
}
I am feeding the 3 points from my player object using it's sprite radius as a base for the position :
public class PlayerManager : MonoBehaviour
{
public GameObject shield;
private SpriteRenderer sprite;
[SerializeField]
private float shieldDistance = 0f;
void Start()
{
sprite = GetComponent<SpriteRenderer>();
BezierCurve curve = shield.GetComponent<BezierCurve>();
float x = sprite.bounds.extents.x + shieldDistance;
float y = sprite.bounds.extents.y + shieldDistance;
curve.p0 = new Vector3(0, y + 0.05f, 0f);
curve.p1 = new Vector3(x, y, 0f);
curve.p2 = new Vector3(x + 0.05f, 0, 0f);
}
}
This is what it gives me :
![alt text][1]
[1]: /storage/temp/164865-curve-no-gizmo.png
In the screenshot above, I would like the curve to rotate around the red circle following the mouse position.
I tried to rotate my player towards the mouse using the `transform.lookAt` method but it doesn't rotate my curve along side with it (and I don't want the player to rotate really..)
I also tried to apply a `Quaternion.Eulers` on each of my point and the rotation seems to work but it just goes crazy and never stop rotating even when I am not moving the mouse :
Vector2 b = Camera.main.WorldToViewportPoint(p0);
Vector2 a = (Vector2)Camera.main.ScreenToViewportPoint(Input.mousePosition);
float angle = Mathf.Atan2(a.y - b.y, a.x - b.x) * Mathf.Rad2Deg;
p0 = Quaternion.Euler(new Vector3(0f, 0f, angle)) * p0;
p1 = Quaternion.Euler(new Vector3(0f, 0f, angle)) * p1;
p2 = Quaternion.Euler(new Vector3(0f, 0f, angle)) * p2;
Is there any other way that I could apply a rotation on my 3 points following the mouse position and preferably to have the p1 to be the one that is pointing toward the mouse, so the curve is always "center" to the mouse.c#rotationrotatelinerenderereuler anglesThu, 06 Aug 2020 07:10:13 GMTSnazshUnderstanding euler angles and how to properly clamp rotation.
http://answers.unity.com/questions/1742912/understanding-euler-angles-and-how-to-properly-cla.html
When rotating any object around the Z axis I get what I expect. Euler angles will go from 0° to 360° and wrap around again.
![alt text][1]
[1]: /storage/temp/162164-unityeulerangles.png
When rotating around the X axis the Editor value displays what you would expect however the transform.eulerAngles goes from 0° to 90° then back down to 0° then jumps to 360° then down to 270° then back up to 360°. When going past 90° to 270° the Y and Z values jump to 180°.<br><br><br>
Eventually I will clamp the X and Z values to not rotate more than 20° but before I do that I need to understand what is going on. Why the sudden jump in values when X is rotated more than 90°?clamped rotationeuler anglesunderstanding logicWed, 17 Jun 2020 08:57:09 GMTAllTheGoodNamesWereTakenOrder of the rotations around the axes with Euler angles
http://answers.unity.com/questions/1713103/order-of-the-rotations-around-the-axes-with-euler.html
According to the [documentation][1], with Euler angles:
> rotations are performed around the Z axis, the X axis, and the Y axis, in that order.
When I try to reproduce it in the inspector and with scripts, it seems that the rotation order is "YXZ", and not "ZXY".
Do you know the reason of this behaviour ? If Unity use the "YXZ" order, do you know how to convert the euler angles to "ZXY" ?
You can check the GIF files or the script below:
using UnityEngine;
public class RotationTest : MonoBehaviour
{
public Vector3 rotation;
public bool useYXZ = true;
private void Awake()
{
if (useYXZ) ApplyQuaternionYXZ();
else ApplyQuaternionZXY();
Debug.Log(transform.eulerAngles);
}
private void ApplyQuaternionYXZ()
{
transform.RotateAround(transform.position, transform.up, rotation.y);
transform.RotateAround(transform.position, transform.right, rotation.x);
transform.RotateAround(transform.position, transform.forward, rotation.z);
}
private void ApplyQuaternionZXY()
{
transform.RotateAround(transform.position, transform.forward, rotation.z);
transform.RotateAround(transform.position, transform.right, rotation.x);
transform.RotateAround(transform.position, transform.up, rotation.y);
}
}
![Example of YXZ][2]
![Example of ZXY][3]
[1]: https://docs.unity3d.com/ScriptReference/Transform-eulerAngles.html
[2]: /storage/temp/155543-yxz.gif
[3]: /storage/temp/155544-zxy.gifquaterniondocumentationeuler anglesTue, 31 Mar 2020 16:30:38 GMTFlo_P,How to clamp my turret rotation?
http://answers.unity.com/questions/1703997/how-to-clamp-my-turret-rotation.html
][1]][1]
I have a turret that looks to the enemy object it works perfectly, but I don't know how to clamp the rotation please help!
void Update ()
{
if (target == null)
return;
Vector3 dir = target.position - xPivot.position;
Quaternion lookRotation = Quaternion.LookRotation(dir);
Vector3 yRotation = Quaternion.Lerp(yPivot.rotation, lookRotation, Time.deltaTime * turnSpeed).eulerAngles;
Vector3 xRotation = Quaternion.Lerp(xPivot.rotation, lookRotation, Time.deltaTime * turnSpeed).eulerAngles;
yPivot.rotation = Quaternion.Euler(0f, yRotation.y, 0f);
xPivot.rotation = Quaternion.Euler(xRotation.x, yRotation.y, 0f);
}rotationeulerangleseulereuler anglesSun, 01 Mar 2020 20:47:39 GMT$$anonymous$$Tilt first person camera's z axis while turning
http://answers.unity.com/questions/1694569/tilt-first-person-cameras-z-axis-while-turning.html
I am working on controls for a bipedal tank, that has a boost/dash feature. When the boost/dash mode is activated by holding a button the tank changes from a traditional WASD strafing movement to something like a jet fighter but on ground. Now when the player tries to turn while boosting the camera needs to tilt with the turning direction to make it feel smoother.
[Here is a video showing my problem][1]
This is the script added to the main camera
public class PlayerCameraTilt : MonoBehaviour
{
private Camera viewCam;
private CharacterController characterController;
public float tiltAmount = 10f;
private void Start()
{
viewCam = GetComponent<Camera>();
characterController = GetComponentInParent<CharacterController>();
}
private void Update()
{
if (characterController.GetComponent<PlayerController>().movementState == MovementModes.boost)
{
float inputValue = Input.GetAxis("MoveHorizontal") * tiltAmount;
Vector3 euler = transform.localEulerAngles;
euler.z = Mathf.Lerp(euler.z, -inputValue, 5f * Time.deltaTime);
transform.localEulerAngles = euler;
}
else
{
Vector3 euler = transform.localEulerAngles;
euler.z = Mathf.Lerp(euler.z, 0f, 5f * Time.deltaTime);
transform.localEulerAngles = euler;
}
}
}
It kind of works, BUT only in one rotation direction, namely the positive one. If i turn the other way the angle becomes negative and does a whole 360 degree rotation until it ends up at 0 degrees.
I tried around with the built in Quaternion methods like Quaternion.Rotate() or Quaternion.AngleAxis() but they didn't work because Quaternion.Rotate() doesn't tilt the Camera but completely rotates it. Quaternion.AngleAxis() works with the whole tilt angle limit but also does something to the other axis which i don't want to modify.
Is there a way to prevent the camera from doing a complete 360° rotation when the axis input becomes negative and just tilt in a slight angle?
[1]: https://youtu.be/hNyjPL4NQVQcameraquaternioncamera rotationeuler anglesMon, 27 Jan 2020 23:17:11 GMTBreakSilenceQuaternion.slerp messing with wrong axies
http://answers.unity.com/questions/1680425/quaternionslerp-messing-with-wrong-axies.html
So I'm trying to write a script to rotate my sled between -10 and 10 degrees on the X axis when pressing the left and right arrow keys respectively. What currently happens is that the sled will flip entirely the wrong direction on the Y axis and begin to spiral out.
I have been trying to figure this out for days, can anyone help?
private void Start()
{
rb.transform.rotation = Quaternion.Euler(0, 180, 20);
AngleL = Quaternion.Euler(-10, rb.rotation.y, rb.rotation.z);
AngleR = Quaternion.Euler(10, rb.rotation.y, rb.rotation.z);
Physics.gravity = new Vector3(0, -400f, 0);
}
void FixedUpdate()
{
transform.Translate(new Vector3(-moveSpeed,0,0) * Time.deltaTime);
if(Input.GetAxis("Horizontal") > 0)
{
transform.Translate(new Vector3(0,0,turnSpeed) * Time.deltaTime);
//anim.SetTrigger("Right");
currentAngle = AngleR;
transform.rotation = Quaternion.Slerp(transform.rotation, currentAngle, 0.1f);
}
if (Input.GetAxis("Horizontal") < 0)
{
transform.Translate(new Vector3(0,0,-turnSpeed) * Time.deltaTime);
currentAngle = AngleL;
transform.rotation = Quaternion.Slerp(transform.rotation, currentAngle, 0.1f);
}
}rigidbodyrotation axiseuler anglesquaternion.slerpTue, 26 Nov 2019 17:40:56 GMTLukeThePunk666Read rotations around local axis
http://answers.unity.com/questions/1669900/read-rotations-around-local-axis.html
Hi! I'm new with Unity and I may need some help for a project.
In my work I need to get the angle of rotation of an object around one of its own axis. I know that EulerAngles gives me the rotation angles in world coordinates and localEulerAngles in parents coordinates. But i really need to know the angle of rotations in the object reference frame.
Is there a way?
Thanksrotationeuler anglesSat, 05 Oct 2019 12:06:07 GMTDan1994How to reflect object's direction when collides wall ?
http://answers.unity.com/questions/1669534/how-to-reflect-objects-direction-when-collides-wal.html
Hi,
I move enemy by using rigidbody.velocity and pick a random euler angle and instantiate it on floor.
i stuck on reflecting enemy when it collides objects tagged "Wall". I want enemy to reflect with random euler angle when it collides wall.
I write this code so far and enemy reflects but it sticks with wall. So can you help me to make it work properly. Thanks.
void Awake()
{
rb_Enemy = GetComponent<Rigidbody>();
PickDirection();
}
void FixedUpdate()
{
EnemyMovement();
}
public void PickDirection()
{
axes = new float[8]{12, 24, 42, 72, 192, 204, 222, 252};
int indexAxe = Random.Range(0, axes.Length);
randomAxe = axes[indexAxe];
}
public void EnemyMovement()
{
Vector3 enemyDirection = Quaternion.AngleAxis(randomAxe, Vector3.up) * transform.forward;
rb_Enemy.velocity = enemyDirection * eThrust * Time.deltaTime;
}
public void ReflectEnemy()
{
enemy.transform.Rotate(0, randomAxe +90, 0);
}
public void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Wall")
{
ReflectEnemy();
}
}collisionvelocityreflectionwalleuler anglesFri, 04 Oct 2019 00:03:44 GMTCoolCosmosrotation.eulerAngles.x not working
http://answers.unity.com/questions/1666830/rotationeuleranglesx-not-working.html
![alt text][1]
[1]: /storage/temp/146487-untitled.jpg
The only debug log is "Debug.Log(head.rotation.eulerAngles.x.ToString());" and the head is the selected object.
Why I get 352 and not -49?rotationeuler anglesSat, 21 Sep 2019 14:35:12 GMTBBalazs95Convert movement direction into rotation?
http://answers.unity.com/questions/1665953/convert-movement-direction-into-rotation.html
I'm a bit of a noob and im struggling to wrap my head around euler angles and quaternions etc so sorry if this seems a dumb question. I want to convert my Vector3 movement direction ((1, 1, 0), (1, -1, 0), etc.) into an axis of rotation. For example (1, 1, 0) would be 135, (-1, -1, 0) would be -45, (0, -1, 0) would be 0, etc. How would i go about calculating this in code?rotationtransformunity 2deulerangleseuler anglesTue, 17 Sep 2019 14:29:44 GMTMr_ZoovaskaHow to clamp rotation, following Unity's quaternion rules
http://answers.unity.com/questions/1609035/how-to-clamp-rotation-following-unitys-quaternion.html
I want to clamp my camera's pitch (rotation around its x-axis), and nearly every forum post or Unity Answers page says to use this method:
1. Grab the Euler angle for the angle to be clamped.
2. Clamp using Math.Clamp().
3. Assign the new angle to the rotation.
<br>
However, this method is precisely what Unity advises ***not*** to do in their [Quaternion and Euler Rotation article][1].
<br>
After some more searching, I found [another answer][2] that points to Unity's own MouseLook.cs script in the Standard Assets. This script uses the following function (full code in the link):
Quaternion ClampRotationAroundXAxis(Quaternion q)
{
q.x /= q.w;
q.y /= q.w;
q.z /= q.w;
q.w = 1.0f;
float angleX = 2.0f * Mathf.Rad2Deg * Mathf.Atan (q.x);
angleX = Mathf.Clamp (angleX, MinimumX, MaximumX);
q.x = Mathf.Tan (0.5f * Mathf.Deg2Rad * angleX);
return q;
}
<br>
I've implemented this method in my own code, with a slight alteration explained in the comments in the code.
public void Rotate(Vector3 input)
{
// The camera's pitch (xRotation) should be done about its own x-axis (Space.Self),
// but its pan (yRotation) should be down about the world's y-axis (Space.World).
// In order to accomplish this, I need to rotate the camera twice, isolating the x- and y-rotations.
Vector3 xRotation = new Vector3(input.x, 0, 0);
Vector3 yRotation = new Vector3(0, input.y, 0);
transform.Rotate(xRotation, Space.Self);
transform.Rotate(yRotation, Space.World);
// Clamp the xRotation amount - Bad way
//Vector3 currentRotation = transform.rotation.eulerAngles;
//currentRotation.x = Mathf.Clamp(currentRotation.x, pitchMin, pitchMax);
//transform.rotation = Quaternion.Euler(currentRotation);
// Clamp the xRotation amount - Good way
transform.rotation = ClampRotationAroundXAxis(transform.rotation);
}
// This method is taken in full from Unity's MouseLook.cs class in the Standard Assets.
private Quaternion ClampRotationAroundXAxis(Quaternion q)
{
q.x /= q.w;
q.y /= q.w;
q.z /= q.w;
q.w = 1.0f;
float angleX = 2.0f * Mathf.Rad2Deg * Mathf.Atan(q.x);
angleX = Mathf.Clamp(angleX, pitchMin, pitchMax);
q.x = Mathf.Tan(0.5f * Mathf.Deg2Rad * angleX);
return q;
}
However, an odd behavior occurs when I try to rotate the camera beyond the clamp: the camera begins to roll about its z-axis. (GIF included below.) What's going on here, and how do I fix it? Is it caused by my two-rotation solution?
<br><br>
![Issue in action][3]
[1]: https://docs.unity3d.com/Manual/QuaternionAndEulerRotationsInUnity.html
[2]: https://answers.unity.com/questions/1125669/how-to-clamp-rotation.html
[3]: /storage/temp/134206-unityrotationissue.gifrotationquaternioneuler anglesTue, 05 Mar 2019 06:23:02 GMTJDCAce55Smoothly rotating a gameObject 90 degrees on a button press and being able to rotate it in the opposite direction off of the same button.
http://answers.unity.com/questions/1600895/smoothly-rotating-a-gameobject-90-degrees-on-a-but.html
I'm having trouble rotating my magnification scope +90 degrees on a button press to move it to the seeing position through to my holographic sight. Everything I have tried through Update will just snap the scope to its position without smoothly moving.
The Code For That:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MagnifierRotation : MonoBehaviour
{
public GameObject Magnifier;
Transform from;
Transform to;
float speed = 0.1f;
void Update()
{
if (Input.GetKeyDown(KeyCode.N))
{
transform.rotation = Quaternion.Lerp(from.rotation, to.rotation, Time.time * speed);
}
}
}
I've also tried using a Coroutine, but when I attempted to rotate the object off of its z-axis, it started rotating over all three axes instead of the z. The transform's rotation began at (-90, 0, 0), and after the coroutine rotation, of Vector3.forward, it ended up at a weird (-173. blahblahblah, 256.blahblahblah, 39.blahblahblah) instead of (0,0,0).
The Code For That:
public class MagnifierRotation : MonoBehaviour
{
private bool magnifierSwitch;
IEnumerator RotateMe(Vector3 byAngles, float inTime)
{
var fromAngle = transform.rotation;
var toAngle = Quaternion.Euler(transform.eulerAngles + byAngles);
for (var t = 0f; t < 1; t += Time.deltaTime / inTime)
{
transform.rotation = Quaternion.Slerp(fromAngle, toAngle, t);
yield return null;
}
}
void Update()
{
if (magnifierSwitch && Input.GetKeyDown(KeyCode.M))
{
StartCoroutine(RotateMe(Vector3.right * -90, 0.8f));
magnifierSwitch = !magnifierSwitch;
}
else if (!magnifierSwitch && Input.GetKeyDown(KeyCode.M))
{
StartCoroutine(RotateMe(Vector3.right * 90, 0.8f));
magnifierSwitch = !magnifierSwitch;
}
}
}
I have also tried Quaternion.Euler's and the good old transform.Rotate, but both have the same effect as the Quaternion.Lerp that just snapped the object when in Update and under an if statement checking for an input.
If there is any other way to make the object smoothly rotate on a button press in both directions after one rotation is over, please let me know before I just make a broken looking animation.rotationgameobjectquaternionlerpeuler anglesTue, 12 Feb 2019 00:24:51 GMTBLKNinja40Rotation Problem
http://answers.unity.com/questions/1564458/rotation-problem-20.html
Hello,
currently i am on the way to programm a shotgun in and a pistol is already success done. My aim is it to rotate my arm, which has a degrees down and up, for the FPS. To handle this, i want to rotate a extern gameobject and transfer its rotation on my arm. The Problem i have is, that a normal eulerangel or transform.rotate seems like to be a raw quaternion which let my arm go up and down for the aim, but it rotates in a cycle.![alt text][1]
that is there the null position of the arm : ![alt text][2]
The spot of the roation is 0,0,0 but i can`t figure out, whats the problem on it, i hope that anyone can help me :)
script code :
arm_rotation_right += Input.GetAxis("Mouse Y") * roationSpeed * Time.deltaTime;
arm_up_right.transform.eulerAngles = new Vector3(arm_up_right.transform.eulerAngles.x,
player.transform.eulerAngles.y - 145, -arm_rotation_right);
Much greetings
[1]: /storage/temp/126424-21.png
[2]: /storage/temp/126425-5467.pngrotation axiseuler anglesSun, 21 Oct 2018 04:42:08 GMTmarceltmgHow to find the 'longitude' of a collision location on a sphere collider?
http://answers.unity.com/questions/1553649/how-to-find-the-longitude-of-a-collision-location.html
I have a situation where a character is being punched in the head (which has a sphere collider), and I want to call an animation blend tree that deflects the head in the proper direction. So, one of the values I need for this (assuming the Y axis points upwards) is the Y-coordinate or longitude at which the collision happened. I expect most hits will be slightly off-center (the movement vector of the incoming object will never point exactly to the center of the collider). You can think of it like a meteor impacting the earth; they rarely come straight inwards, instead they usually graze sideways and strike at an angle.
So far, everything I've tried is either incompatible with eulers, or I can't grasp the math required to convert it. The collision.contacts[0].normal gives (roughly) the normal of the collision point - but I can't find any way to convert that to to Euler degrees. And that only works when the hit is along the 'equator' of the collider, as otherwise (imagining that the collision is at the 'south pole') the other coordinates will have far greater values. I hit the same problem if I do the math to get the vector between the collision location and the collider's center.
What am I missing or overlooking? I know I'm trying to get a 2D coordinate from a 3D sphere, but there must be some simple approach. In this particular case the collider isn't well aligned with the global default locations, but I should be able to figure out at least this part.collision detectionanglesquaternionseuler anglesSat, 15 Sep 2018 23:44:37 GMTChris45215Difficulty making a complex camera rotation
http://answers.unity.com/questions/1525590/difficulty-making-a-complex-camera-rotation.html
I'm creating a turn based game set in space in which two teams of players are on opposite sides of a three-dimensional playing field. The white team and black team begin the game with the same orientation, except that the white team is rotated 180 on the Y axis and also rotated 180 degrees on the X axis (meaning the two teams are facing each other and one team is upside down).
To make play even between turns, I need the camera to perform a complex rotation so that both players can control their own team from the same perspective on their turn. My goal is to have either player, at the beginning of his own turn, facing toward the right side of the screen with his head up toward the top and his feet down toward the bottom. My goal is to make a smooth camera rotation from (0,0,0) to (180, 0, 180) or (0, 180, 180), and then make it go back when the turn changes again.
I am not having a problem changing the position of the camera so that it points at the correct team, but I am having trouble with the rotation.
In attempting to solve this problem, I have created a focal point around which the camera orbits. When a turn change begins, the variable slerpOnBlack or slerpOnWhite turns to true. As long as this variable is true, one of the following two methods will fire on every update:
void SlerpCameraBlack(){
if (new Vector3(CameraFocus.transform.rotation.x, CameraFocus.transform.rotation.y, CameraFocus.transform.rotation.z) != new Vector3(0, 0, 0)) {
CameraFocus.transform.rotation = Quaternion.Slerp (CameraFocus.transform.rotation, Quaternion.Euler(new Vector3(0, 0, 0)), (10*Time.deltaTime));
} else {
SlerpOnBlack = false;
}
}
void SlerpCameraWhite(){
if (new Vector3(CameraFocus.transform.rotation.x, CameraFocus.transform.rotation.y, CameraFocus.transform.rotation.z) != new Vector3(0, 180, 180)) {
CameraFocus.transform.rotation = Quaternion.Slerp (CameraFocus.transform.rotation, Quaternion.Euler(new Vector3(0, 180, 180)), (10*Time.deltaTime));
} else {
SlerpOnWhite = false;
}
}
Use of these functions results in inconsistent results which I do not understand, and which lead me to believe that I must not understand clearly how to use quaternions.
Can anyone help me resolve this?quaternionslerpquaternionscamera rotationeuler anglesWed, 04 Jul 2018 23:11:44 GMTJustinTheSwiftHow to get object's X axis rotation EXACTLY as in inspector ?
http://answers.unity.com/questions/1514780/how-to-get-objects-x-axis-rotation-exactly-as-in-i.html
Hello,
I just want to know How to get object's X axis rotation EXACTLY as in inspector ?
I tried transform.eulerAngles.x it works at the beginning but when the X axis in the inspector hits 83 the one in the script starts to decrease on its own, and when the one in the script hits 0 it goes up to 340 directly..
and I also tried transform.rotation.x and when the x axis in inspector hits like 10 the one in the script is something like 0.9465872 O_o
So how to get the EXACT same value in the inspector's Transform window ?
Thanksrotationeditorinspectoraxiseuler anglesTue, 05 Jun 2018 19:55:56 GMTHarD-izzeRWhy transform.eulerAngles and rotation in editor transform are different?
http://answers.unity.com/questions/1446227/why-transformeulerangles-and-rotation-in-editor-tr.html
Why transform.eulerAngles and rotation in editor transform are different?
For example, in editor rotation is {-90, 0, 243.97}, but eulerAngles of bulObj are {270, 334, 0}
This is my code:
BulletObject bulObj = GetComponentInChildren<BulletObject>();
transform.eulerAngles = new Vector3(0, -bulObj.transform.eulerAngles.x, 0);
Debug.Log(bulObj.transform.rotation.eulerAngles);
bulObj.transform.eulerAngles = new Vector3(bulObj.transform.eulerAngles.x, bulObj.transform.eulerAngles.y, 0);editortransformquaternioneulerangleseuler anglesSun, 24 Dec 2017 09:39:20 GMTVitos23Yaw And Pitch Switched When Rolled To The Side
http://answers.unity.com/questions/1446162/yaw-and-pitch-reversed-when-rolled-to-the-side.html
I'm making a game with a free camera and when the player rolls left or right, yaw and pitch get messed up. More Specifically, when rolled 90 degrees, moving the mouse up or down makes the camera look left and right, and vice-versa. This happens to all degrees but is most noticeable at 90. I would like the rotation to be completely local. Here's the bulk of the code:
private float verticalMouse;
private float horizontalMouse;
public static Vector3 viewRotation;
void View () {
verticalMouse = Input.GetAxis("Mouse X");
horizontalMouse = Input.GetAxis("Mouse Y");
viewRotation.y += verticalMouse;
viewRotation.x -= horizontalMouse;
if (Input.GetKey(KeyCode.Q)) {
viewRotation.z += 0.5f;
} else if (Input.GetKey(KeyCode.E)) {
viewRotation.z -= 0.5f;
}
gameObject.transform.localEulerAngles = viewRotation;
}rollpitcheuler anglesyaxisSun, 24 Dec 2017 01:34:50 GMTPelferClamp an object on 2 sides
http://answers.unity.com/questions/1427389/clamp-an-object-on-2-sides.html
![alt text][1]
<h3>The Attempt</h3>
Hello, So I'm trying to clamp a wall between two sides. So black is the main wall.
<br>
<h3> What I want </h3>
I want the second wall to join the first wall (The black wall), and it must either be between 30 and 120, or it can be between 210 and 290. So where ever the mouse is closest, it will jump there.
<br>
<h3> The issue I've been banging my head against the wall for. </h3>
Currently my issue is based on the walls direction. so if it faces down, left or right the code does not work.
![alt text][2]
<br>
<br>
<b>So I'm out of ideas. Any help would be greatly appreciated</b>
I asked a similar question [here][3], but I thought this was more better explained.
[1]: /storage/temp/104656-wall-v3.png
[2]: /storage/temp/104658-untitled.jpg
[3]: http://answers.unity3d.com/questions/1427313/limiting-rotation-based-on-direction.htmlc#rotationdirectioneuler anglesSun, 29 Oct 2017 15:08:02 GMTJaredHDform a 3d straight line using opengl
http://answers.unity.com/questions/1426013/form-a-3d-straight-line-using-opengl.html
Hi i am trying to display a line using opengl. i have a 3d point (x, y, z, azimuth angle, elevation angle ,roll angle) which acts as start point of line. and using this single point i want to create a straight line knowing the length of line, as the angles of start point changes the direction of the line should also change..how do i achieve this???3d modelsrayopengleuler anglesThu, 26 Oct 2017 06:33:20 GMTrkantiAnnoying Quaternions and Eulers
http://answers.unity.com/questions/1421930/annoying-quaternions-and-eulers.html
private Vector3 v3;
void Start () {
v3 = new Vector3 (0f, 359f, 0f);
Debug.Log ("" + transform.rotation.eulerAngles);
Debug.Log ("" + v3);
transform.Rotate (v3);
}
// Update is called once per frame
void FixedUpdate () {
v3.y = v3.y + 3f;
transform.Rotate (v3);
Debug.Log ("tr " + transform.rotation.eulerAngles);
Debug.Log ("v3 " + v3);
v3 = transform.rotation.eulerAngles;
Debug.Log ("---------------------------------------------------");
}
and here is my log:
![alt text][1]
[1]: /storage/temp/103920-screenshot-42.png
359 degree + 3 becomes 1 degree, 5 + 3 becomes 13, 13 + 3 becomes 29
i just have no ideac#quaternionangleseuler angleshealthTue, 17 Oct 2017 18:29:57 GMTdniwe_koreGet local euler angles by Quaternion
http://answers.unity.com/questions/1417640/get-local-euler-angles-by-quaternion.html
What do I need that is to get object's local euler angles by given global rotation. One solution is to set object required rotation and then roll back, but it seems ugly:
static Vector3 GetLocalEulerAtRotation(this Transform transform, Quaternion targetRotation)
{
var tempRotation = transform.rotation;
transform.rotation = targetRotation;
var localEuler = transform.localEulerAngles;
transform.rotation = tempRotation;
return localEuler;
}
Is there any another more compact and more optimized solution?rotationquaternioneuler anglesSun, 08 Oct 2017 11:22:39 GMTlPVDlRotating an object around the centre of its mesh, using Quarternion AxisAngle
http://answers.unity.com/questions/1416889/rotating-an-object-around-the-centre-of-its-mesh-u.html
I'm working on a legacy project made entirely by someone else.
A group of objects called "surfaces" are instantiated using data from a JSON file. The meshes assigned to these objects are generated procedurally, so from what I can tell, the rotation origin is offset by a different amount for every object.
If I use RotateAround, with the point being the center of the mesh, the Euler rotation produces completely the wrong set of rotations, but in the right position.
Using Quaternion.AxisAngle produces the correct rotations, but in the wrong position.
Using the rotation tool in the editor at runtime produces perfect results, as the tool seems to know to take the center of the mesh as the origin for rotation, and simply translates the object to compensate.
Is there a way to replicate what the Editor rotation tool is doing in a script?rotationquaternioneuler anglesFri, 06 Oct 2017 12:12:01 GMTFoxAlive555Rotation Problem
http://answers.unity.com/questions/1410941/rotation-problem-18.html
Hi, I have a problem with rotation or most Specifically Euler Angels, I want to put an if statement that says if the X rotation is smaller than 90 then you can execute the if statement so I wrote
if (transform.localEulerAngels.x <90)
{
// orders
}
but when the game object Euler angel on the X-axis is after 90 degree the X value Starts decreasing and the if statement orders continue and the script fails, anybody has a good idea on how to do it, and thanks.
[This is my script]
using UnityEngine;
public class Air_Plane_Script : MonoBehaviour {
public float RollSpeed;
public float PitchSpeed;
public float PlaneSpeed;
public float Gravity;
public float DesiredRotationX;
private float RollInput;
private float PitchInput;
public float YawSpeed;
private bool Change = true;
void Update()
{
ApplyPlaneControls();
ApplyPlanePhysics();
}
void ApplyPlaneControls()
{
transform.position += transform.forward * PlaneSpeed*Time.deltaTime;
RollInput = Input.GetAxisRaw("Horizontal")*RollSpeed*Time.deltaTime;
PitchInput = Input.GetAxisRaw("Vertical")*PitchSpeed*Time.deltaTime;
transform.Rotate(PitchInput,YawSpeed,-RollInput);
}
void ApplyPlanePhysics()
{
PlaneSpeed -= transform.forward.y * Gravity * Time.deltaTime;
if (PlaneSpeed < 10 && // Here is where i want to put the if statement that i asked the question for)
{
Change = false;
DesiredRotationX += 50f * Time.deltaTime;
transform.localEulerAngles = new Vector3(DesiredRotationX, transform.localEulerAngles.y,transform.localEulerAngles.z);
}
else
{
Change = true;
}
if (PlaneSpeed < 1)
{
PlaneSpeed = 1;
}
}
}c#euler anglesrotatonSat, 23 Sep 2017 10:40:33 GMTMAMFMAHMOUD