0
\$\begingroup\$

I'm trying to make my first game in Unity and I want a line renderer to draw from the end of a turret's gun barrel. The problem is I'm rotating 2 bones in the turret to make it look at the enemy. I've used the bounding box of the turret to calculate the offset from the turret's origin to the end of the gun barrel. But when the turret rotates this position is obviously wrong. How can I calculate the correct starting point for the line renderer after performing a rotation?

Here is the code for the rotation and the line renderer:

void Start () {
    enemyManager = GameObject.Find("EnemyManager").GetComponent<EnemyManager>();
    lazerLine = GetComponent<LineRenderer>();

    platform = transform.root.Find("Armature/BaseBone/PlatformBone"); //Get the root transform of this turret (I.e. the turret itself, not the gun barrel)
    gunBox = transform.root.FindChild("Armature/BaseBone/PlatformBone/LowerArmBone/UpperArmBone/GunBoxBone");

    gunBounds = transform.parent.GetComponent<SkinnedMeshRenderer>().bounds.size; //Get the bounding box of the turret
}

void LateUpdate()
{
    Vector3 lookAt;
    Quaternion platformRotation;
    Vector3 gunLookAt;
    Quaternion rotation;

    if (enemiesInRange.Count > 0)
    {
        //Rotate Platform (The silver platform at the base of the turret)
        lookAt = enemyManager.activeEnemies[enemiesInRange[0]].transform.position - platform.position;
        lookAt.y = 0; //0 the Y axis to stop the turret tilting up or down

        platformRotation = Quaternion.LookRotation(lookAt);

        float rotateSpeed = 10.0f * Time.deltaTime;
        platform.rotation = Quaternion.Lerp(platform.rotation, platformRotation, rotateSpeed);

        //Rotate GunBox
        gunLookAt = enemyManager.activeEnemies[enemiesInRange[0]].transform.position - gunBox.position;

        rotation = Quaternion.LookRotation(gunLookAt);
        gunBox.rotation = Quaternion.Lerp(gunBox.rotation, rotation, rotateSpeed);

    }

    if (timer <= effectsDisplayTime)
    {
        Vector3 lazerStart = transform.position;
        //Adjust the lazer to draw from the default position of the top gun barrel (using the bounding box of the turret to calculate the position)
        lazerStart.y += (gunBounds.y * 0.85f);
        lazerStart.z += (gunBounds.z / 2) + 0.05f;

        //Adjust the lazer to follow the current rotation of the gun
/* ##### This is the part I need to add ##### */

        //Draw the lazer
        lazerLine.enabled = true;
        lazerLine.SetPosition(0, lazerStart);
        lazerLine.SetPosition(1, enemyManager.activeEnemies[enemiesInRange[0]].transform.position); //Of the enemies in range, shoot the first enemy that entered the turrets range

        effectsEnabled = true;
    }
}

Here are 2 picture to give you a better idea of what's happening.

This is with the rotation code commented out. enter image description here

This is with the rotation code running.

enter image description here

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

It might be simpler to use an empty gameobject on the tip of the barrel, with the head of your turret as its parent, and use that as a starting point for your line renderer.

EDIT: Final sollution (can be found in comments as well): Make an empty gameobject the child of the bone that controls the head of the turret. Use this as a startingpoint for the line renderer.

\$\endgroup\$
6
  • \$\begingroup\$ Thank you for your reply. Since posting this I have added an empty game object in Unity (not Blender) inside the barrel object and attached my line render to it. It does align the line renderer to the gun barrel at first, but when my script rotates the bones of the turret the line renderer/empty object doesn't follow. Would it behave differently if I created the empty object in Blender instead? I've been trying to adjust the start position of the line renderer using RotateAround and LookAt, but I can't get it right. \$\endgroup\$
    – Daniel
    Commented Sep 19, 2016 at 14:10
  • \$\begingroup\$ I've also noticed that the origin of the bone which connects the lower arm to the upper arm rotates how I want but obviously too far away from the barrel. So was wondering if it's easier to attach the line renderer to that and offset it's position? Either way the gun tilts up and down on top of the arm so I need to move the start position on the Y axis as well. \$\endgroup\$
    – Daniel
    Commented Sep 19, 2016 at 14:11
  • \$\begingroup\$ I dont know if changing it in blender will work. But if you can make the bone that handles the head the parent of your empty gameobject instead, that might solve your problem. (Grasping in the dark a bit here, never worked with armatures in Unity before, sorry). \$\endgroup\$
    – Dennis
    Commented Sep 19, 2016 at 14:14
  • \$\begingroup\$ No problem, I appreciate you trying to help. I will try tonight when I get home and let you know how I get on, thanks. \$\endgroup\$
    – Daniel
    Commented Sep 19, 2016 at 14:19
  • \$\begingroup\$ That solved my problem. I was adding the empty object as a child of the Gun Barrel object. For some reason I hadn't thought of making it a child of the bone! Suppose it was pretty obvious lol. Thanks for your help :) \$\endgroup\$
    – Daniel
    Commented Sep 19, 2016 at 20:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .