- Home /
Camera raycast and rotated collider
Hi folks, I am programmatically creating a mesh and assigning a collider as follows:
GameObject newLedge = new GameObject(name);
Mesh newMesh = new Mesh();
newLedge.AddComponent<MeshFilter>();
newLedge.AddComponent<MeshRenderer>();
.........
newMesh.Optimize();
newMesh.RecalculateNormals();
newLedge.renderer.material = new Material(Shader.Find("Diffuse"));
newLedge.GetComponent<MeshFilter>().mesh = newMesh;
newLedge.AddComponent<MeshCollider>();
I think use ray casts from my camera to check where the ray intersects the model:
GameObject gameObject;
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray.origin, ray.direction, out hit, 1000, 1))
{
.................
}
}
This works great. However, whenever I try to rotate the model, the ray cast still hits the side of the object as if it were not rotated. Any ideas why this is? Here is the what the rotate code is. Also, it is extremely slow - is there something I Can add to the mesh/object to make the physics rotation run faster?
gameObject = GameObject.Find("Planet");
//tried but no change: gameObject.collider.transform.Rotate(0, Time.deltaTime, 0, Space.Self);
gameObject.transform.Rotate(0, Time.deltaTime, 0, Space.Self);
Thanks!
Edit: I should note, none of this stuff needs to be affected by gravity, etc, just that it needs to be able to be raycasted.
Your answer
Follow this Question
Related Questions
Returning list of Triangles, Vertices or Points by raycasting through a mesh (iPhone) 1 Answer
Physics.Raycast not checking layermask properly? 1 Answer
Camera gets flung off of the map when the player collides with certain objects. 0 Answers
How to prevent a sphere from jumping? 0 Answers
Raycasts hit triggers disabled - force collision for specific raycast 1 Answer