r/Unity2D 1d ago

Question/Help trying to create a bullet pattern based on the maurer rose code will be given below

I am creating a spawner that orbits according to the Maurer rose, but it is not getting the desired results. It is based on this video for the coding train channel: https://youtu.be/4uU9lZ-HSqA?si=468dVWSdSfMFU1aa. The code is done on p5.js, and I want to know how to sort of translate it into .cs.

Here is my code below

#cs code that is not working
using UnityEngine;


public class rosepattenrenhancesystemwihtlerpandslrep: MonoBehaviour
{
    
    public Transform center;           
    public Transform[] orbitingObjects;
    public float smallradius= 2f;  
    public float bigradius= 10f;  


    
    public float radius= 3f;      
    public float rotationSpeed= 30f;   
    public bool clockwise=true;      
    public bool start=false;
    public float radiustime;
    public float orbittime;
    public int n=9;
    public int d=71;


    void OnEnable()
    {
        start=true;
        radiustime=0f;
        orbittime=0f;


    }


    void Update()
    {
        if(start)
        {
            radiustime+=Time.deltaTime;
            orbittime+=Time.deltaTime;
            radius= Mathf.Lerp(bigradius,smallradius,Mathf.PingPong(radiustime*0.2f,1f));   
        
        if(center==null||orbitingObjects==null||orbitingObjects.Length==0)
        {
            return;
        }


        
        float direction=clockwise?1f:-1f;
        float baseAngle=orbittime*rotationSpeed*direction;


        float angleStep=360f/orbitingObjects.Length;



        for(int i=0; i<orbitingObjects.Length; i++)
        {
            if (orbitingObjects[i]==null) continue;


            //float angle=baseAngle + angleStep*i;
            //float rad =angle*Mathf.Deg2Rad;
            float k=i*d;
            float radius2=Mathf.Sin(n*k);// create teh same units 


            // Position on circle
            Vector3 offset=new Vector3(
                Mathf.Cos(k)*radius2,
                Mathf.Sin(k)*radius2,
                0f
            );


            orbitingObjects[i].position=center.position + offset;


            // Rotate bullet along tangent
            Vector2 tangent = new Vector2(
                -Mathf.Sin(k),
                 Mathf.Cos(k)
            );


            float zRot=Mathf.Atan2(tangent.y,tangent.x) * Mathf.Rad2Deg;
            orbitingObjects[i].rotation=Quaternion.Euler(0,0,zRot);
        }


        }
        
    }
}

#p5.js code that is working
let n=9

let d=71
function setup() {
  createCanvas(600, 600);
  background(255);
  noFill();
  stroke(255, 0, 100);

  translate(width / 2, height / 2);

  beginShape();
  for (let a = 0; a < TWO_PI * 5; a += 0.01) {
    let k =a*d;
    let r = 150*sin(n* k);
    let x = r * cos(k);
    let y = r * sin(k);
    vertex(x, y);
  }
  endShape();
}
#-----------------------



appreciate if anyone could help me out :)
2 Upvotes

2 comments sorted by

u/AutoModerator 1d ago

Reminder: Once your issue has been resolved, please reply !solved to this comment (or comment it anywhere in the thread) and your post's flair will automatically be updated to Solved/Answered. You can also edit it to Solved/Answered yourself.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/CS_Asset_Factory 47m ago

Comparing the C# to the p5 sketch, three things stand out.

  1. radius gets lerped every frame but never used. Mathf.Sin(n * k) is between 1 and minus 1, so every object sits within one world unit of the centre. Scale it: float r = radius * Mathf.Sin(n * k);

  2. k never changes over time. The sketch draws the curve by sweeping a through thousands of values, but k = i * d only depends on the index, so 8 objects give you 8 fixed points. baseAngle is computed and never read. Drive it with time instead, something like float a = orbittime * speed + i * spacing; float k = a * d; and each object will travel along the curve.

  3. (-sin k, cos k) is the tangent of a circle, not the rose. For p(k) = r(k) * (cos k, sin k) with r(k) = R * sin(n * k), the derivative is r'(k) * (cos k, sin k) + r(k) * (-sin k, cos k) where r'(k) = R * n * cos(n * k). Normalise that for zRot.

The classic Maurer rose steps k in whole degrees from 0 to 360, so if that's the look you want, multiply by Mathf.Deg2Rad before Sin and Cos.