V
V
Vlad228 Don't Say2021-01-13 22:36:54
Unity
Vlad228 Don't Say, 2021-01-13 22:36:54

How to make the correct movement of the road in the game?

Hello everyone, while creating a (next) game, I had a problem with the "endless road".
Specifically, it is expensive for the player, and he stands still (logically) and when she reaches the z = -40f coordinate, she teleports to the end of the road to the z = 140f coordinate. And when you set, for example, a speed of 10, after one (the cycle of this road (there are 9 of them)) gaps begin between the parts of the road and after a while they become wider and wider. The problem is that when it moves, the z coordinate shows these numbers: z = 123.06784 , z = 54.428342. And I need it to be: z = 23 , z = 106. I catch this coordinate like this:

if ( z < finish || z == finish)
        {      
            road.transform.position = new Vector3(0f, 0f, statrt); // start = 140f это начало
        }

And I want to have z = not float but int at any speed. Help please, otherwise I'm already tired of fixing this problem.
Here is the whole code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class moveplat : MonoBehaviour
{
    public int speed;
    public GameObject road;
    public float z;
    public float finish = -40f;
    public float start = 140f;
    
  
    void Start()
    {
        speed = PlayerPrefs.GetInt("DataRoadSpeed"); // это я беру скорость с меню
    }


    void Update()
    {

        road.transform.Translate(Vector3.back * speed * Time.deltaTime);// перемещение дороги
        z = road.transform.position.z; // узнаю координату z дороги

        if ( z < finish || z == finish) // пытаюсь словить дорогу вот-так
        {      
            road.transform.position = new Vector3(0f, 0f, start); // тепехаю её в конец
        }
    }
  
}

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