stroudieJoin Date: 2008-11-28 Post Count: 2556 |
I'm currently trying to make a turret that rotates with the movement of the mouse. The turret is on top of a tank, and the turret movement is done with a weld between itself and a fixed point on the tank.
My problem is that I can't work out what existing values (the lookVector of the Engine, the lookVector of the turret etc) to use, and how to use them to point the turret correctly. Only one axis needs to be affected, so the turret just turns to face any side of the tank. I have set up all of the parts facing the right directions, to make things easier (front of parts facing the front etc).
I need to know how to keep the turret pointing in the direction of a players mouse, no-matter the direction that the tank itself is facing in.
If anyone can help, it would be appreciated. If this description's confusing, let me know. Currently with the script I have the following, which only lets the turret point within a certain range infront of the tank, whilst the tank is facing forward in relation to the map:
MWeldX.C1 = CFrame.new() * CFrame.Angles(0,Mouse.Hit.lookVector.x,0) |
|
stroudieJoin Date: 2008-11-28 Post Count: 2556 |
b1 |
|
stroudieJoin Date: 2008-11-28 Post Count: 2556 |
b2 |
|
Scubadoo2Join Date: 2008-07-15 Post Count: 98 |
I had this same problem trying to make my own tank. You need to convert the mouse hit to the turrent by doing turrent.CFrame:pointToObjectSpace(Mouse.Hit) and then taking the desired vector out of that.
...I think. I'm not at the computer now so I can't double-check at the moment. Please say if it works or not. |
|
TimeShopJoin Date: 2010-11-24 Post Count: 1274 |
Wouldn't just creating a lookVector be good enough? CFrame.new(Tank.Position, mouse.Hit.p) |
|
stroudieJoin Date: 2008-11-28 Post Count: 2556 |
Using...
MWeldX.C1 = CFrame.new() * CFrame.Angles(0,MainPart.CFrame:pointToObjectSpace(Mouse.Hit.lookVector).x,0)
The turret just spazzed out. Did you mean it in some other form? |
|
stroudieJoin Date: 2008-11-28 Post Count: 2556 |
@TimeShop, No, because welds work in relation between two parts, not the world. That would only work whilst the vehicle is facing a certain way. |
|
Scubadoo2Join Date: 2008-07-15 Post Count: 98 |
Woops... yep, I meant it in some other form. How I did it with my tank is this:
----
engine:findFirstChild("Weld").C0 = CFrame.new(Vector3.new(welded.x, welded.y, welded.z), engine.CFrame:pointToObjectSpace(target))
----
This works perfectly, no matter how much it turns. What you were asking was a little tricky but I figured it out. Just tried it out so this should work:
----
it = engine.CFrame:pointToObjectSpace(target)
MWeldX.C1 = CFrame.new(Vector3.new(0,0,0), Vector3.new(0,it.y,it.z))
----
If this movement isn't what you are looking for then mess around with the second Vector3. Hopefully it works. |
|
stroudieJoin Date: 2008-11-28 Post Count: 2556 |
Thanks. |
|
IctisJoin Date: 2011-09-01 Post Count: 1216 |
Does that actually working considering that the weld is subjective to its part 0? I tried something similar and figured it spazzed out because I was changing the weld, not the actual CFrame of the part. |
|
|
Wouldn't it just be easier to set the position of the turret atop the tank with CFrame's, and to change the rotation with CFrame's every 0.01 second? |
|
IctisJoin Date: 2011-09-01 Post Count: 1216 |
Easier? Yes.
Not so much in terms of efficiency or looks (it will stutter horribly). |
|
stroudieJoin Date: 2008-11-28 Post Count: 2556 |
The method didn't work. I eventually just changed it so that the turret uses the A and D keys to rotate by 5 degrees as fast as it can (doesn't stutter). |
|
stroudieJoin Date: 2008-11-28 Post Count: 2556 |
For this turret I think it actually works better like that. The turret user doesn't also drive the vehicle, or fire the main guns. |
|
|
Is this scripting?, All I know I need to know how to script and CFrame and everything is all better. :/ |
|