Vector3.MoveTowards () не работает, не можете это исправить? - PullRequest
0 голосов
/ 20 марта 2019

Я действительно новичок в кодировании, и английский не является моим основным языком, так что простите за любые глупые ошибки и мой английский.

Я работаю над системой движения, которая перемещает игрока в точку щелчка, кажется, что все в сценарии работает, но во вкладке игры игрок вообще не двигается. Я уже прочитал много информации о Vector3.MoveTowards(), но у меня ничего не получалось.

Vector3 currentpos;
Vector3 targetpos;

bool ismoving = false;

public float speed = 10f;


void Update()
{
    currentpos = transform.position;
    move();

    if (Input.GetMouseButtonDown(0))
    {
        click();
    }

}


void click()                         //get the position of the click using a raycast//
{
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit hit;

    if(Physics.Raycast(ray, out hit))
    {
        targetpos = hit.point;
        targetpos.y = 0f;            //only whants to move the player in the x and z axis//


        ismoving = true;
        move();
    }
}


void move()
{
    if (ismoving==true)
    {
        if (Vector3.Distance(currentpos,targetpos)>0.1f)  //if distance between current position and target position is greater than 0.1f player should move//
        {
            currentpos = Vector3.MoveTowards(currentpos, targetpos, speed * Time.deltaTime);

            Debug.Log("Current Position is:" + currentpos + " and target position is" + targetpos);
        }

        if(Vector3.Distance(currentpos,targetpos)<0.1f)
        {
            ismoving = false;
        }
    }
}

В консоли текущая позиция и целевая позиция правильные, но игрок не двигается.

1 Ответ

0 голосов
/ 20 марта 2019

Хорошо, вы изменяете значение currentpos, но никогда не устанавливаете его обратно на transform.position, например, здесь

currentpos = Vector3.MoveTowards(currentpos, targetpos, speed * Time.deltaTime);
transform.position = currentpos;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...