- Home /
Instantiated object not rotating around the center
I'm instantiating an object that has localScale and then two scripts attached.
It starts as a 1x1x1 cube, is then adjusted with localScale, instantiated with the two scripts; one for movement, one for rotation.
The issue I'm having is that the new longer cubed object does not rotate around the center. (level2 function is where it's at).
How do I get the cuboid to rotate around the center while it is moving rather than stuck in an infinite circle around the edge of the object.
Thanks.
enemyRotation.cs
using UnityEngine;
using System.Collections;
public class enemyRotation : MonoBehaviour {
public float speed = 10f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.Rotate (Vector3.down, speed * Time.deltaTime);
}
}
enemyMovement.cs
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
public class enemyMovement : MonoBehaviour {
public float speed = 1f;
float deathTimer = 21f;
// Use this for initialization
void Start () {
Destroy (gameObject, deathTimer);
}
// Update is called once per frame
void Update () {
transform.Translate (speed * Time.deltaTime, 0.0f, 0.0f);
}
void OnCollisionEnter (Collision col){
if (col.collider.tag == "Player") {
if (SceneManager.GetActiveScene ().buildIndex == 1) {
SceneManager.LoadScene ("1 - levels");
}
if (SceneManager.GetActiveScene ().buildIndex == 2) {
SceneManager.LoadScene ("2 - endless");
}
}
}
}
level1GM.cs
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class level1GM : MonoBehaviour {
public GameObject enemy;
public GameObject player;
public Text levelDisplay;
Vector3 size3, size4, size7, size9, size10, size14, size17, size27;
private enemyMovement enemyMovementScript;
private enemyRotation enemyRotationScript;
void Start () {
enemyMovementScript = enemy.GetComponent<enemyMovement> ();
enemyRotationScript = enemy.GetComponent<enemyRotation> ();
enemyMovementScript.enabled = true;
enemyRotationScript.enabled = false;
StartCoroutine (levelLoad ());
}
IEnumerator levelLoad() {
createEnemySizes ();
//level1 ();
yield return new WaitForSeconds (0f);
level2 ();
}
void createEnemySizes (){
size3 = new Vector3 (0.0f, 0.0f, 2.0f);
size4 = new Vector3 (0.0f, 0.0f, 3.0f);
size7 = new Vector3 (0.0f, 0.0f, 6.0f);
size9 = new Vector3 (0.0f, 0.0f, 8.0f);
size10 = new Vector3 (0.0f, 0.0f, 9.0f);
size14 = new Vector3 (0.0f, 0.0f, 13.0f);
size17 = new Vector3 (0.0f, 0.0f, 16.0f);
size27 = new Vector3 (0.0f, 0.0f, 26.0f);
}
void level1(){
levelDisplay.text = "LEVEL 1";
enemyMovementScript.speed = 6f;
enemy.transform.localScale += size9;
Instantiate (enemy, new Vector3(-20.0f, 0.0f, 5.5f), Quaternion.identity);
enemy.transform.localScale -= size9;
enemy.transform.localScale += size9;
Instantiate (enemy, new Vector3(20.0f, 0.0f, -5.5f), Quaternion.Euler(0.0f, 180.0f, 0.0f));
enemy.transform.localScale -= size9;
enemyMovementScript.speed = 4f;
Instantiate(enemy, new Vector3(9.5f, 0.0f, 25.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
Instantiate(enemy, new Vector3(2.5f, 0.0f, 25.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
Instantiate(enemy, new Vector3(-1.5f, 0.0f, 25.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
Instantiate(enemy, new Vector3(-3.5f, 0.0f, 25.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
Instantiate(enemy, new Vector3(-5.5f, 0.0f, 25.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
Instantiate(enemy, new Vector3(-7.5f, 0.0f, 25.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
Instantiate(enemy, new Vector3(-9.5f, 0.0f, 25.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
enemyMovementScript.speed = 4.2f;
Instantiate(enemy, new Vector3(-9.5f, 0.0f, 32.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
Instantiate(enemy, new Vector3(-2.5f, 0.0f, 32.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
Instantiate(enemy, new Vector3(1.5f, 0.0f, 32.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
Instantiate(enemy, new Vector3(3.5f, 0.0f, 32.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
Instantiate(enemy, new Vector3(5.5f, 0.0f, 32.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
Instantiate(enemy, new Vector3(7.5f, 0.0f, 32.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
Instantiate(enemy, new Vector3(9.5f, 0.0f, 32.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
enemyMovementScript.speed = 7f;
enemy.transform.localScale += size17;
Instantiate (enemy, new Vector3(-1.5f, 0.0f, 37.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
enemy.transform.localScale -= size17;
enemyMovementScript.speed = 6f;
enemy.transform.localScale += size7;
Instantiate (enemy, new Vector3(-80.0f, 0.0f, 6.5f), Quaternion.identity);
enemy.transform.localScale -= size7;
enemy.transform.localScale += size10;
Instantiate (enemy, new Vector3(-80.0f, 0.0f, -5.0f), Quaternion.identity);
enemy.transform.localScale -= size10;
enemy.transform.localScale += size14;
Instantiate (enemy, new Vector3(-86.0f, 0.0f, -3.0f), Quaternion.identity);
enemy.transform.localScale -= size14;
enemy.transform.localScale += size3;
Instantiate (enemy, new Vector3(-86.0f, 0.0f, 8.5f), Quaternion.identity);
enemy.transform.localScale -= size3;
enemy.transform.localScale += size7;
Instantiate (enemy, new Vector3(-93.0f, 0.0f, 6.5f), Quaternion.identity);
enemy.transform.localScale -= size7;
enemy.transform.localScale += size10;
Instantiate (enemy, new Vector3(-93.0f, 0.0f, -5.0f), Quaternion.identity);
enemy.transform.localScale -= size10;
}
void level2 (){
levelDisplay.text = "LEVEL 2";
player.transform.position = new Vector3 (-6.0f, 4.0f, -6.0f);
enemyMovementScript.enabled = false;
enemyRotationScript.enabled = true;
enemyRotationScript.speed = 50f;
enemy.transform.localScale += size27;
Instantiate (enemy, new Vector3(0.0f, 0.0f, 0.0f), Quaternion.identity);
enemy.transform.localScale -= size27;
enemyMovementScript.enabled = true;
enemyMovementScript.speed = 3f;
enemyRotationScript.enabled = true;
enemyRotationScript.speed = 80f;
enemy.transform.localScale += size4;
Instantiate (enemy, new Vector3(12.0f, 0.0f, -8.0f), Quaternion.Euler(0.0f, 180.0f, 0.0f));
Instantiate (enemy, new Vector3(12.0f, 0.0f, 0.0f), Quaternion.Euler(0.0f, 180.0f, 0.0f));
Instantiate (enemy, new Vector3(12.0f, 0.0f, 8.0f), Quaternion.Euler(0.0f, 180.0f, 0.0f));
Instantiate (enemy, new Vector3(16.0f, 0.0f, -4.0f), Quaternion.Euler(0.0f, 180.0f, 0.0f));
Instantiate (enemy, new Vector3(16.0f, 0.0f, 4.0f), Quaternion.Euler(0.0f, 180.0f, 0.0f));
enemy.transform.localScale -= size4;
}
}
that is kind of strange. Is the cube a child of some other object maybe?
do they rotate around the center when you put the prefab in the scene and rotate it there? Like manually withe the rotate gizmo ;) As far as i know the pivot should be centered. To be extra sure look at the buttons at the top of the unity screen if you have selected an object. There is pivot and centered i think.
The rotation by itself is fine but as soon as it has the constant movement as well, the whole rotation thing stops working around the center.
I see you are rotation about Vector3.down. Note this is a world space value, and will point "down" regardless of the object's orientation. If you want it to rotate about the OBJECT's "down" direction, use -transform.up
Also, though I never use it, I've seen people report having trouble with transform.translate
. When they switch to transform.position+=
it fixes things for them (this is what I normally use too).
I changed to transform.position and it seems to have fixed it. I would just need 4 scripts ins$$anonymous$$d of 1 for all the different directions the 'enemies' can come from. Thank you.