Aligning Turret Arm Forward Rotation

I am trying to add turrets to my sidescroller demo and am having trouble getting them to aim in the right direction. Here is a video showing the problem. I am drawing a Debug ray in the code to show where it should be aiming. You may have to view the video in the 720p resolution on YouTube in order to see the red ray being rendered.

Checkout what I mean in the YouTube demo video.

Here is the TurretController script I am using:

#pragma strict

public var turretArmMoveSpeed : float = 10.0;
public var weaponTransform : Transform;
public var target : Transform;

function Start () {
}


function LateUpdate () {
	var targetDirection = target.position - weaponTransform.position;
	var newDirection = Vector3.RotateTowards(weaponTransform.forward, targetDirection, turretArmMoveSpeed * Time.deltaTime, 0.0);
	Debug.DrawRay(weaponTransform.position, newDirection, Color.red);
    weaponTransform.rotation = Quaternion.LookRotation(newDirection);
}

The motion in my sidescroller is confined to the XY-plane where z=0. When I place my turrets, the child weapon mesh is set to weaponTransform and the Player is set to the target. As you can see in the YouTube video, it appears the forward direction of my turret arms are not aligned along the arms length, so the seems to be 90 degrees off? Does anyone know how I can fix this?

1 Answer

1

I was able to fix this by creating an empty GameObject and adding it to the Turret prefab and making the turret arms a child of it and rotating the game object 90 degrees to match. I assume this problem was caused by the model not being aligned correctly to match Unity in the 3D modeling program that was used to create it.