N
N
N1ghtPlayer2021-04-06 17:55:31
C++ / C#
N1ghtPlayer, 2021-04-06 17:55:31

How to move an object a certain distance over time?

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

public class cubeBehavior : MonoBehaviour
{
    // Start is called before the first frame update
    GameObject Player;

    Queue<int> queue = new Queue<int>();

    private Vector3 startPos;

    private Vector3 endPos;

    private float distance = 30f;

    private float lerpTime = 5;

    private float currentLerpTime = 0;

    private bool keyHit = false;

    void Start()
    {
        startPos = Player.transform.position;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.UpArrow))//очередь создана
            queue.Enqueue(1);


        if (Input.GetKeyDown(KeyCode.LeftArrow))
            queue.Enqueue(2);


        if (Input.GetKeyDown(KeyCode.RightArrow))
            queue.Enqueue(3);


        if (Input.GetKeyDown(KeyCode.DownArrow))
            queue.Enqueue(4);


        if (Input.GetKeyDown(KeyCode.Space))
            Play();

    }

    public void Play()
    {
        foreach (int number in queue)
        {
            if (number == 1)<b>//нужна помощь здесь</b>
            <b>{</b>
                <b>endPos = Player.transform.position + Vector3.forward * distance;
                keyHit = true;
                if (keyHit == true)
                {
                    currentLerpTime += Time.deltaTime;
                    if (currentLerpTime >= lerpTime)
                    {
                        currentLerpTime = lerpTime;
                    }
                }
                float Perc = currentLerpTime / lerpTime;
                Player.transform.position = Vector3.Lerp(startPos, endPos, Perc);
            }</b>
            else if (number == 2)
            {
                transform.Rotate(0, 90, 0);
            }
            else if (number == 3)
            {
                transform.Rotate(0, 45, 0);
            }
            else if (number == 4)
            {
                print("4");
            }
        }
    }
}

I want to make an object move from one point to another over time, but it does not work

PS sorry if I formatted something wrong
PPS

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
namee, 2021-04-07
@namee

even in the official documentation there is an example that exactly answers the question
https://docs.unity3d.com/ScriptReference/Vector3.M...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question