- Home /
Turret aiming script needs adjustment / fine tuning
Hey all!
Here is my issue, and I'll post some code / pseudo code to make sure everyone is with me on what I am doing / trying to accomplish.
I have a Turret, with attached Shooters, and attached Aimer. The general workflow of my code is :
Aimer finds all of my Target objects, arrays them, and runs through the array. It checks to make sure the target is within my allowed shooting area, and if so it makes sure the target is closer than the last selected target, and if so it sets the Target as my "target" variable, and sets its distance as the closest. It goes through all of the Targets with this criteria and eventually settles on a target.
My Turret then pulls in this Target and sets it as its own target. It then takes what I call "Lead Info" from my target (basically the rigidbody.velocity of the target) and combines that with the targets current position to figure out where it needs to shoot in order to hit. It then shoots.
Upon shooting, my Shooter has a Fire() function on it that fires the bullets, and everything works well together.
I hope that made sense for everyone. Now, on to my problem. My aiming code for the Aimer script, and also in my Turret script, is something like this :
transform.rotation = Quaternion.RotateTowards(transform.rotation,
Quaternion.LookRotation(leadDirection, transform.up),
maxDegreeRotation);
where leadDirection is the place where my LeadInfo + target position have told me the target will be.
Ok, so, now, I want to transform this script due to a number of problems. First of all, currently my Aimer has a very loose constraint on what is considered within its shooting radius. And since my Turret uses a similar code to actually track the target, even though it is going after the "lead direction" as its shooting spot, it runs into problems. The problems are as such; The turret rotates but can still fire through the platform its on (not good enough constraints) and the turret rotates weirdly, as in, it not only rotates on X axis and Y (for left right / up down) but also on the Z axis, which is causing weird rolling issues whenever it turns.
What I want to do :
Take a starting direction being straight up from the turret. That will be my base rotation. I then want to limit the X and Y rotation (I think its X and Y...) so that it simply CANNOT aim beyond a certain cone that I can alter. I think staring from a straight up rotation, I could set it something like 75 degrees either way, so if up is 0, it cannot target more than -75 degrees or more than 75 degrees. Do this for both X and Y so that it cannot go beyond that cone. Once I am able to constrain the Aimer, I want to constrain the Turret itself to also have a similar shooting cone so that even if my LeadDirection tells it to go beyond it, it will not. Now, I reckon I could do something like this using the Math Clamp function, but I am not sure how to do it. In addition, I want to change my code above so that instead of it rotating toward my target all willy nilly, it can be constrained to aim the X, then aim the Y, so that I can keep the weird Z rotation out of the equation all together. I just can't wrap my head around how something like that would look. Any ideas?
Just FYI, I am posting this at work so I do not have access to my code for another 5 hours or so.
Answer by cjmarsh · Jan 19, 2011 at 04:27 AM
You could try something like this:
var xRotConstraint : float = 75; var yRotConstraint : float = 75; var targetingRot : Quaternion; var xRot : float; var yRot : float; //plus your own vars mentioned above
//then, in your Aimer... targetingRot = Quaternion.LookAt( leadDirection ); targetingRot.x = 0; xRot = targetingRot.eulerAngles.x; yRot = targetingRot.eulerAngles.y; targetingRot.eulerAngles.x = Mathf.Clamp( xRot, -xRotConstraint, xRotConstraint ); targetingRot.eulerAngles.y = Mathf.Clamp( yRot, -yRotConstraint, yRotConstraint ); transform.rotation = Quaternion.RotateTowards( Quaternion.identity, targetingRot, maxDegreeRotation );
Don't quote me on this, but that should achieve the effect you want. Anyways, let me know if that helps any.
Actually, I went through re-writing my ai$$anonymous$$g code this last weekend and found a solution that should work for now. In the end, I ended up doing almost exactly the same thing you're doing here, except I used a separate function for clamping so that I could eli$$anonymous$$ate rotations over 360 degrees. But in the end, my final product was just about the same. Good call man :)
Your answer
Follow this Question
Related Questions
Need Help with random rotatition on game turret 2 Answers
Turrets |Aiming don't work 1 Answer
Turret script. Making it aim in heigth only. 1 Answer
3D Turret Rotation 0 Answers
3D nested Turret Prefab Rotation 1 Answer