Skip to content
Snippets Groups Projects
SmoothFollowSchlau.cs 1.77 KiB
Newer Older
Rico's avatar
Rico committed
using System.Collections;
using System.Linq;
using System.Collections.Generic;
using UnityEngine;

public class SmoothFollowSchlau : MonoBehaviour
{

  public Transform target;

  public float DampPosition = 10.0f;
  public float DampRotation = 10.0f;

  public void Update()
  {
    if (!target)
      return;

    var currentRotationAngleX = Mathf.LerpAngle(transform.eulerAngles.x, target.eulerAngles.x, DampRotation * Time.deltaTime);
    var currentRotationAngleY = Mathf.LerpAngle(transform.eulerAngles.y, target.eulerAngles.y, DampRotation * Time.deltaTime);
    var currentRotationAngleZ = Mathf.LerpAngle(transform.eulerAngles.z, target.eulerAngles.z, DampRotation * Time.deltaTime);

    var curposX = Mathf.Lerp(transform.position.x, target.position.x, DampPosition * Time.deltaTime);
    var curposY = Mathf.Lerp(transform.position.y, target.position.y, DampPosition * Time.deltaTime);
    var curposZ = Mathf.Lerp(transform.position.z, target.position.z, DampPosition * Time.deltaTime);

    if (float.IsNaN(curposX) || float.IsNaN(curposY) || float.IsNaN(curposZ))
    {
      return;
    }

    // if (Lookat == false) {

    this.transform.localEulerAngles = new Vector3(currentRotationAngleX, currentRotationAngleY, currentRotationAngleZ);
    this.transform.position = new Vector3(curposX, curposY, curposZ);

    // } else {
    // this.transform.LookAt(GameObject.Find("TargetSmooth").transform);
    //	this.transform.localPosition = Vector3(curposX, curposY, curposZ);
    //	SmoothRotation(target.position, rotDamp); }
  }

  //function SmoothRotation( targetPos : Vector3, rotSpeed : float ) {  
  //	targetRot = Quaternion.LookRotation( targetPos - transform.position );
  //    transform.rotation = Quaternion.Slerp( transform.rotation, targetRot, Time.deltaTime*rotSpeed ); }

}