Noah Roth
Noah Roth

Reputation: 276

Simple click-to-move script not working?

Here is my code:

public class CharacterController : MonoBehaviour
{

    private Vector3 _startLocation = Vector3.zero;
    private Vector3 _currentLocation = Vector3.zero;
    private Vector3 _endLocation = Vector3.zero;
    private bool _isMoving = false;
    private float _distanceToTravel;

    private float _startTime;

    public float Speed = 1.0f;

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {

            Debug.Log("Left mouse button clicked");

            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            RaycastHit hit;

            if (Physics.Raycast(ray, out hit))
            {
                if (hit.collider.gameObject.CompareTag("Ground"))
                {
                    _startLocation = transform.position;
                    _endLocation = hit.point;
                    _isMoving = true;
                    _startTime = Time.time;
                    _distanceToTravel = Vector3.Distance(_startLocation, _endLocation);

                    Debug.Log(string.Format("Ground has been hit: Start: {0}, End: {1}", _startLocation.ToString(), _endLocation.ToString()));
                }
            }
        }

        if (_isMoving)
           Move();
    }

    void Move()
    {
        float timeElapsed = (Time.time - _startTime) * Speed;
        float t = timeElapsed / _distanceToTravel;

        _currentLocation = Vector3.Lerp(_startLocation, _endLocation, t);

        transform.Translate(_currentLocation);

        if (_currentLocation == _endLocation)
        {
            Debug.Log(string.Format("Destination reached ({0})", _endLocation.ToString()));

            _isMoving = false;
        }
    }
}

I read the documentation on the Vector3.Lerp function, as well as the Physics.Raycast function, and ended up with this code.

The debug console confirms that the Ground has been hit, but my capsule starts moving upwards in the Y direction and never stops!

I'm still very new to Unity and game development in general, so I'm still learning, but any pointers on what I'm doing wrong?

Upvotes: 0

Views: 151

Answers (2)

Jesper
Jesper

Reputation: 422

it is moving in the Y because you use transform.Translate.

transform.Translate is moving it, so if you did transform.Translate(0, 0, 10) it would move in the z, and if you did transform.Translate(0, 10, 10) it will move in the y and z direction.

So to fix this i will show you 2 ways:

1) Using Vector3.Lerp:

transform.position = Vector3.Lerp(_startLocation, _endLocation, t);

2) Using MoveTowards:

transform.position = Vector3.MoveTowards(transform.position, _endLocation, Speed * Time.deltaTime);

In the first Vector3.Lerp is used, and i see you use it too

_currentLocation = Vector3.Lerp(_startLocation, _endLocation, t);

So you could do either this:

transform.position = Vector3.Lerp(_startLocation, _endLocation, t);

or this

transform.position = _currentLocation;

Both will do the same because you assigned _currentLocation to

Vector3.Lerp(_startLocation, _endLocation, t);

And you can read about MoveTowards here http://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html

Upvotes: 1

Rasa Mohamed
Rasa Mohamed

Reputation: 892

Check the Comments line You will understand what I had edited to bring the Solution. It works well for me

void Start()
{
    _startLocation = transform.position; // Changed Here to Initialize once  
}
    void Update()
{
    if (Input.GetMouseButtonDown(0))
    {         
        Debug.Log("Left mouse button clicked");
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit))
        {
            if (hit.collider.gameObject.CompareTag("Ground"))
            {   
                print(hit.point);
                //**Here you mentioned _StartLocation = transform.position
                //this instantly changes the starting point Every time so
                //if SP changes then totally changed in Translation.*** 
                _endLocation= hit.point;
                _isMoving = true;
                _startTime = Time.time;
                _distanceToTravel = Vector3.Distance(_startLocation, _endLocation);
            }
        }
    }
    if (_isMoving)
    {
         Move();
    }

}

void Move()
{
    float timeElapsed = (Time.time - _startTime) * Speed;
    float t = timeElapsed / _distanceToTravel;

    _currentLocation = Vector3.Lerp(_startLocation, _endLocation, t);
    transform.position = Vector3.Lerp(_startLocation, _endLocation, t);
    _startLocation = _endLocation;
                 //*** This line is for Next Mouse
                 //click. So every next click StartPoint is Current Click
                //EndPoint***
    if (_currentLocation == _endLocation)
    {
        Debug.Log(string.Format("Destination reached ({0})", _endLocation.ToString()));
        _isMoving = false;
    }
}
}

Upvotes: 0

Related Questions