Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Sectah · May 26, 2016 at 12:53 PM · rotationmovementinstantiaterotatelocalscale

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;
     }
 }

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Zwusel · May 26, 2016 at 01:52 PM 0
Share

that is kind of strange. Is the cube a child of some other object maybe?

avatar image Sectah · May 26, 2016 at 02:16 PM 0
Share

Nope, just a regular 1x1x1 cube as a prefab

avatar image Zwusel · May 26, 2016 at 03:02 PM 0
Share

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.

avatar image Sectah · May 26, 2016 at 03:17 PM 0
Share

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.

avatar image Glurth · May 26, 2016 at 03:28 PM 0
Share

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).

avatar image Sectah Glurth · May 26, 2016 at 03:44 PM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Why the player is not rotating and moving at the same time ? 0 Answers

How to instantiate on custom rotation? 1 Answer

Rotating Player 0 Answers

Saving Rotation of Sprite 1 Answer

How can I rotate my player to look at the direction my Joystick is pointing to? (Top-Down 2D) 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges