Why the object keep moving up nonstop and how to move it forward fast when it finished moving up?

59 views Asked by At

I want to move the object up smooth slowly from it's current position on y 50.01 to new position 51.255 But the object keep moving up nonstop.

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

public class RoboSphereWindowBreakInteraction : MonoBehaviour
{
    public Transform target;
    public AudioClip audioClip;
    public float speed;

    private bool hasStarted = false;
    private Animator anim;

    void Update()
    {
        if ((Input.GetKeyDown(KeyCode.B) || (hasStarted == true)))
        {
            float step = speed * Time.deltaTime; // calculate distance to move
            transform.position = Vector3.MoveTowards(transform.position, target.position, step);

            hasStarted = true;
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.name == "Square 1")
        {
            GetComponent<Rigidbody>().isKinematic = false;
            hasStarted = false;
            Destroy(GameObject.Find("Wall_Window_Long_03"));
        }
    }

    public void ActivateRoboSphere()
    {
        foreach(Transform child in transform)
        {
            if(child.name == "Camera")
            {
                RepositionCamera(child);
            }
        }

        anim = GetComponent<Animator>();
        anim.enabled = true;

        GetComponent<FPEInteractableActivateScript>().interactionString = "";
        FPEInteractionManagerScript.Instance.BeginCutscene();

        StartCoroutine(PlayAudio());
    }

    private void RepositionCamera(Transform camera)
    {
        var Eyes = GameObject.Find("eyeDome");

        camera.position = Eyes.transform.position + Eyes.transform.forward;
        camera.LookAt(Eyes.transform);
        camera.GetComponent<Camera>().enabled = true;
    }

    IEnumerator PlayAudio()
    {
        AudioSource audio = GetComponent<AudioSource>();

        audio.clip = audioClip;
        audio.Play();
        yield return new WaitForSeconds(audio.clip.length);

        var rotation = Quaternion.LookRotation(target.position - transform.position);


        StartCoroutine(Spin(3f, rotation, () =>
        {
            anim.SetBool("Roll_Anim", true);
        }));

        StartCoroutine(MoveFromTo(transform, transform.position, new Vector3(transform.position.x,
            transform.position.y + 51.255f, transform.position.z), 3f));
    }

    IEnumerator Spin(float lerpTime, Quaternion rotation, Action whenDone)
    {
        float elapsedTime = 0f;

        while (elapsedTime <= lerpTime)
        {
            transform.rotation = Quaternion.Slerp(transform.rotation, rotation, elapsedTime / lerpTime);
            elapsedTime += Time.deltaTime;
            yield return null;
        }

        whenDone?.Invoke();
    }

    // 51.255

    IEnumerator MoveFromTo(Transform objectToMove, Vector3 a, Vector3 b, float speed)
    {
        float step = (speed / (a - b).magnitude) * Time.fixedDeltaTime;
        float t = 0;
        while (t <= 1.0f)
        {
            t += step; // Goes from 0 to 1, incrementing by step each time
            objectToMove.position = Vector3.Lerp(a, b, t); // Move objectToMove closer to b
            yield return new WaitForFixedUpdate();         // Leave the routine and return here in the next frame
        }
        objectToMove.position = b;
    }
}

I did :

StartCoroutine(MoveFromTo(transform, transform.position, new Vector3(transform.position.x,
                transform.position.y + 51.255f, transform.position.z), 3f));

But the object keep moving up nonstop. Or at least very high and not like I wanted from 50.01 to 51.255 And when the object reaching the height of 51.255 then I want it to move fast smooth forward to target.

1

There are 1 answers

0
Hamid Yusifli On

In your code replace this part:

StartCoroutine(MoveFromTo(transform, transform.position, new Vector3(transform.position.x,
                transform.position.y + 51.255f, transform.position.z), 3f));

with this:

StartCoroutine(MoveFromTo(transform, transform.position, new Vector3(transform.position.x,
                51.255f, transform.position.z), 3f));