0

I'm working on a hand sway system. And the problem is that when I use Vector3.Lerp(); for it, it looks really bad and laggy :

https://streamable.com/64y52o

And this is my source code :

Transform target;
float smooth;

void Update()
{
    transform.position = Vector3.Lerp(transform.position, target.transform.position, smooth * Time.deltaTime);
}

And this is my setup in editor :

enter image description here

The script is attached to Hand game object. And the target input is Hand Pos object in inspector.

I tried using both Vector3.Lerp(); and Vector3.Slerp(); but it didn't change anything. I also tried running code on both Update() and FixedUpdate() and even LateUpdate() but not much changed.

Full code for source :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HandSway : MonoBehaviour
{
public float smooth;
public float swayMultiplier = 1f;
public Vector3 handOffset;

public Transform target;

private void Update()
{
    float x = Input.GetAxisRaw("Mouse X") * swayMultiplier;
    float y = Input.GetAxisRaw("Mouse Y") * swayMultiplier;

    Quaternion xRot = Quaternion.AngleAxis(-y, Vector3.right);
    Quaternion yRot = Quaternion.AngleAxis(x, Vector3.up);

    Quaternion rotation = xRot * yRot * target.rotation;

    transform.localRotation = Quaternion.Slerp(transform.localRotation, rotation, smooth * Time.deltaTime);

    transform.position = Vector3.Lerp(transform.position, target.transform.position, smooth * Time.deltaTime);
}
}
  • 1
    What should `smooth * Time.deltaTime` represents for you? Normally the last value should be between 0..1 deltaTime is the time between update calls. So it's jiggling. You should probably use a field (variable) to add the deltaTime and pass that variable to the lerp function – Jeroen van Langen Dec 20 '22 at 15:15
  • @JeroenvanLangen hi ! tnx for your comment. actually `smooth` is the speed here, and as I found in youtube tutorials the lerp should work this way some how like : https://www.youtube.com/watch?v=gqU1t1jpmDw but I'm wondering why it is Jiggling in my case – Erfan_Games Dec 20 '22 at 15:34

1 Answers1

0

There are a few different approaches when animating an object.

One is to start from position A and interpolate over T seconds to position B. This would be expressed in code like:

t += Time.deltaTime;
transform.position = Vector3.Lerp(A, B, t / T);

This should move the object with a constant speed. If you want to control the speed instead you you could compute T = A.Distance(B) / speed. If you want a constant acceleration instead you could use SLerp.

If you want the object to track the target continuously rather than animating it I would probably not recommend just using Lerp. This will result in a speed that is completely dependent on the distance. This would be equal to a proportional only regulation, see PID controller. Notably, if your proportional factor (i.e. smooth * Time.deltaTime) is larger than 1 you will get oscillations, that might be what is happening.

I would probably recommend that you instead calculate the direction to the target, and cap the speed. You might also want to cap the acceleration for a smoother animation.

JonasH
  • 28,608
  • 2
  • 10
  • 23