Vector3 Math : Small issue with camera rotation and translation

Hi there !
Here’s my setup : My camera starts as a top-down view, on coords (0,5,0), and is a child of a “center” object on coords (0,0,0). I can rotate the camera with the expected behaviour using those lines of code :

if (Input.GetMouseButton(1))
{
center.Rotate(Input.GetAxis("Mouse Y") * mouseSensitivity, 0, 0, Space.Self);
center.Rotate(0, Input.GetAxis("Mouse X") * mouseSensitivity, 0, Space.World);
}

But my code for translation is not effective : it starts going upwards when it is tilted towards the horizon. I’d like it to move on the X and Z axis according to the camera orientation, but I can’t find the right way to do math with Vector3 (I guess it is the actual solution) to get the expected behaviour.
Here’s the code (here in 2 separate line to test things more easily, I know in it’s current state it could be made in only 1 line), note that I’ve tried a few thing with adding or substracting another Vector3 and with both Space.Self and Space.world

if (Input.GetMouseButton(2))
{
center.Translate(-Input.GetAxis("Mouse X")*mouseTranslateSensitivity*(cameraYPosition.position.y/10), 0, 0, Space.Self);
center.Translate(new Vector3(0, 0, -Input.GetAxis("Mouse Y")*mouseTranslateSensitivity*(cameraYPosition.position.y/10)), Space.Self);
}

I know it’s quite a quick fix with some Vector math but I can’t find the right way to do this.
I also know the answer must already be in the forums but I’ve searched and I think I don’t have the right keywords or I can’t find a similar setup in the answers I got.

Thanks in advance for your answers.

This looks like a super-simple bog-standard mouse look script. Don’t reinvent the wheel until you’ve used a few of the millions of MouseLook scripts out there and you need something new. Try one of these:

http://wiki.unity3d.com/index.php/Category:MouseLook

ALSO… VERY important…

Never write code like that. That’s “hairy code” and no normal human can figure out what it’s doing. The computer will NOT generate any faster code because you hammered it all into one line, trust me.

Instead, break it down, one step per line, one step at a time, probably about 7 or 8 lines of code with temporary variables so you can print them out and figure out where the problem is.

How to break down hairy lines of code:

http://plbm.com/?p=248

That way when stuff doesn’t work you can reason about it.

1 Like

Ok, thanks for the advice, I’m into graphics initially so coding is still quite new to me, even after a few years using C#.
I’ll dig into those mouse look scripts, but at first glance they seem to cover rotations (yes I’m doing an orbit-like behaviour) but not movement.
(ps : sorry I feel like my english is quite bad today)

Quite commonly there are two parented Transforms, one that you drive left/right for turn head, and one that you drive up down for raise/lower head.

Here’s an example of what I mean in the shape of a gun turret:

Turret aiming/rotating:

https://discussions.unity.com/t/783469/2

I feel like your English is just fine! Welcome!

Ok thanks all of your advices are fine and all but I’ve asked a simple question which you did not actually answer.
But I’ve found my solution which simply consist of doing my vector math outside of those translates, to then use them inside of the translates.
Closing the thread, thank you !