I want to move objects smoothly within a function, not update.

Asked 2 years ago, Updated 2 years ago, 115 views

Within the Update function, you can move objects smoothly using Lerp and time.deltaTime. But I don't know how to implement it within the functions I define. 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;

private float distance;

public float speed;

void smashGo()
{
    Vector3 pos = new Vector3(position.x, 10, position.z);
    Rhand.transform.position = Vector3.Lerp(Rhandpos, pos, 1);
}

void smashCome()
{
    Vector3 pos = new Vector3(position.x, 10, position.z);
    Rhand.transform.position = Vector3.Lerp(Rhandpos, pos, 0);
}

void smash()
{
    smashGo();
    Invoke("smashCome", 3);
}

void swing()
{

}

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

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

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

}

}

For your information, I haven't made a swing function yet

unity move

2022-09-22 18:55

1 Answers

I haven't used Unity and C#, so I've been struggling to code after installing it.

I think you're trying to use Invoke.

I can't verify the code that the questioner wrote I just leave the code that I tested after flying an airplane.

Just keep that in mind.

using UnityEngine;
using System.Collections;

public class Aircraft : MonoBehaviour {

    private Vector3 from;
    private Vector3 to;
    private float startTime;
    private const float totalTime = 5.0f;

    void go () {

        to.Set (20, 10, -50);
        startTime = Time.time;

        // Move method invoke every 16ms for smooth animation
        InvokeRepeating ("move", 0, 0.01667f);
    }

    void move () {

        float deltaTime = Time.time - startTime;

        if (deltaTime < totalTime) {
            this.transform.position = Vector3.Lerp (from, to, deltaTime / totalTime);
        } } else {
            this.transform.position = to;
            Cancel Invoke ("move"); // End invoke repeater when animation ends
        }
    }

    // // Use this for initialization
    void Start () {

        from.Set (0, 0, 0);
        this.transform.position = from;

        Invoke ("go", 2); // Run after 2 seconds
    }

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

    }
}

P.S. Unity has several discussions about which of the Update, FixedUpdate, Coroutine, and InvokeRepeating should be used for frame processing in certain situations. I think you need to look for these and make sure that they are the right way to do the tasks you have to be done.


2022-09-22 18:55

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.