| |
|
»
»
|
|
| |
Re: Brick tweening and coroutine help...
|
|
|
blockoo
|
  |
| Joined: 08 Nov 2007 |
| Total Posts: 17202 |
|
|
| 07 Dec 2011 07:23 PM |
This script is supposed to tween a brick's position and angles to the desired values...however, it acts strange. It will move along the X axis, then the Y axis, then the Z axis instead of moving simultaneously. Same thing happens with the angles. Why?
function tweenPosition(startP, endP, part) if (startP.x < endP.x) then xFact = 0.1 else xFact = -0.1 end if (startP.y < endP.y) then yFact = 0.1 else xFact = -0.1 end if (startP.z < endP.z) then zFact = 0.1 else zFact = -0.1 end coroutine.resume(coroutine.create(function() for i = startP.x, endP.x, xFact do wait() local ax, ay, az = part.CFrame:toEulerAnglesXYZ() part.CFrame = CFrame.new(i, part.Position.y, part.Position.z) * CFrame.Angles(ax, ay, az) end end) ) coroutine.resume(coroutine.create(function() for i = startP.y, endP.y, yFact do wait() local ax, ay, az = part.CFrame:toEulerAnglesXYZ() part.CFrame = CFrame.new(part.Position.x, i, part.Position.z) * CFrame.Angles(ax, ay, az) end end) ) coroutine.resume(coroutine.create(function() for i = startP.z, endP.z, zFact do wait() local ax, ay, az = part.CFrame:toEulerAnglesXYZ() part.CFrame = CFrame.new(part.Position.x, part.Position.y, i) * CFrame.Angles(ax, ay, az) end end) ) end
function tweenAngles(startA, endA, part) if (startA.x < endA.x) then xaFact = 0.1 else xaFact = -0.1 end if (startA.y < endA.y) then yaFact = 0.1 else yaFact = -0.1 end if (startA.z < endA.z) then zaFact = 0.1 else zaFact = -0.1 end coroutine.resume(coroutine.create(function() for i = startA.x, endA.x, xaFact do wait() local ax, ay, az = part.CFrame:toEulerAnglesXYZ() part.CFrame = CFrame.new(part.Position.x, part.Position.y, part.Position.z) * CFrame.Angles(math.rad(i), ay, az) end end) ) coroutine.resume(coroutine.create(function() for i = startA.y, endA.y, yaFact do wait() local ax, ay, az = part.CFrame:toEulerAnglesXYZ() part.CFrame = CFrame.new(part.Position.x, part.Position.y, part.Position.z) * CFrame.Angles(ax, math.rad(i), az) end end) ) coroutine.resume(coroutine.create(function() for i = startA.z, endA.z, zaFact do wait() local ax, ay, az = part.CFrame:toEulerAnglesXYZ() part.CFrame = CFrame.new(part.Position.x, part.Position.y, part.Position.z) * CFrame.Angles(ax, ay, math.rad(i)) end end) ) end
-------------------------------------------------------- part = Workspace.Part startPos = part.Position endPos = Vector3.new(10, 100, 50) startAng = Vector3.new(part.CFrame:toEulerAnglesXYZ()) endAng = Vector3.new(10, 10, 10) startAng = Vector3.new(math.deg(startAng.x), math.deg(startAng.y), math.deg(startAng.z)) coroutine.resume(coroutine.create(function() tweenPosition(startPos, endPos, part) end) ) coroutine.resume(coroutine.create(function() tweenAngles(startAng, endAng, part) end) ) |
|
|
| Report Abuse |
|
|
blockoo
|
  |
| Joined: 08 Nov 2007 |
| Total Posts: 17202 |
|
|
| 07 Dec 2011 07:25 PM |
| Nevermind, it's a timing issue... |
|
|
| Report Abuse |
|
|
Spectrumw
|
  |
| Joined: 04 Aug 2009 |
| Total Posts: 13510 |
|
|
| 07 Dec 2011 07:29 PM |
Y U NO use ternary operators? D: Using them you would be able to reduce this; if (startP.x < endP.x) then xFact = 0.1 else xFact = -0.1 end if (startP.y < endP.y) then yFact = 0.1 else xFact = -0.1 -- By the way, you made a mistake here n_n end if (startP.z < endP.z) then zFact = 0.1 else zFact = -0.1 end
To this; xFact = (startP.x < endP.x) and 0.1 or -0.1 yFact = (startP.y < endP.y) and 0.1 or -0.1 zFact = (startP.z < endP.z) and 0.1 or -0.1 |
|
|
| Report Abuse |
|
|
blockoo
|
  |
| Joined: 08 Nov 2007 |
| Total Posts: 17202 |
|
|
| 07 Dec 2011 07:35 PM |
I fixed that, but now this part won't even work because I fixed the timing issues. I'm getting no output:
part = Workspace.Part startPos = part.Position endPos = Vector3.new(10, 100, 50) startAng = Vector3.new(part.CFrame:toEulerAnglesXYZ()) endAng = Vector3.new(10, 10, 10) startAng = Vector3.new(math.deg(startAng.x), math.deg(startAng.y), math.deg(startAng.z)) coroutine.resume(coroutine.create(function() tweenPosition(startPos, endPos, 5, part) end) ) coroutine.resume(coroutine.create(function() tweenAngles(startAng, endAng, 5, part) end) ) |
|
|
| Report Abuse |
|
|
blockoo
|
  |
| Joined: 08 Nov 2007 |
| Total Posts: 17202 |
|
|
| 07 Dec 2011 07:36 PM |
Whole thing:
function tweenPosition(startP, endP, time, part) coroutine.resume(coroutine.create(function() xFact = (startP.x - endP.x) / time yFact = (startP.y - endP.y) / time zFact = (startP.z - endP.z) / time for i = startP.x, endP.x, xFact do wait() local ax, ay, az = part.CFrame:toEulerAnglesXYZ() part.CFrame = CFrame.new(i, part.Position.y, part.Position.z) * CFrame.Angles(ax, ay, az) end end) ) coroutine.resume(coroutine.create(function() for i = startP.y, endP.y, yFact do wait() local ax, ay, az = part.CFrame:toEulerAnglesXYZ() part.CFrame = CFrame.new(part.Position.x, i, part.Position.z) * CFrame.Angles(ax, ay, az) end end) ) coroutine.resume(coroutine.create(function() for i = startP.z, endP.z, zFact do wait() local ax, ay, az = part.CFrame:toEulerAnglesXYZ() part.CFrame = CFrame.new(part.Position.x, part.Position.y, i) * CFrame.Angles(ax, ay, az) end end) ) end
function tweenAngles(startA, endA, time, part) coroutine.resume(coroutine.create(function() xaFact = (startA.x - endA.x) / time yaFact = (startA.y - endA.y) / time zaFact = (startA.z - endA.z) / time for i = startA.x, endA.x, xaFact do wait() local ax, ay, az = part.CFrame:toEulerAnglesXYZ() part.CFrame = CFrame.new(part.Position.x, part.Position.y, part.Position.z) * CFrame.Angles(math.rad(i), ay, az) end end) ) coroutine.resume(coroutine.create(function() for i = startA.y, endA.y, yaFact do wait() local ax, ay, az = part.CFrame:toEulerAnglesXYZ() part.CFrame = CFrame.new(part.Position.x, part.Position.y, part.Position.z) * CFrame.Angles(ax, math.rad(i), az) end end) ) coroutine.resume(coroutine.create(function() for i = startA.z, endA.z, zaFact do wait() local ax, ay, az = part.CFrame:toEulerAnglesXYZ() part.CFrame = CFrame.new(part.Position.x, part.Position.y, part.Position.z) * CFrame.Angles(ax, ay, math.rad(i)) end end) ) end
-------------------------------------------------------- part = Workspace.Part startPos = part.Position endPos = Vector3.new(10, 100, 50) startAng = Vector3.new(part.CFrame:toEulerAnglesXYZ()) endAng = Vector3.new(10, 10, 10) startAng = Vector3.new(math.deg(startAng.x), math.deg(startAng.y), math.deg(startAng.z)) coroutine.resume(coroutine.create(function() tweenPosition(startPos, endPos, 5, part) end) ) coroutine.resume(coroutine.create(function() tweenAngles(startAng, endAng, 5, part) end) ) |
|
|
| Report Abuse |
|
|
Spectrumw
|
  |
| Joined: 04 Aug 2009 |
| Total Posts: 13510 |
|
|
| 07 Dec 2011 07:38 PM |
| Ermh, your function has three arguments; startP, endP and part. Now, when you called it that snippet of code, you added a new one, my best guess is that it is the time. Could that be a problem? |
|
|
| Report Abuse |
|
|
blockoo
|
  |
| Joined: 08 Nov 2007 |
| Total Posts: 17202 |
|
|
| 07 Dec 2011 07:39 PM |
| I added time in both the function declaration and the call... |
|
|
| Report Abuse |
|
|
Spectrumw
|
  |
| Joined: 04 Aug 2009 |
| Total Posts: 13510 |
|
|
| 07 Dec 2011 07:42 PM |
| Nevermind, my last post was a latetoast. Give me some minutes to test the script and check it. |
|
|
| Report Abuse |
|
|
blockoo
|
  |
| Joined: 08 Nov 2007 |
| Total Posts: 17202 |
|
| |
|
blockoo
|
  |
| Joined: 08 Nov 2007 |
| Total Posts: 17202 |
|
|
| 07 Dec 2011 07:52 PM |
| Found anything yet? I'm completely stumped :I |
|
|
| Report Abuse |
|
|
Spectrumw
|
  |
| Joined: 04 Aug 2009 |
| Total Posts: 13510 |
|
|
| 07 Dec 2011 07:57 PM |
Replace; xFact = (startP.x - endP.x) / time yFact = (startP.y - endP.y) / time zFact = (startP.z - endP.z) / time
With; xFact = (endP.x - startP.x) / time yFact = (endP.y - startP.y) / time zFact = (endP.z - startP.z) / time
It worked for me n_n |
|
|
| Report Abuse |
|
|
blockoo
|
  |
| Joined: 08 Nov 2007 |
| Total Posts: 17202 |
|
|
| 07 Dec 2011 08:03 PM |
| Weird, now it goes super-fast to the endpoint instead of taking the amount of time given... |
|
|
| Report Abuse |
|
|
Spectrumw
|
  |
| Joined: 04 Aug 2009 |
| Total Posts: 13510 |
|
|
| 07 Dec 2011 08:05 PM |
time = time / wait() xFact = (endP.x - startP.x) / time yFact = (endP.y - startP.y) / time zFact = (endP.z - startP.z) / time
That should solve the problem but, since these values will be floats, you might want to use some weird round sistem so it doesn't continue moving forever. |
|
|
| Report Abuse |
|
|
blockoo
|
  |
| Joined: 08 Nov 2007 |
| Total Posts: 17202 |
|
|
| 07 Dec 2011 08:08 PM |
| Now it doesn't even go anywhere near it's designated point D: |
|
|
| Report Abuse |
|
|
Spectrumw
|
  |
| Joined: 04 Aug 2009 |
| Total Posts: 13510 |
|
|
| 07 Dec 2011 08:10 PM |
| That is because as I already said, these uses float values, they are unexact, so the condition used in the for loop might never be true, so it will keep going forever. |
|
|
| Report Abuse |
|
|
blockoo
|
  |
| Joined: 08 Nov 2007 |
| Total Posts: 17202 |
|
|
| 07 Dec 2011 08:17 PM |
| I got it pretty accurate by using time = time / 0.03 |
|
|
| Report Abuse |
|
|
|
| 07 Dec 2011 08:29 PM |
Couldn't you just do something along the lines of
pos1,pos2 = Vector3.new(0,0,0),Vector3.new(10,10,10) time = 5 --Number of seconds you want it to take norm = (pos1-pos2).unit
for i = 0,1,(1/(time^2) do part.Position = norm + Vector3.new(i,i,i) end
-[::ƧѡÎḾḠΰῩ::]-[::Maker of stuff and Helper of Scripting::]- |
|
|
| Report Abuse |
|
|
|
| 07 Dec 2011 08:31 PM |
Needs more waiting
pos1,pos2 = Vector3.new(0,0,0),Vector3.new(10,10,10) time = 5 --Number of seconds you want it to take norm = (pos1-pos2).unit
for i = 0,1,(1/(time^2) do wait() part.Position = norm + Vector3.new(i,i,i) end
-[::ƧѡÎḾḠΰῩ::]-[::Maker of stuff and Helper of Scripting::]- |
|
|
| Report Abuse |
|
|
blockoo
|
  |
| Joined: 08 Nov 2007 |
| Total Posts: 17202 |
|
|
| 07 Dec 2011 08:36 PM |
@swimguy I'm going to ragequit if that works...I did all of that for nothing e.e
Anyway, what about tweening the angles? |
|
|
| Report Abuse |
|
|
|
| 07 Dec 2011 08:37 PM |
It should work, but the timing may be off, I just kind of threw it together in a minute or two. But yea, it should work, but you might want to change Position to CFrame, so parts can overlap without freaking out :P
-[::ƧѡÎḾḠΰῩ::]-[::Maker of stuff and Helper of Scripting::]- |
|
|
| Report Abuse |
|
|
blockoo
|
  |
| Joined: 08 Nov 2007 |
| Total Posts: 17202 |
|
|
| 07 Dec 2011 09:02 PM |
| I am using CFrame, though :P |
|
|
| Report Abuse |
|
|
|
| 07 Dec 2011 09:03 PM |
pos1,pos2 = Vector3.new(0,0,0),Vector3.new(10,10,10) time = 5 --Number of seconds you want it to take norm = (pos1-pos2).unit part = workspace.PART YOU WANT TO MOVE
for i = 0,1,(1/(time^2) do wait() part.CFrame = CFrame.new(norm + Vector3.new(i,i,i)) end
-[::ƧѡÎḾḠΰῩ::]-[::Maker of stuff and Helper of Scripting::]- |
|
|
| Report Abuse |
|
|
ENET
|
  |
| Joined: 01 Jan 2010 |
| Total Posts: 4820 |
|
|
| 07 Dec 2011 09:15 PM |
| Learn2Use.Unit? The unit circle is your friend! :)> |
|
|
| Report Abuse |
|
|
| |
|
blockoo
|
  |
| Joined: 08 Nov 2007 |
| Total Posts: 17202 |
|
|
| 07 Dec 2011 09:52 PM |
@crazy pointA:Lerp(pointB, 0.5) = (pointA + pointB) / 2
pointA:Lerp(pointB, 0.33333...) = (pointA + pointB) / 3
pointA:Lerp(pointB, 0.25) = (pointA + pointB) / 4
Which is why lerp is a waste of typing |
|
|
| Report Abuse |
|
|
|
| |
|
|
| |
|
»
»
|
|
|
|
|