Turret made from gameObjects

Hello all!

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. :expressionless:

Nothing? :frowning:

Are you doing any manual rotation on the barrel?

i don’t touch the barrel at all. Just the barrelmount.

What exactly is the “barrelmount”? Where is it positioned in relation to everything? Can you post your scripts?

Oh, and I’m guessing the warping is due to you messing with scaling.

In image 1 is the barrelmount benig selected.

Image 2 shows the barrel mount pitched upwards, and the barrel having pitched also, but on it’s own axis.

I’m going to guess it’s a scaling issue. Try switching to a capsule and keep its default scale and tell me if you still have the same issue.

The capsule does the same. i left it all default, and still messes up.

Don’t know. Post your scripts.

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)

BTW the codes u posted is not in php, it’s c#.

I know, but the normal code box has no highlighting :stuck_out_tongue:

Oh, and the barrel is in the script so that I can use it for firing etc. :slight_smile:

yes, good point, but it is also a clue to the cause for the misaligned barrel and barrelmount, but the code that tilted them up is no longer there :slight_smile:

The skewing has been solved. I guess making tanks the easy way is not so much the easy way, If i cannot scale them at all…

Now my next primary issue is the turret rotating on all axis. I’ve tried this :

if (target)
		{
//			Quaternion parentRot = transform.parent.transform.rotation;
//			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(parentRot.eulerAngles.x, rotate.eulerAngles.y, parentRot.eulerAngles.z)); // Quaternion.Slerp(transform.rotation, rotateEu, Time.deltaTime * rotResistance);
			
			var lookPos = target.transform.position - transform.position;
			lookPos.y = 0;
			var rotation = Quaternion.LookRotation(lookPos);
			transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * rotResistance);
			
		}

Not sure what you mean by this.

transform.Rotate(0,1,0) will rotate the turret on its local Y axis only. That would be how a tank works too.

If you do it with tranform.rotation, it would be by using transform.localRotation instead