I’ve been trying to work an enemy that moves in a circular motion for my RPG game, but for some reason, whenever I press play the GameObject instantly goes hundreds of units in the X and Y coordinates. It also move back to -1 on the Z axis. Here’s the script to my enemy’s movement:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class EnemyScript : MonoBehaviour { private Rigidbody2D rb; [SerializeField] float rotationRadius = 2f, angularSpeed = 2f; float posX, posY, angle = 0f; // Start is called before the first frame update void Start() { // Gets the RigidBody2D component rb = GetComponent<Rigidbody2D>(); } // Update is called once per frame void Update() { Movement_1(); } void Movement_1() { posX = rb.position.x + Mathf.Cos(angle) + rotationRadius; posY = rb.position.y + Mathf.Sin(angle) + rotationRadius; transform.position = new Vector2(posX, posY); angle = angle + Time.deltaTime * angularSpeed; if (angle >= 360f) { angle = 0f; } } }