K
K
Konstantin Shkilev2014-09-01 14:26:22
C++ / C#
Konstantin Shkilev, 2014-09-01 14:26:22

Why doesn't movement work in Unity3d?

Good time of the day. Next problem: we need to move an object between 3 points X1 (-1.0, -1.5, 0.5), X2 (0.0, -1.5, 0.5) and X3 (1.0, -1.5, 0.5) . And do it smoothly, not just set a new position, but with interpolation between two points. But when using Vectror3.Lerp(), the desired coordinate does not reach the maximum. Those. if we move from X2 to X1 , then x coordinate = -0.9999. I understand this is due to the use of float. But that's why the conditions don't work. What should be done? Round up. But what if we use Vector3.Lerp(), or the gliding approach itself is wrong and my code is shit code. Then how else to do it?

using UnityEngine;
using System.Collections;

public class pController : MonoBehaviour {

  public float speed;
  private Vector3 left;
  private Vector3 right;
  private bool leftOn = false;
  private bool rightOn = false;
  private Vector3 target;
  // Use this for initialization]
  void Start () {
  }
  // Update is called once per frame
  void Update () {
    if (transform.position.x == 0.0f) {
            left = new Vector3 (-1.0f, -1.5f, 0.5f);
            right = new Vector3 (1.0f, -1.5f, 0.5f);
        }
    if (transform.position.x == -0.1f) {
            right = new Vector3 (0.0f, -1.5f, 0.5f);
        }
    if (transform.position.x == 1.0f) {
      left = new Vector3(0.0f, -1.5f, 0.5f);
        }
    if(Input.GetKeyDown(KeyCode.A) && transform.position.x!=-1.0f && !leftOn && !rightOn){
      leftOn = true;
    } else if(Input.GetKeyDown(KeyCode.S) && transform.position.x!=1.0f && !rightOn && !leftOn){
      rightOn = true;
    }
    if(leftOn){
      target = transform.position;
      transform.position = Vector3.Lerp(target, left, Time.deltaTime*speed);
    }
    if(rightOn){
      target = transform.position;
      transform.position = Vector3.Lerp(target, right, Time.deltaTime*speed);
    }
    if ((transform.position.x == 0.0f) || (transform.position.x == -1.0f)) {
            leftOn = false;
        }
    if ((transform.position.x == 0.0f) || (transform.position.x == 1.0f)) {
            rightOn = false;
        }
  }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Daniil Basmanov, 2014-09-01
@shkilevk

Don't compare floats like this if you need precision. Correct like this:
The accuracy threshold can be anything, it must be selected for a specific situation, for example 0.001

I
Ivan Khlopianik, 2014-09-01
@ping_rulezzz

You misunderstood how the function works. Briefly from the help:
static Vector3 Lerp(Vector3 from, Vector3 to, float t);
t = 0 returns "from", t = 1 returns "to", when t = 0.5 returns the point midway between from and to.
And in your code, you always only get closer to the desired value.
See usage example: docs.unity3d.com/ScriptReference/Vector3.Lerp.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question