Orientation Issue 1
Hi everybody. First time posting. I'm creating a game similar to the 3D roller ball tutorial, but I would like to add in the function of changing your camera angles so I can maybe hide some items and the player will need to rotate the camera to see around corners. I've added 3 other cameras, and set them around my character to give me a 360* view. My question is this - is there a way for me to change the orientation of my player so that when I switch to another camera, my player's orientation switches with it? Currently, when I switch to a different camera, my players orientation stays the same, so hitting UP now actually makes my player go RIGHT because he's using the original orientation input.
you should have a look at the character controller scripts that come with unity. basically you need to transform the direction of your input to the rotation(specifically the y rotation) of the current camera. in English that means you move right relative the your orientation, not relative to the world.
heres how I would do it. using C# if that matters
Detect when you orientation changes, this can be as simple as an event when you change your orientation. you could even pass the active camera into a function.
Store the current orientation now that you have detected its changed. when need to hold onto a reference of this camera so we can use it to modify our direction of movement. you can do this by creating a member wise variable.
Get the Camera's to the block of code that calculates movement. GetComponent() is useful here.
$$anonymous$$ultiply your input vector by the current cameras rotation
Alrighty...so I gave what you said a shot. I think my C# skills were nowhere near ready to do this lol. I think I did steps 1-3 correctly...but i can't seem to apply the camera's angle to my movement. I keep getting errors saying things like "you can't multiply a Vector3 x Vector3), or when I pull out the individual values of the camera angles, and multiply my input by it, it simply makes my x values 65* stronger, so I fly through the walls when I move. Can you possibly give me an example of how you would apply the camera's angles to my movement? This is the code I use for my movement. Thanks!!!
``` void FixedUpdate() { float moveHorizontal = Input.GetAxis("Horizontal"); float moveVertical = Input.GetAxis("Vertical"); Vector3 movement = new Vector3(moveHorizontal, 0f, moveVertical); rb.AddForce(movement * speed); } ```
Answer by Psyk16 · Sep 20, 2016 at 07:23 PM
This looks fantastic! thank you so much for the quick response. I'm still very much a beginner of C#, but I think I can translate what you said in text into code. I'll be giving this a try tonight. You rock :)