K
K
kokapuk2021-04-06 20:29:41
Unity
kokapuk, 2021-04-06 20:29:41

Why does Vector3.MoveTowards move inaccurately?

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

public class Mover : MonoBehaviour
{
    public float speed;
    private Vector3 target;
    public float xOffest;

    private void Start()
    {
        target = transform.position;
    }

    private void FixedUpdate()
    {
        transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
        if (Input.GetKeyDown(KeyCode.Z))
        {
            target = new Vector3(transform.position.x + xOffest, transform.position.y, transform.position.z);
        }
        else if (Input.GetKeyDown(KeyCode.X))
        {
            target = new Vector3(transform.position.x - xOffest, transform.position.y, transform.position.z);
        }
    }
}

in the inspector, I set speed = 5, xOffset = 1, but why when I move the cube on the buttons, it shifts inaccurately, it shifts by 1 integer and tenths or hundredths, but in my case this is critical, how can I fix this?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question