- Home /
Editor Gizmo logic question? How do the gizmos move relative to camera
I am trying to build level editor for my game. I currently have built gizmos that work but I am confused by the logic of how the object moves relative to the cameras position (Sorry if the explanation sucks).
(see how the X axis flipped)
Here is the line for moving on the X axis
tempPos += focusedGameObject.transform.right * Input.GetAxis("Mouse X") * Time.deltaTime * moveSensitivity;
If my camera is looking at it from one direction it feels correct but when the camera moves around the controls feel flipped over. I am not sure of how to solve this.
Answer by Eno-Khaon · Oct 15, 2020 at 02:34 AM
If you use the Sign() of the Dot Product of the camera's right relative to the local axis' right as a multiplier, the problem should be solved fairly smoothly.
(Also, you neither need nor want to multiply by Time.deltaTime in this situation. GetAxis() with mouse input already provides only a per-frame based changed, where a constant value (i.e. held key) would need that scaling)
While there are many ways to make this system more robust (and also require further additional programming to scale input strength with distance to the object), this should help get you on the right track with what you're currently working on:
float inputScale = Mathf.Sign(Vector3.Dot(focusedGameObject.transform.right, Camera.main.transform.right));
tempPos += focusedGameObject.transform.right * Input.GetAxis("Mouse X") * moveSensitivity * inputScale;
If you want the strength of the input to vary based on angle...
If you want a low speed at a steep angle, remove Mathf.Sign()
from around it.
If you want a high speed at a steep angle, replace Mathf.Sign()
with Mathf.Tan()
and scale accordingly:
float inputScale = Mathf.Tan(Vector3.Dot(focusedGameObject.transform.right, Camera.main.transform.right) * Mathf.PI);
Note that no further accommodations need to be made in this situation because both inputs for the Dot Product are normalized vectors.
Thank you for the explanation! worked like a charm and I have direction to continue going
Your answer
Follow this Question
Related Questions
how are Mathf.SmoothDamp and Mathf.SmoothStep different 1 Answer
UNITY 3D: How to make the camera follow the player? Smoothly 2 Answers
Change the FOV from "60" to "90" smoothly when pressing "W" (C#, first time posting, beginner) 2 Answers
C# locking Main Camera's Rotations 1 Answer
GameObject transform relative camera 0 Answers