Hello everyone, I’m trying to make a tank control system from birdseye view, and I have some problems. The tank model I’m using is free from the internet, so I can’t edit it, I mean I don’t know how to deal with 3d model. Anyways, my problem is, when I rotate the model, it rotates to the angles I want but when it does that it changes it’s own position, it like it’s not rotating from the center, it’s rotating from another point. here is a pic about it :
125125
as you can see here, the tank changes it’s position according to the position too, I want it to rotate from the center, is there anyway to do that with code or by the editor ?
Here is my rotating code:
function Update () {
print (transform.eulerAngles.y);
if (Input.GetKey("w")){
transform.eulerAngles = Vector3(0, 0, 0);
transform.Translate(Time.deltaTime*2.5,0,0,Space.World);
}
if (Input.GetKey("a")){
transform.eulerAngles = Vector3(0, -90, 0);
transform.Translate(0,0,Time.deltaTime*2.5,Space.World);
}
if (Input.GetKey("d")){
transform.eulerAngles = Vector3(0, 90, 0);
transform.Translate(0,0,Time.deltaTime*-2.5,Space.World);
}
if (Input.GetKey("s")){
transform.eulerAngles = Vector3(0, 180, 0);
transform.Translate(Time.deltaTime*-2.5,0,0,Space.World);
}
}
The simplest solution without changing the model is to make the model a child gameobject of your tank gameobject (the one that has the script), and position the child gameobject of the model so the rotation point you want is at the origin of the parent tank gameobject with the script. (I actually use that setup as a rule for all scripted objects in Unity even when I don’t have origin problems with the model)
i know this has been answered but i thought id put it out there - i had this problem with a 3d model myself and if you select the model you need to pivot about the center, go into ‘gameobject’ and click on ‘center on children’. simple as, took me a long time
Yeah, I had similar issue. What I did was create an empty object to act as the “pivot point” and position the model within it so that the pivot point is correct. Your script would then control the pivot point (parent). It works for but I also wonder if there is a better, more direct way to specify the pivot center… ?
The trick is to have the object you want to move put under an additional parent, set the position of the parent the rotation center you want, then set the localRotation of the parent instead of the child. (important! if not using localRotation it will only rotate in world coordinates instead of local)
The example was having the hierarchy of Clock > Hour > cube, where Clock means the clock face, Hour is the additional parent that rotates, and the Cube is the physical object.