I have a little test tank, that is made of cube objects (the standard “click to make” cubes), and my issue is that when my script rotates the turret, the barrel object, and it’s mount, sink into the turret, and the barrel rotates in the middle of it’s self, ignoring the barrel mount as it’s parent.
The hierarchy is :
Tank > Turret > barrelmount > barrel
and the scripted objects for the turret action is Turret and barrelmount.
The turret does all lateral rotation, and the barrelmount acts as a pivot for the barrel and controls the pitch action.
Yet, when the barrelmount pitches up, it does so normally, but the barrel child is pitching in it’s central pivot, and not off the parent.
Is this by design?
Cheers.
p.s - They also morph and warp at times too, skewing the shapes etc.
I think you parent them wrongly. From what i see, your barrel mount is parented to turret when what you want is rotating the barrel mount, the turret will follow. I don’t know if that helps.
Well. I felt that perhaps a bit of polish for the scripts was in order, and that it may solve my issues. Well. Turns out that’s a no. I’m stuck on getting the turret to turn and “snap” to a stop when facing the target. As the Quaternion.Slerp() function is good, but it turns to the target and then slows down immensely. Which, is not an awful lot of use.
Here is the script so far.
using UnityEngine;
using System.Collections;
public class TurretController : MonoBehaviour
{
//Public
public GameObject target;
//Private
private float engageRange;
//Transform properties
private float minTraversal;
private float maxTraversal;
private float minPitch; // 15
private float maxPitch; // -65 (minus for some reason)
private Transform barrelmount;
private Transform barrel;
private float rotResistance = 1;
// Use this for initialization
void Start ()
{
// get the turret's children.
barrelmount = transform.Find("barrelmount");
barrel = transform.Find("barrel");
// Set transform limits.
minPitch = 15;
maxPitch = 65;
minTraversal = 0;
maxTraversal = 360;
// Debug
//give a target
target = GameObject.Find("targetTank");
}
// Update is called once per frame
void Update ()
{
//TargetManager(); // Gets targets. OMITTED!! Will be placed in the main unit's code and will update when needed.
TurretManager();
FireControlManager();
}
void TurretManager()
{
if (target)
{
var rotate = Quaternion.LookRotation(target.transform.position - transform.position, transform.up);
rotate = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * rotResistance);
transform.rotation = Quaternion.Euler(new Vector3(0, rotate.eulerAngles.y, rotate.eulerAngles.z)); // Quaternion.Slerp(transform.rotation, rotateEu, Time.deltaTime * rotResistance);
}
}
void FireControlManager()
{
}
}
Slerping from the current rotation to the target rotation on each update does result in an ease out at the end of the interpolation.
Another option is to just use transform.Rotate() until it reaches the target angle. That would give it an abrupt stop. Then follow with one frame that sets it to the exact angle needed - if that is necessary.
The script only looks like it is rotating the turret. Somewhere the barrelmount is being tilted? but I don’t see where.
You also set the barrel into a variable, but that should just act as a child when you tilt the barrelmount, so there would not be a use for that (unless you were to give it a quick translate backwards on its local axis when it shoots to show the recoil).
Is the rotation problem gone?
P.S. - in your screenshot, “barrel” was the child of “barrelmount”, but here you do transform.Find(“barrel”) which would fail in that case - it should be transform.Find(“barrelmount/barrel”) - but again, only if you need to do something with the barrel.
Nope. I have it skewing when it rotates, I can’t keep the turret to ONLY rotate on the Y axis and maintain normal angles on the rest of the fields, and how do you mean by “You also set the barrel into a variable, but that should just act as a child when you tilt the barrelmount, so there would not be a use for that” ← I see no other way? Care to explain your idea?
if the barrel is a child of the barrelmount - then you should only have to rotate the barrelmount and the child will move wiith it (local space of child to parent hierarchy)
Just like rotating the turret on Y should make the barrelmount(with barrel) appear to rotate as a single unit.
So if the turret is skewing, it must be due to non-uniform scaling of the parent(s) above the turret. Rather than replacing the turret or barrel with a capsule as suggested, try replacing the parent of the turret with something scaled 1,1,1 (or 2,2,2 - just not non-unitform like (1,1,3))
or just scale the current parent to 1,1,1 for the test and see if it still skews.
(but skewing is a distortion - and that was not seen in your screenshot, in that I could onlyl see the barrel not moving to the correct position as child of the barrelmount, so my guess was that you were rotating both the barrelmount and the barrel in the script at the same time but they dont seem to be rotated at all in the code you posted)