Search Unity

Need help with my Move Code.

Discussion in 'Animation' started by Dnielsen, Oct 1, 2019.

  1. Dnielsen

    Dnielsen

    Joined:
    Sep 20, 2019
    Posts:
    2
    Hi All.
    i want my Gameobject to move smoothly to the waypoint.

    If i change the code so i use a Letter Like "A" then it works. but then i use Buttons. it will not go smoothly to the waypoint. but it jumps a bit. and then i press again it jumps a bit more, until it hit my waypoint.
    how does i make the buttonpress move smoothly.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using UnityEngine.EventSystems;

    public class WaypointMove : MonoBehaviour
    {
    public Button button1;
    public GameObject[] waypoints;
    int current = 0;
    float rotspeed;
    public float speed;
    float WPradius = 1;

    void OnEnable()
    {
    button1.onClick.AddListener(() => buttonCallBack(button1));
    }

    // Update is called once per frame
    private void buttonCallBack(Button buttonpressed)

    {
    if (Vector3.Distance(waypoints[current].transform.position, transform.position) < WPradius)
    {
    current++;
    if(current >= waypoints.Length)
    {
    current = 0;
    }
    }

    transform.position = Vector3.MoveTowards(transform.position, waypoints[current].transform.position, Time.deltaTime * speed);
    }
    }