- Home /
Rotate an object from inside (With Flipped Normals)
I have a camera inside a sphere with flipped normals. When i click and drag, i'm supposed to rotate this sphere. Here is what i done:
float rotSpeed = 20;
private void OnMouseDrag() {
float rotX = Input.GetAxis("Mouse X") * rotSpeed*Mathf.Deg2Rad;
transform.RotateAround(Vector3.up, -rotX);
}
Pretty simple code, and it works perfectly if the camera is outside the sphere. When inside tho, it doesn't work. I tried using raycasts, but they all can't hit the Sphere collider from inside. How do i do this? can't seem to find the answer anywhere
Answer by Eno-Khaon · Jul 01, 2020 at 09:57 PM
One option would be to reverse the Raycast with the expectation that the sphere is what you would intend to hit:
// If the current code is something like:
Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
// ... you could invert it using something like:
Ray invertedMouseRay = new Ray(mouseRay.origin + mouseRay.direction * sphereRadius * 1.01f, -mouseRay.direction);
Answer by LeFlop2001 · Jul 01, 2020 at 08:47 PM
Im not sure what youre probleme is but if its the collisiondection when castin rays from inside of colliders theres a checkbox under projectsettings/Physics called queries hit backfaces that solves that problem
Weirdly enough it doesn't solve the problem, even with that i can't interact with the sphere from inside
Answer by Bunny83 · Jul 02, 2020 at 10:18 AM
Well the OnMouseDrag callback is only called when Unity automatically detects a gameobject under the current mouse position. For this purpose Unity performs a raycast for you. Since you are located inside the object you can not hit the object.
Unless you really need that "when I click on that object" thing, I would suggest you just do:
void Update()
{
if (Input.GetMouseButton(0))
{
float rotX = Input.GetAxis("Mouse X") * rotSpeed*Mathf.Deg2Rad;
// rotate here
}
}
ps: Your RotateAround call doesn't look like it would work. You're missing the point you want to rotate around. See the documentation for RotateAround. When you just want to rotate around the worldspace up axis you could simply use Rotate with Space.World
transform.Rotate(0, rotX, 0, Space.World);
Your answer
Follow this Question
Related Questions
hit.normal rotation problem, help please really confused 1 Answer
How to get rotation relative to the ground normal? 0 Answers
Raycast hits and then doesn't fire again? 2D Game Raycasting Problem 1 Answer
Rotating with a preferred driection. 0 Answers
Rotating an object to equal normals of object below 1 Answer