Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more..
    Dismiss Notice
  3. Dismiss Notice

Obstacles are not Spinning properly

Discussion in 'Scripting' started by dyach3579, Apr 16, 2020.

  1. dyach3579

    dyach3579

    Joined:
    Jan 3, 2018
    Posts:
    22
    Hi I’m building a 2D mobile game where I have obstacles spawning moving upward on the Y axis. One thing I wanted to spawn is a Holder of 4 obstacles that will all spin in the same direction on the Z axis. So I have 4 obstacles as children of my spinner parent. I have a RB and my spinner script on the parent/holder itself, and the obstacle movement script on each child. My holder is centered right in the middle of the children because that's what I had to do before in order to get the 4 obstacles to spin.

    I’ve done this before moving my holders on the X axis while spinning and it worked fine, but for some reason cannot get it to work properly on the Y axis. My obstacles are moving on the Y without spinning. And I noticed when I select the Holder in the hierarchy while running the game, I can see in the Scene view that the Holder/parent stays put. It's not moving with the children. There's no errors, I just can't get it to work properly.

    Any ideas why it’s doing this and why my obstacles aren’t spinning? Or if there's a better line of code to use than what I have? Any help is appreciated, thank you.

    Here's my spinner script.

    Code (CSharp):
    1. [SerializeField] float speed = 1f;
    2.  
    3.     private void Update()
  
    4.     {
    5.           transform.Rotate(0, 0, speed * Time.deltaTime);
    6.     }
    And here's the Obstacle Movement script

    Code (CSharp):
    1.  
    2. [SerializeField] float moveSpeed;

  
    3. public int ColorID;
  
    4. public bool isEnemy = false;
  
    5. public float yThreshhold = 10f; 

  
    6.  
    7. [HideInInspector]
    public bool isDying = false;
    8.  
    9. 
  private void Awake()
  
    10.   {
      
    11.      isDying = false;
      
    12.      moveSpeed = Random.Range(3f, 3f);       
  
    13.   }

  
    14.  
    15. void Update()
  
    16. {
      
    17.     Vector3 currentpos = transform.position;
      
    18.     currentpos.y += moveSpeed * Time.deltaTime; 
        
    19.     transform.position = currentpos;
    20.  
    21. 
        if (transform.position.y > yThreshhold || !GameManager.instance.bGameStarted) 
      
    22.        {        
          
    23.              Destroy(transform.gameObject);
      
    24.        }
  
    25.    }
    26. }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    A picture or animation to explain the desired effect might be helpful as I can't decipher which one of these you're looking for:
    • A pinwheel that is moving upwards while the wheel spins
    • A plus sign that moves up while objects at the tips spin in place.
     
  3. dyach3579

    dyach3579

    Joined:
    Jan 3, 2018
    Posts:
    22
    Okay I took a screenshot of my spinner when they're all together before i run the game.

    Then i took one when i run it and the fish go flying off, not spinning real fast and going a weird direction. And the Spinner stays put.

    So Im looking for these fish to spin around the spinner (like a pinwheel) while moving up on the Y.

    Hopefully this helps. Thank you for responding.
    Screen Shot 2020-04-16 at 9.22.28 AM.png Screen Shot 2020-04-16 at 9.22.54 AM.png
     

    Attached Files:

    Last edited: Apr 16, 2020
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    It seems to me like you just need this:

    First, make sure the fish are children of the Spinner object in the object the hierarchy.
    Then just do this in Update() for the spinner (the fish don't need any script themselves):

    Code (CSharp):
    1. public float RotateSpeed = 50f;
    2. public float moveSpeed = 3f;
    3.  
    4. void Update() {
    5.   transform.position += Vector3.up * moveSpeed * Time.deltaTime;
    6.   transform.Rotate(0, 0, rotateSpeed * Time.deltaTime);
    7. }
     
  5. dyach3579

    dyach3579

    Joined:
    Jan 3, 2018
    Posts:
    22
    That seems to work beautiful. Thank you PraetorBlue! I had the ObstacleMove script on the fish themselves cuz of the color ID but i just needed to delete the movement code from that script and everything works great. Thank you again.