How to make a ui move as its direction

  1. What do you want to achieve?
    I am making some sort of 2d game and I am working on the projectiles and I want the projectile to lunge at the player by its direction

  2. What is the issue?
    I tried one of the codes I found on the devforum and it doesn’t work; it just moves off the screen and not in the direction.

  3. What solutions have you tried so far?
    I tried using this code from the devforum:

-- credit: TOP_Crundee123
local direction = Vector2.new(math.cos(Bullet.Rotation), math.sin(Bullet.Rotation))
			Bullet.Position = UDim2.fromOffset( -- Offsets the gui
				Bullet.AbsolutePosition.X + direction.X * speed,
				Bullet.AbsolutePosition.Y + direction.Y * speed
			)

but when I run it, the ui moves off the screen and not towards the dirrection. And this code by Apicphis:

Bullet:TweenPosition(UDim2.new(Bullet.Position.X.Scale + math.tan(math.rad(Bullet.Rotation))*(Bullet.Position.Y.Scale-newY),0,newY,0))

but, to me, this is really inconsistent since I am working on a 1.5 aspect ratio.
Any help will be appreciated :).

2 Likes

I had a similar script with the same issue, I solved it by putting math.rad in the direction variable.
Example:

local direction = Vector2.new(math.cos(math.rad(UI.Rotation)), math.sin(math.rad(UI.Rotation)))