How to get the transform's current LookPosition

Hello!

I’m making a script that controls a laser turret that shoots at the player. Since I don’t want to instantly kill the player, I need it to have reduced accuracy.

Transform.LookAt(LookPosition) is a very powerful tool, and I’m using it as shown here:

using UnityEngine;
using System.Collections;

public class LaserGunController : MonoBehaviour {

    public GameObject Player;
    public float TimeBetweenAims;
    public float AccuracyRemovalRange;
    public float RandomAccuracyAmount;
    //the rest is for lerping
    public float startTime;
    public float journeyLength;
    private Vector3 NewAimPosition;
    // Use this for initialization
    void Start ()
    {
        //find player to shoot at
        Player = GameObject.FindWithTag("Player");
        StartCoroutine(AimTime());
    }
  
    // Update is called once per frame
    void Update ()
    {
        float distCovered = (Time.time - startTime) * 5;
        float fracJourney = distCovered / journeyLength;
        //I want to lerp from the current look position, NOT the player position
        //as this results in jerky movement because it doesn't take into account where it's looking right now.
        transform.LookAt(Vector3.Lerp(Player.transform.position, NewAimPosition, fracJourney));
    }

    IEnumerator AimTime()
    {
        //get a random accuracy value
        RandomAccuracyAmount = Random.Range(-AccuracyRemovalRange, AccuracyRemovalRange);
        yield return new WaitForSeconds(TimeBetweenAims);
        //add the random accuracy value we generated
        NewAimPosition = new Vector3(Player.transform.position.x + RandomAccuracyAmount, Player.transform.position.y + RandomAccuracyAmount, Player.transform.position.z + RandomAccuracyAmount);
        //start lerping and repeat
        startTime = Time.time;
        journeyLength = Vector3.Distance(Player.transform.position, NewAimPosition);
        StartCoroutine(AimTime());
    }
}

However, currently the movement is very jerky because I’m lerping from PlayerPosition > RandomPosition, instead is CurrentLookPosition > RandomPosition.

As I’m using Vector3.Lerp and Transform.LookAt(), I need to find what is the position it is currently looking at. How would I find what is the position the turret looking at?

Thank you!

Vector3 oneUnitInFrontOfTurret = transform.position + transform.forward;

2 Likes

Thanks, that works perfectly! I’m surprised I didn’t think of that myself.

The whole script for future reference:

using UnityEngine;
using System.Collections;

public class LaserGunController : MonoBehaviour {

    public GameObject Player;
    public float TimeBetweenAims;
    public float AccuracyRemovalRange;
    public float RandomAccuracyAmount;
    //the rest is for lerping
    public float startTime;
    public float journeyLength;
    private Vector3 NewAimPosition;
    private Vector3 oneUnitInFrontOfTurrent;
    // Use this for initialization
    void Start ()
    {
        //find player to shoot at
        Player = GameObject.FindWithTag("Player");
        StartCoroutine(AimTime());
        oneUnitInFrontOfTurrent = transform.position + transform.forward;
    }
   
    // Update is called once per frame
    void Update ()
    {
        float distCovered = (Time.time - startTime) * 0.02f;
        float fracJourney = distCovered / journeyLength;
        //this is from here: https://forum.unity3d.com/threads/how-to-get-the-transforms-current-lookposition.443134/#post-2865891
        oneUnitInFrontOfTurrent = transform.position + transform.forward;
        //I want to lerp from the current look position, NOT the player position
        //as this results in jerky movement because it doesn't take into account where it's looking right now.
        transform.LookAt(Vector3.Lerp(oneUnitInFrontOfTurrent, NewAimPosition, fracJourney));
    }

    IEnumerator AimTime()
    {
        //get a random accuracy value
        RandomAccuracyAmount = Random.Range(-AccuracyRemovalRange, AccuracyRemovalRange);
        yield return new WaitForSeconds(TimeBetweenAims);
        //add the random accuracy value we generated
        NewAimPosition = new Vector3(Player.transform.position.x + RandomAccuracyAmount, Player.transform.position.y + RandomAccuracyAmount, Player.transform.position.z + RandomAccuracyAmount);
        //start lerping and repeat
        startTime = Time.time;
        journeyLength = Vector3.Distance(Player.transform.position, NewAimPosition);
        StartCoroutine(AimTime());
    }
}

I would have just used the rotations myself, with Quarternion.LookRotation and RotateTowarda. But your system also works.

Yeah, you’re probably better off getting rotations using Quaternion.LookRotation and then using Slerp or rotateTowards. You current solution will have funny behaviour if the turret has to focus on something behind it. When the vector you are lerping goes through the center of the turret the turret will instantly rotate its direction. You’ll probably also notice that the speed of rotation may not be consistent. But really it all depends on your situation and what results you’re after. If you’re happy with it, move on.

2 Likes