Unity Lerp Linear Interpolation

Asked 1 years ago, Updated 1 years ago, 65 views

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

public class TitanAttack : MonoBehaviour {

public GameObject player;
public GameObject titan;
public GameObject Rhand;
public GameObject Lhand;

private Vector3 position;
private Vector3 Rhandpos;

public float speed;

void smash()
{
    Vector3 pos = new Vector3(position.x, 10, position.z);
    Rhand.transform.position = Vector3.Lerp(Rhandpos, pos, Time.deltaTime * speed);
}

void swing()
{

}

void attack()
{
    position = player.transform.position;
    if (Vector3.Distance(position, titan.transform.position) > 26)
    {
        smash();
    }
    else
    {
        swing();
    }
}

// // Use this for initialization
void Start () {
    Rhandpos = new Vector3(-15, 5, 35);
    InvokeRepeating("attack", 3, 10);
}

// // Update is called once per frame
void Update () {

}

}

It stops in the middle of running Lerp in the above smash code, so how can I get to the end?

unity

2022-09-22 18:23

1 Answers

Time.deltaTime returns the time taken per frame, so if you use it, it is when you calculate delta based on the present. To implement with this value, you must set a separate state variable that accumulates each delta.

If you need to specify an animation or position value for interpolation, Usually, the difference from the current time is based on the start time.

The third parameter of the Lerp function must have a value between 0 and 1.0.

The value of the third parameter is It depends on what you base it on.

Based on the total distance,

https://docs.unity3d.com/ScriptReference/Vector3.Lerp.html You can look at the update() function in this example.

Based on the total time taken

float ratio = (startTime - Time.time) / totalTime;
Rhand.transform.position = Vector3.Lerp(Rhandpos, pos, ratio);


2022-09-22 18:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.