- Home /
Rotate only mesh (not whole GameObject)
Hi all, I'm trying to billboard the mesh on a GameObject without rotating the collider too. I've tried fiddling with: renderer.transform, MeshFilter.transform, MeshRenderer.transform... all of them seem to ref the main object transform. Any idea if this is possible?
Did that do the trick? Don't forget to mark the question as answered if it did.
@$$anonymous$$GB this is an interesting but tricky question. The only way I see it to be possible is to re-position all the vertices in the mesh. Building an array of verts and assigning new positions for the verts would be the easy part, the tricky part would be calculating what rotation from the origin of the gameObject to apply to the verts (hence a comment, not an answer). I shall think about this and post an answer if I solve the vert relocation equation to billboard to the camera.
EDIT : I have just submitted an answer ($$anonymous$$ $$anonymous$$ay) =]
Answer by Khada · Aug 20, 2012 at 10:53 AM
The transform on that object hold the transform data for all components attached to it. You'll need to create a new GameObject and keep your mesh/collider separate so that you can manipulate them independently.
EDIT: Or what deekr said, which effectively gives the mesh a new game object behind the scenes.
But if it's not yet a Skinned$$anonymous$$eshRenderer it's way better to just have the mesh as child. The Skinned$$anonymous$$eshRenderer has some overhead since it has to "skin" the mesh each frame.
@$$anonymous$$hada This seems to be the most intuitive solution (parenting another GameObj), cheers.
Answer by AlucardJay · Aug 20, 2012 at 06:51 PM
After some very quick testing on a cube, I think I have the solution =]
Create a new scene, create a Cube and attach the below script. Hit play, change the value rotatedAngleY in the Inspector, then left-click in the game view. In the scene view, the collider should be visible as a wireframe, and this should be at the original rotation while the cube has rotated to the angle set by rotatedAngleY. How you calculate and implement rotatedAngleY is up to your scene. Hope this helps =]
#pragma strict
public var theObject : Transform;
private var theMesh : Mesh;
private var originalVerts : Vector3[];
private var rotatedVerts : Vector3[];
public var rotatedAngleY : float = 0.0;
function Start()
{
if (!theObject)
{
theObject = this.transform;
}
theMesh = theObject.GetComponent(MeshFilter).mesh as Mesh;
originalVerts = new Vector3[ theMesh.vertices.length ];
originalVerts = theMesh.vertices;
rotatedVerts = new Vector3[ originalVerts.length ];
}
function Update()
{
if (Input.GetMouseButtonUp(0))
{
RotateMesh();
}
}
function RotateMesh()
{
var qAngle : Quaternion = Quaternion.AngleAxis( rotatedAngleY, Vector3.up );
for (var vert : int = 0; vert < originalVerts.length; vert ++)
{
rotatedVerts[vert] = qAngle * originalVerts[vert];
}
theMesh.vertices = rotatedVerts;
}
Yes, this does work of course, but i think it's a bit uneffective. At least create the quaternion outside of the for loop. You create a new one for each vertex, but the values are the same.
Btw, when you rotate around the local axis it's a bit easier to use
Quaternion.Euler(0,rotatedAngleY,0);
Thanks for picking that up, I have edited the answer. But am questioning if I should remove it (or convert to comment).?
I am very inexperienced and have my own trouble implementing rotations, a fellow user showed me AngleAxis here: http://answers.unity3d.com/questions/300162/method-to-rotate-a-vector3-around-a-vector3-by-spe.html , and looking back to that I can see where I replaced my transform.RotateAround with AngleAxis in the loop (as you correctly pointed out is inefficient).
If you have any suggestions on how to make this not uneffective (or elaborate) I would be very grateful. (personally I couldn't see why the object isn't bounded by and correctly aligned to the collider, but $$anonymous$$e is not to question the vision or idea !)
Perhaps I should again refrain from submitting answers for a while, until I get some more practice and experience in, am wary of this after the first errors on the UV coordinates of a native cube question.
While this would work, its very processor intensive to do every update!
no worries, thanks for the feedback (and putting up with my enthusiasm!). I should've thought of separate gameObjects for obj and collider, having done that myself for a high-poly mesh with a custom low-poly mesh collider =]
@numberkruncher: Exactly the same way. Just multiply them with the quaternion
Answer by Sindorej · Dec 14, 2013 at 03:32 PM
In my opinion the best way is to create a game object that is a child of the main object and add the mesh collider to the child.
Answer by deekr · Aug 20, 2012 at 11:54 AM
You could give the mesh a root bone in your modeling package and then transform it.
Or, in Unity, do the same thing by parenting an empty gameObject to the mesh.
Your answer
Follow this Question
Related Questions
How to rotate child 3D object to match bend of mesh 1 Answer
Change Pivot Point- Maya 3 Answers
rotation script in unity 3 Answers
[Help] Object flips around when using transform.Rotate 3 Answers
Trying to rotate weapon 0 Answers