Search Unity

Help With Vector Rotation

Discussion in 'Scripting' started by Catalystix, May 28, 2020.

  1. Catalystix

    Catalystix

    Joined:
    May 13, 2020
    Posts:
    13
    Hello, i've been using unity for a couple weeks and i've been challenging myself quite a bit and . To get to the point, here is a project i made in Scratch that i'm trying to recreate using clones: https://scratch.mit.edu/projects/399333634/. Basically, it finds the distance to the mouse in the world and creates a number of clones needed to reach the mouse. My problem is: the first clone works fine, spawning at spawnPoint (p = 0) and orbits the object in the center, both pointing towards the mouse. The others however will orbit around the first clone at the correct distance(like a clock hand,) but rotate much faster than the angle of the mouse changes, which is what confuses me, being that their positions are all based on the mouse angle. even at an angle of 90, where cos(90) should equal 0, each clone but the first points NW. I'd appreciate any help. ps: i have another script that removes the clones after they are rendered to continuously "update" the screen. i don't know if there's a way i can do this without clones.
    Code (CSharp):
    1.  
    2. using JetBrains.Annotations;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. public class spawning : MonoBehaviour
    8. {   public Camera cam;
    9.     Vector2 mousePos;
    10.     public float mouseDis;
    11.     public GameObject boxPrefab;
    12.     public Transform spawnPoint;
    13.     public Rigidbody2D rb;
    14.     public float  size = 0.5f;
    15.     public float angle = 0f;
    16.  
    17.  
    18.     void FixedUpdate()
    19.     {
    20.         mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
    21.         mouseDis = mousePos.magnitude;
    22.         Vector2 dir = mousePos; //-rb.position;
    23.         angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
    24.         rb.rotation = angle;
    25.  
    26.         int p = 0;
    27.  
    28.         while (p < Mathf.Floor(mouseDis / size))
    29.         {
    30.             Vector2 pos = new Vector2
    31.                 (spawnPoint.position.x + Mathf.Cos(angle) * p * size,
    32.                  spawnPoint.position.y + Mathf.Sin(angle) * p * size);
    33.    
    34.             GameObject box = Instantiate(boxPrefab, pos, spawnPoint.rotation);
    35.             p++;
    36.         }
    37.   }
    38. }




    The image shows both the center object and the first clone pointing NE (30°) while the others are pointing SE.
     
    Last edited: May 28, 2020
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    Traditionally you keep them all in a list and since instantiating and destroying are fairly "heavy" operations, you just add more when needed, and take away only when not needed.

    Alternately you can just disable ones you're not using, never destroying them. If you need more, make more, keep them all in a list, then each frame:

    - check if you have enough in the list, if not add more
    - reuse them from the list, reassigning their positions and rotations.
    - turn off any you didn't use this frame. (SetActive(false))

    As far as the actual rotation problem, unfortunately your images didn't come through. The code appears right however: you're using the same rotation on all of the objects...
     
    faqeerhasnain likes this.
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Your images are broken but a couple of comments:

    1. Why do this in FixedUpdate? That method is usually reserved for physics updates. This code probably belongs in Update().
    2. You are instantiating all of these objects in FixedUpdate() which usually runs ~50 times per second. That's a lot of objects! Are you destroying them later? Do they have their own script which causes them to move?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    Here's another thought: if there are colliders on your prefab, and physics Rigidbodies on them, they're probably bumping each other upon spawning and knocking each other around a bit.
     
    PraetorBlue likes this.
  5. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    When you say "orbit" do you mean that they move over time? I don't see anything in the posted code that would do that.

    Or do you mean purely as a function of the mouse position?
     
  6. Catalystix

    Catalystix

    Joined:
    May 13, 2020
    Posts:
    13
    They rotate around the center like the hand of a clock, pointing towards the mouse. im not sure why none of the pics worked. tried sharing the images on google drive and imgur and pasting the urls. thanks for the reply
     
  7. Catalystix

    Catalystix

    Joined:
    May 13, 2020
    Posts:
    13
    I fixed the image (i think). i was experimenting with the update timing. the clones are deleted in LastUpdate.
     
  8. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Try doing like Kurt said - create them once, keep them in a list, and enable/disable them as needed. Simplest thing would be to instantiate maybe ~10 or 20 in Start() and disable them all initially. Then just alter the positions/rotations of the existing boxes as needed.
     
    faqeerhasnain likes this.
  9. Catalystix

    Catalystix

    Joined:
    May 13, 2020
    Posts:
    13
    Thanks for the replies. I got an image to appear. They have rigid bodies but no coliders. And I thought of doing the clones that way but I wanted to use as few lines as possible. They're just rotating around the spawn point seperatly from the center object. My original thought was that multiplying by P was somehow affecting the angle. Makes my brain hurt haha