Home > Mobile >  How can I be able to change the object starting position but also to update it's position when
How can I be able to change the object starting position but also to update it's position when

Time:10-04

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

public class MoveToTarget : MonoBehaviour
{
    public enum TransitionState
    {
        None,
        MovingTowards,
        Transferring
    }

    public Transform destinationTransform;
    public Transform navi;
    public Transform player;
    public bool isChild = false;
    public AnimationCurve curve = AnimationCurve.EaseInOut(0.0f, 0.0f, 1.0f, 1.0f);
    public float duration = 10.0f;
    public bool go = false;

    private float t;
    private Transform originTransform;
    private float timer;
    private TransitionState state = TransitionState.MovingTowards;
    private Vector3 originPosition;

    void Start()
    {
        t = 0.0f;

        curve.postWrapMode = WrapMode.Once;
        originPosition = transform.position;
    }

    void Update()
    {
        if (go)
        {
            switch (state)
            {
                case TransitionState.MovingTowards:
                    var v = destinationTransform.position - transform.position;
                    if (v.magnitude < 0.001f)
                    {
                        state = TransitionState.Transferring;
                        originTransform = destinationTransform;
                        timer = 0;
                        return;
                    }

                    t  = Time.deltaTime;
                    float s = t / duration;

                    transform.position = Vector3.Lerp(originPosition,
                        destinationTransform.position, curve.Evaluate(s));
                    break;

                case TransitionState.Transferring:
                    timer  = Time.deltaTime;
                    this.transform.position = Vector3.Lerp(originTransform.position, destinationTransform.position, timer);

                    if (timer >= 1.0f)
                    {
                        this.transform.parent = destinationTransform;
                        transform.localPosition = new Vector3(0, 0, 0);
                        isChild = true;

                        state = TransitionState.None;
                        this.enabled = false;
                        return;
                    }
                    break;

                default:
                    this.enabled = false;
                    return;
            }
        }
    }
}

I want to set the starting position before running the game in the editor when I drag the transform to some position when running the game it's changing back to the originPosition if I mark not to use the line at the Start()

//originPosition = transform.position;

Than it will start from the other side at all. I want it to start moving from where I set it at the start position.

Moving the line

originPosition = transform.position;

To the Update() is also not a solution.

CodePudding user response:

If transform.position returns something different from what you’ve set in edit mode, it’s affected by something else on Start() or Awake() in some script. Also it is possible, that some script sets public go variable to true on awake.

  • Related