Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
236 views
in Technique[技术] by (71.8m points)

c# - Moving from one place to another over time (or rotating) in Unity

I have a problem with moving from one place to another in Unity over time. I would like my character to move from current position to current + 1 on Y. Unfortunately, looks like it does not get the current position properly or something, since if I debug what I wrote, it says that the magnitude is always 1, so the point is moving with me. Shouldn't it just check the current position and add 1 to Y, move to that position and then check again? I have no idea what's wrong with this code, or if it's strictly connected with how Unity checks positions and things in real time?

 public bool moving = false;
 private Vector3 dir;
 void FrontMovement()
 {
     Vector3 movement = new Vector3(-Mathf.Sin(transform.eulerAngles.z * Mathf.PI / 180), Mathf.Cos(transform.eulerAngles.z * Mathf.PI / 180), 0f); // always moving front, even after rotation

     if (moving == false)
     {
         dir = movement - transform.position;
         moving = true;
         return;
     }
     transform.Translate(dir.normalized * Time.deltaTime);
     if(dir.magnitude <= Time.deltaTime)
     {
         Debug.Log("Finished movement");
         moving = false;
     }
 }
 void FixedUpdate()
 {
     Debug.Log(dir.magnitude);
     FrontMovement();
 }

I would also like to know how to do rotations over time.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

https://docs.unity3d.com/ScriptReference/Vector3.Lerp.html

lerp also works for rotations

// Movement speed in units per second.
public float speed = 1.0F;

// Time when the movement started.
private float startTime;

// Total distance between the markers.
private float journeyLength;

void StartMoving() {
    // Keep a note of the time the movement started.
    startTime = Time.time;

    Vector3 modifiedPosition = transform.position;
    transform.position.y += 1.0f;

    // Calculate the journey length.
    journeyLength = Vector3.Distance(transform.position, modifiedPosition.position);

    moving = true;
}

// Move to the target end position.
void Update()
{
    if (moving) {
    // Distance moved equals elapsed time times speed..
    float distCovered = (Time.time - startTime) * speed;

    // Fraction of journey completed equals current distance divided by total distance.
    float fractionOfJourney = distCovered / journeyLength;

    // Set our position as a fraction of the distance between the markers.
    transform.position = Vector3.Lerp(startMarker.position, endMarker.position, fractionOfJourney);

        if (fractionOfJourney >= 1.0f) {
        moving = false;
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...