- Home /
How to rotate instantated prefab without rotating it's local directions by javascript?
I have created Wheel prefab. And instantiate it 4 times to get 4 wheels with one drawcall. Wheel have more datailed external side and less detailed internal side. During instantiation need rotate Front right and back right wheels - to make it's less detailed sides look inside. Rotation I try to do in 2 wrong ways:
var fwStifness = 0.9;
var swStifness = 0.25;
var spring = 5500;
var damper = 50;
var Vehicle : GameObject;
var Mesh_FL : GameObject;
var Mesh_BL : GameObject;
var Mesh_FR : GameObject;
var Mesh_BR : GameObject;
function Start () {
Vehicle = Instantiate(Resources.Load("IS200-prefab", GameObject),Vector3.zero, Quaternion.identity);
Mesh_FL = Instantiate(Resources.Load("Wheel", GameObject),Vector3.zero, Quaternion.identity);
Mesh_BL = Instantiate(Resources.Load("Wheel", GameObject),Vector3.zero, Quaternion.identity);
Mesh_FR = Instantiate(Resources.Load("Wheel", GameObject),Vector3.zero, Quaternion.identity);
Mesh_BR = Instantiate(Resources.Load("Wheel", GameObject),Vector3.zero, Quaternion.identity);
//*********************************************
//....here was unnecessary code for my question
//*********************************************
Mesh_FL.transform.position = Vector3(-0.7,0.32,1.33);
//1st way
Mesh_FL.transform.localEulerAngles = Vector3(0,180,0);
//2nd way
//Mesh_FL.transform.RotateAround(Mesh_FL.transform.position,Vector3.up,180);
Mesh_FL.transform.parent = Vehicle.transform;
Mesh_FL.name="FL";
Mesh_FL.AddComponent(WheelCollider);
Mesh_FL.GetComponent(WheelCollider).forwardFriction.stiffness=fwStifness;
Mesh_FL.GetComponent(WheelCollider).sidewaysFriction.stiffness=swStifness;
Mesh_FL.GetComponent(WheelCollider).suspensionSpring.spring=spring;
Mesh_FL.GetComponent(WheelCollider).suspensionSpring.damper=damper;
//Mesh.FL.GetComponent(WheelCollider).suspensionSpring.damper=5500;
Mesh_FR.transform.position = Vector3(0.7,0.32,1.33);
Mesh_FR.transform.localEulerAngles = Vector3(0,0,0);
Mesh_FR.transform.parent = Vehicle.transform;
Mesh_FR.name="FR";
Mesh_FR.AddComponent(WheelCollider);
Mesh_FR.GetComponent(WheelCollider).forwardFriction.stiffness=fwStifness;
Mesh_FR.GetComponent(WheelCollider).sidewaysFriction.stiffness=swStifness;
Mesh_FR.GetComponent(WheelCollider).suspensionSpring.spring=spring;
Mesh_FR.GetComponent(WheelCollider).suspensionSpring.damper=damper;
}
function Update () {
Mesh_FL.transform.Rotate(10 * -6 * Time.deltaTime, 0, 0);
Mesh_FR.transform.Rotate(10 * -6 * Time.deltaTime, 0, 0);
}
On update this wheels are rotating in different ways (because their pivot directions look in different ways)
How can I rotate mesh of wheel prefab after instantiation without changing pivot direction? Thank you, and sorry for my poor English.
Answer by Julien-Lynge · Jan 30, 2013 at 06:14 PM
As I understand it, you want to rotate an object without changing its axes. That simply isn't possible - it's the same as saying "I want to rotate this without rotating it"
There are ways to achieve the same effect, however. The easiest way is to add a parent to the object. You can then rotate the object without rotating the parent. The parent axes won't move, and the child axes will.
Alternatively, you can modify the mesh itself. I wouldn't suggest that for a beginner.
That's bad idea to edit mesh - because that mean to make an internal side of a wheel more detailed. I think that it's not good to grow up trianglecount in unaccessible places for $$anonymous$$ainCamera shot in scene.
Instantiate another prefab with rotated axis in right directions - is to waste 1 more drawcoll.
So I will try to operate with Empty GO and child\parent. P.S. I'will not close this question and mark your answer as best for few days. Hope never dies. Just believe. If no one will offer me another solution - then the question will be closed.
http://answers.unity3d.com/questions/390323/how-to-make-my-new-instances-spawn-in-the-right-di.html
Here is something.. want to try..
I have the same problem, all 4 wheels are facing the same side :( any tutorial that $$anonymous$$ch this aspect?
Your answer
Follow this Question
Related Questions
[JS] transform.rotation not working 1 Answer
I need an object to rotate using a moving object as a pivot. 0 Answers
Change object position on trigger enter 3 Answers
Rotating Rigidbody properly using AddRelativeForce 0 Answers
How would I rotate an object relative to another without parenting? 2 Answers