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): using JetBrains.Annotations; using System.Collections; using System.Collections.Generic; using UnityEngine; public class spawning : MonoBehaviour { public Camera cam; Vector2 mousePos; public float mouseDis; public GameObject boxPrefab; public Transform spawnPoint; public Rigidbody2D rb; public float size = 0.5f; public float angle = 0f; void FixedUpdate() { mousePos = cam.ScreenToWorldPoint(Input.mousePosition); mouseDis = mousePos.magnitude; Vector2 dir = mousePos; //-rb.position; angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg; rb.rotation = angle; int p = 0; while (p < Mathf.Floor(mouseDis / size)) { Vector2 pos = new Vector2 (spawnPoint.position.x + Mathf.Cos(angle) * p * size, spawnPoint.position.y + Mathf.Sin(angle) * p * size); GameObject box = Instantiate(boxPrefab, pos, spawnPoint.rotation); p++; } } } The image shows both the center object and the first clone pointing NE (30°) while the others are pointing SE.
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...
Your images are broken but a couple of comments: Why do this in FixedUpdate? That method is usually reserved for physics updates. This code probably belongs in Update(). 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?
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.
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?
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
I fixed the image (i think). i was experimenting with the update timing. the clones are deleted in LastUpdate.
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.
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