|
hi there my friends i have a trouble in making the following: 4-the turret must be alinged with the ship at all times , but must also try to look at the target lead with the following: the above script works the best if used on a turret made with 1 piece of geometry. the turret is made up with 2 parts ,(turret) which moves only on Y Axis , (barrel) which moves only on X Axis but also follows the turret Y Axis. the troubles i am having:
please i wish someone really helps me with this because it have taken a long time in debugging , trying , scripting ....etc and also i am having lot of troubles with (quaternions) i have read the documents on it but still didnt understand it fully and i think thats why i failed at fixing this. thx in adv
(comments are locked)
|
|
I had the same freaking problem and worked around it like this. Here the corpus and barrel are handled. Both transformations are reduced to a 1 or 2 dimensional rotation. I use a temp look-at vector to work around quaternions. I stripped all the code for shooting particle control and some other stuff. !! For this to work, the barrel must be an individual model and has to have it's origin at it's rotation point If you only want to rotate the whole turret (corpus) then just stip the barrel code. ---------- As requested, here the javascript version, but not tested: can u good sir if u please , convert it to js , i am nothing in C#
Jul 10 '12 at 12:22 AM
MigLeader
Hi there, I tried to convert it to js, even if I am not often working with it. As you can see in this case only the variable declarations are a problem. (See above in my original post) C# and js are not so different. The idea behind the code stays the same, it are mostly some syntax issues. I am not sure about using .NET in js though. Anyways - learn C#, I can recommend it. It is very handy to have all that classes, inheritance (And all the other object oriented stuff) stuff on your side. Best of luck!
Jul 10 '12 at 10:29 AM
captaincrunch80
(comments are locked)
|
|
I didn't understand exactly what your script should do, but you should not mix eulerAngles with localEulerAngles - localEulerAngles are the angles relative to the parent transform, and you should take the parent rotation into account before setting them. Furthermore, eulerAngles aren't too reliable - eulerAngles are actually the transform.rotation converted to 3-axes representation on the fly; since there are several XYZ combinations equivalent to any possible rotation, Unity sometimes selects a weird one to return, driving us crazy. I think the simplest solution would be just to use transform.LookAt(target) in the turret script:
var target: Transform;
function Update(){
transform.LookAt(target);
}
If you want to follow the target with some delay, use this:
var target: Transform;
function Update(){
var dir = target.position - transform.position;
var rot = Quaternion.LookRotation(dir); // LookRotation takes a direction as argument
transform.rotation = Quaternion.Slerp(transform.rotation, rot, damping * Time.deltaTime);
}
EDITED: If you want to rotate the turret and the barrel around their local Y and X axes, respectively, there's a "dirty trick" that can split movements: for the turret, transform the target position in local space with InverseTransformPoint, zero its Y coordinate and convert it back to world space with TransformPoint - this will project the target position in the turret's local horizontal plane; you can then use Quaternion.LookRotation to find the desired rotation, and Slerp to it. You could also repeat the trick for the barrel (zeroing its X coordinate instead, so that the point would be projected in the barrel's vertical plane), but it's not advisable: errors accumulated over time will cause a deviation. It's better to just use LookRotation to find the necessary rotation to the target position and Slerp the barrel to it - the result is the same, but you'll not get accumulated errors: EDITED2: You were right: the turret was using its local space to find the point, and gradually this reference was being lost, giving very weird rotations. I changed the things a little: created an empty object (let's call it Ref) at the turret position, added the script below to it, then childed the turret and the barrel to the Ref object (which was childed to the ship):
Ship <- ship is parent of the Ref object
Ref <- control script goes here
Barrel <- barrel is childed to Ref
Turret <- turret is childed to Ref
The script cache references to Barrel and Turret, and control both:
var target: Transform;
var damping: float = 5.0;
private var turret: Transform;
private var barrel: Transform;
private var trf: Transform;
function Start () { // the barrel is a turret's child:
trf = transform;
barrel = trf.Find("Barrel");
turret = trf.Find("Turret");
}
function Update () {
// convert target position to local space:
var pos = trf.InverseTransformPoint(target.position);
pos.y = 0; // project it in the horizontal plane
// convert pos back to world space:
pos = trf.TransformPoint(pos);
// find the desired turret rotation and Slerp to it:
var rot = Quaternion.LookRotation(pos-trf.position, trf.up);
turret.rotation = Quaternion.Slerp(turret.rotation, rot, damping*Time.deltaTime);
// just point the barrel to the target:
rot = Quaternion.LookRotation(target.position-barrel.position, trf.up);
barrel.rotation = Quaternion.Slerp(barrel.rotation, rot, damping*Time.deltaTime);
}
ok thx for ur comment , but if u looked in my first post , u will find that i have a script that also leads the target depending on the speed or the bullet and the speed and range of the target. so if i used transform.LookAt(target) it will just look at the target without leading it. the only problem i am facing now , is to align the turret and the barrel to the ship without ruining the targeting script or the angles of the turret. thx for ur help anyway , anymore hints?
Jul 09 '12 at 07:14 PM
MigLeader
Ok, I suppose to have understood your question now: your first script is good for a single piece turret, but you're having problems with a combination turret+barrel, where you want the turret to rotate around its local Y axis, and the barrel to rotate around its X axis - is it correct?
Jul 09 '12 at 07:44 PM
aldonaletto
thats right good sir , but also i want them to align with the moving ship that they are on it. as u said , i want them to rotate around their local axis only. thx a lot for ur time
Jul 09 '12 at 09:20 PM
MigLeader
Take a look at my answer: I made the turret rotate around its local Y axis in direction to the target, while the barrel follows it and rotate also around its X axis to point the target - and all of this with some delay. It doesn't matter if the turret is childed to the ship: it will rotate around its local Y axis.
Jul 09 '12 at 10:52 PM
aldonaletto
i tried ur script but.... i still get the same problem, + another one: 1-the turret still gets really strange angle if the ship rotates to down or left for Ex... i was thinking , can i take the rotation of my first script posted and split it in to angles so i can use whatever i want but convert them in to local angles. as i said i am really bad with Quaternion , thats why i failed. and sry for bad english or anything wrong , its 2:30AM here and i am really tired. still i really thx u for ur time in helping me.
Jul 09 '12 at 11:32 PM
MigLeader
(comments are locked)
|
|
hi again , sigh , i tried everything above and trying to combine it with my code of lead targeting but nothing worked probably, so here i am asking a fix for it as the following: i have this script: all i want is to split the rotations from it in to X , Y , Z and store them in Vars , then convert them in to local rotations. i prefer to not change the code of leading targets cuz its working the best and i couldn't find another way to do it. please this problem have taken from me most of my Time , still i thank everyone who tried to help me with it. thx in adv
(comments are locked)
|
|
ok this Script works semi fine: but as soon as i move the ship , the turrets starts to inherit some added rotation from the ship so it starts missing the right point to aim to. any fix for this? please help me at least with a hint only.
(comments are locked)
|

anyone , i worked more on it but still failing to get at least a good results , hope i get help soon.
thx