This site uses strictly necessary cookies. More Information

X- Home /

# rotate vector around local axis

Hello,

I am trying to rotate a vector3 ( new Vector3 (0, 0, dist) ) around 3 axes locally.

-Edit2: see comments below -Edit: I think I am very close to it but something goes wrong the axes I think:

```
Vector3 _vec = new Vector3 (0, 0, dist);
_vec = Quaternion.AngleAxis(_rotYaw.y, Vector3.up) * _vec;
_vec = Quaternion.AngleAxis(_rotationVec.x, Vector3.right) * _vec;
_vec = Quaternion.AngleAxis(_rotationVec.z, Vector3.forward) * _vec;
```

Here is a pic (the 3d sphere is just there to visualize the rotation, the yellow and green lines are important, the green line is where the vector should point to or whatever how that is called):

Pretty please with sugar on top, please help, I just want to know how to rotate vector3 around axes locally.

**Answer** by FlaSh-G
·
Aug 12, 2017 at 01:10 PM

I don't know where the problem is with your code (not saying there is none), but why not just put all rotations into one quaterion and then multiply it?

```
Quaternion.Euler(_rotationVec) * _vec
```

Hi Flash-G, I tried that but doesn't give the desired effect. I want rotation around the axes locally, e.g. similar to rotation in flight/spacesims. $$anonymous$$ore specifically; pitch and roll world-rotation like and jaw local-rotation like. Not sure if I am using the right definitions, otherwise I can try to elaborate it more if it is still unclear.

Oh, then you shouldn't define your rotation with three axes. That's basically an euler angles vector. The (or one?) reason Unity doesn't use that for rotations internally (ins$$anonymous$$d using Quaternions) is because euler angles can cause a gimbal lock.

How about you have a Quaternion (maybe transform.localRotation already is that) and multiply it with other Quaternionswhen input happens?

I forgot to mention something, the jaw rotation is stored in an other Quaternion. But I am almost there with your advices but I now only still have a problem when roll != 0 and also when pitch is negative (so it now only works when roll == 0 and pitch >= 0 ):

```
Vector3 _vec = new Vector3(0, 0, dist);
_vec = Quaternion.Euler(new Vector3(_rotationVec.x, 0, _rotationVec.z)) * _vec; //add pitch/roll to direction
_vec = Quaternion.AngleAxis(_rotYaw.eulerAngles.y, Vector3.forward) * _vec; // add jaw (note that I tried Vector3.upward too but that does nothing)
```

Hi @FlaSh-G , do you perhaps know why the following code works but only when _rotationVec points forwards or backwards, but not when it points to the left or right? (I think it has to do with Vector3.forward in RotateAround but not sure);

```
Vector3 _vec = new Vector3(0, 0, dist);
_vec = Quaternion.Euler(new Vector3(_rotationVec.x, 0, _rotationVec.z)) * _vec; //add pitch/roll to direction
GameObject _rotGO = new GameObject();
_rotGO.transform.position = _vec;
_rotGO.transform.RotateAround(Vector3.zero, Vector3.forward, _rotYaw.eulerAngles.y); //add local yaw rotation
_vec = _rotGO.transform.position;
Destroy(_rotGO); //clean up
```

You mentioned that you want to affect the local-space rotation of the object, but it looks like you are changing the WORLD-space rotation of the object with:

```
_rotGO.transform.RotateAround
```

I suspect you want to adjust _rotGO.transform.localRotation ins$$anonymous$$d. This will probably simplify things sufficiently that you can use a single Quaternion.Euler to generate that quaternion.

To be honest, at a certain point rotation code becomes very hard to follow for me. I suspect it's like that for many people. Anyway... It should be possible to set the rotation once ins$$anonymous$$d of rotating the thing and then rotating it again. Even more so, it should be very possible without doing the expensive action of creating a GameObjecta and then deleting it again.

I recommend getting familiar with the methods Quaternion has to offer and see if what you're trying to accomplish hasn't been conveniently bundled for you already.

Hi, I already tried most of those functions but there doesn't seem to be anything that lets me enter the rotation values as euler angles while actually rotating it around the axis ins$$anonymous$$d of standard rotation. Very frustrating. :/ Quaternion.AngleAxis (in my first post) seems like the solution but it just didn't work for me for some reason. Anyway thanks for your time.

**Answer** by Gandarufu
·
Jun 10, 2020 at 09:31 AM

Is this still an issue? I solved a similar problem with the following code:

```
transform.Rotate(Vector3.up, mouseDelta.x, Space.Self);
transform.Rotate(Camera.main.transform.right, mouseDelta.y, Space.World);
```

This results in a rotation similar to the prefab preview window in the inspector. Maybe the code above helps.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Checking vector3 gameObject is about to move to (pathfinding collision detection) 1 Answer

Dynamically change Vector3.up/right?,How to make a cylinder chase mechanic? 2 Answers

Quaternion amount/value of rotation? 2 Answers

Flipping a 2D sprite based on direction along the x axis (2D)? 2 Answers

How to get offset postition after rotation ? (Spent hours figuring it out) 3 Answers