0
\$\begingroup\$

Making a 2d game:

I want to move character on circular surface only in one direction?

Script attached to child object is:

    public GameObject ball;
    float speed=2f;

    // Start is called before the first frame update
    void Start()
    {
        GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        Left();
    }

    void Left()
    {
        if(Input.GetKey(KeyCode.LeftArrow))
        {
            ball.GetComponent<Rigidbody2D>().AddForce(Vector3.forward * speed);
            //transform.Rotate(Vector2.left);
        }
    }


Note: character will move on a circular surface in only one direction...

Image reference:

enter image description here

\$\endgroup\$
1
\$\begingroup\$

you need polar coordiates. you need to find the position in polar coordintaes(radius,teta) to unity basic cartesian coordinates(x,y) you can move the object by changing the theta angle this is my example code:

private void FixedUpdate()
    {
        foreach (var item in PolarElements)
        {

            float x = item.radius * Mathf.Cos(item.myAngle);
            float y = item.radius * Mathf.Sin(item.myAngle);

            item.position = new Vector3(transform.position.x+  x,transform.position.y+y,transform.position.z);
            item.myAngle += item.speed;
        }
    }
\$\endgroup\$
  • \$\begingroup\$ what is PolarElements it is vector2 / Ienumarator?? what is PolarElements,Which value store in polar 90degree or 180 degree?? that is vector 2?? \$\endgroup\$ – Tim Sep 18 at 9:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.