# Move unit forward with changing it's angle

Status
Not open for further replies.

#### kawas11

Level 5
I got a qauestion how to makes unit move forward and changing it's angle using radians.

http://imgur.com/6x4U8oX

#### Chaosy

Tutorial Reviewer
Level 40
I am not really good with math, but I think using SIN and COS make that kind of pattern.

It's possible to do it without it as well but I imagine that it gets complicated then

#### kawas11

Level 5
moving forward is rly not problem xD

it SetUnitPosition (unit, unitx + speed * Cos(angle), unity + speed * Sin(angle)

the onlt problem i got is to change angle during unit move to make it move like on that image

#### Chaosy

Tutorial Reviewer
Level 40
JASS:
``native SetUnitFacing takes unit whichUnit, real facingAngle returns nothing``

or

• Unit - Make Your unit face angle facing over x seconds
to get the angle itself it's AngleBetweenPoints in jass I think.

#### Dr Super Good

Spell Reviewer
Level 63
I got a qauestion how to makes unit move forward and changing it's angle using radians.
All of the Cos, Sin and Tan as well as their opposite functions use radians. Only unit facing is in degrees. As you are aware you can convert degrees to radians and visa versa using a constant.

#### kawas11

Level 5
Ok, i was trying to do it and i am close but it's not that i am trying t do.

At this moment missle is truning back and moving as i wanted but backward.

JASS:
``````scope Missle
globals
//Ability rawcode
private constant integer        SPELL_CODE      = 'A003'
//Dummy rawcode
private constant integer        DUMMY_CODE      = 'h000'
//Max ange increase/decrease
private constant real           MAX_ANG         = 10
//Angle increase ammout every cd
private constant real           ANG_INC         = 5
//Angle increase CD
private constant real           ANG_CD          = 0.30
endglobals

globals
private     real        array       M_Distance
private     real        array       M_Speed
endglobals

private function Missle_Setup takes nothing returns nothing

set M_Distance[1] = 900
set M_Distance[2] = 900
set M_Distance[3] = 900

set M_Speed[1] = 600
set M_Speed[2] = 600
set M_Speed[3] = 600

endfunction

globals
private constant real INTERVAL  = 0.031250
endglobals

private struct missle

unit caster
unit dummy
real distance
real speed
real angle
real angle2
real ang_cd
real ang_ccd
player owner

private static thistype tmpDat

private static method Missle_Move takes nothing returns nothing
local timer TIMER = GetExpiredTimer()
local thistype dat = GetTimerData(TIMER)

local real unitx
local real unity

local integer i = 0
local boolean way

set unitx = GetUnitX(dat.dummy) + dat.speed * Cos(dat.angle)
set unity = GetUnitY(dat.dummy) + dat.speed * Sin(dat.angle)

if dat.ang_cd > dat.ang_ccd then
set dat.ang_ccd = dat.ang_ccd + INTERVAL
else
if dat.angle > dat.angle2 + MAX_ANG then
set dat.angle = dat.angle - ANG_INC
call BJDebugMsg("1")
else
set dat.angle = dat.angle + ANG_INC
call BJDebugMsg("2")
endif
set dat.ang_ccd = dat.ang_ccd - dat.ang_cd
endif

call SetUnitPosition(dat.dummy, unitx, unity)

if dat.distance <= 0 then
call RemoveUnit(dat.dummy)
set dat.dummy = null
set dat.caster = null
call ReleaseTimer(TIMER)
call dat.destroy()
endif

set TIMER = null
endmethod

private static method Missle_Run takes nothing returns nothing
local thistype dat
local timer TIMER = NewTimer()

local integer level

local real casterx
local real castery

local real targetx
local real targety

set dat = thistype.allocate()

set dat.caster = GetTriggerUnit()
set dat.owner = GetOwningPlayer(dat.caster)
set level = GetUnitAbilityLevel(dat.caster, SPELL_CODE)

set casterx = GetUnitX(dat.caster)
set castery = GetUnitY(dat.caster)

set targetx = GetSpellTargetX()
set targety = GetSpellTargetY()

set dat.distance = M_Distance[level]
set dat.speed = M_Speed[level] * INTERVAL

set dat.angle = Atan2(targety - castery, targetx - casterx)
set dat.angle2 = dat.angle

set dat.ang_cd = ANG_CD
set dat.ang_ccd = ANG_CD

set dat.dummy = CreateUnit(dat.owner, DUMMY_CODE, casterx, castery, GetUnitFacing(dat.caster) + dat.angle)

call SetTimerData(TIMER, dat)

call TimerStart(TIMER, INTERVAL, true, function thistype.Missle_Move)

set TIMER = null
endmethod

private static method onInit takes nothing returns nothing
call RegisterSpellEffectEvent(SPELL_CODE,function thistype.Missle_Run)

call Missle_Setup()
endmethod
endstruct
endscope``````

#### Arhowk

Level 19
JASS:
``````set adjustedAngle = angle + Sin(TimerGetElapsed(t) * FREQUENCY) * WAVELENGTH
call SetUnitX(GetUnitX() + speed * Cos(adjustedAngle))
call SetUnitY(GetUnitY() + speed * Sin(adjustedAngle))``````

#### kawas11

Level 5
With your help i made it to move left or move right every some time and it works as i wanted big thanks

JASS:
``````if dat.ang_cd > dat.ang_ccd then
set adjustedAngle = dat.angle + Sin(TimerGetElapsed(TIMER) * 2.0) * 10.0
call BJDebugMsg("first")
set dat.ang_ccd = dat.ang_ccd + INTERVAL
else
if dat.ang_cd2 > dat.ang_ccd2 then
set adjustedAngle = dat.angle + Sin(TimerGetElapsed(TIMER) * 2.0) * - 10.0
call BJDebugMsg("second")
set dat.ang_ccd2 = dat.ang_ccd2 + INTERVAL
else
set dat.ang_ccd = dat.ang_ccd - dat.ang_cd
set dat.ang_ccd2 = dat.ang_ccd2 - dat.ang_cd2
endif
endif``````

Status
Not open for further replies.

Replies
2
Views
810
Replies
11
Views
772
Replies
3
Views
688
Replies
14
Views
587
Replies
2
Views
1K