- Home /
Click to move issue.
I'm trying to make a script that moves the character where the player clicks. I have it mostly working apart from making the character continue moving after the mouse button is released. I also need the player to stop moving upon reaching it's destination.
Would appreciate if someone could point out what I'm doing wrong or missing.
using UnityEngine;
using System.Collections;
public class InputManager : MonoBehaviour {
Ray ray = new Ray();
RaycastHit hit = new RaycastHit();
public float playerSpeed = 5;
private bool isMoving;
public GameObject movingPivot; //empty object placed exactly beneath player for position
void Start(){
movingPivot = GameObject.Find("MovingPivot");
}
void Move(){
transform.Translate((hit.point - movingPivot.transform.position).normalized * playerSpeed * Time.deltaTime);
}
void Update (){
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if(Input.GetButtonDown("Fire1")){
if(Physics.Raycast (ray, out hit, 1000)){
if(hit.collider.gameObject.tag == "Terrain"){
isMoving = true;
Move ();
}
}
}
}
}
Answer by Timer · Jan 14, 2013 at 11:14 AM
I think, I got the problem, the isMoving boolian you have created you are turning that on but not using it.
Inside the move function do something so that like
if(isMoving){
translate
}
and don't forget the turn the isMoving off when you have reached your target,
If I had to do this I would simply make a prefab that has trigger component attached to it and instantiating it at the hit point of the ray so every time user clicks the mouse it creates the small trigger in the terrain and small script would be like this:
void OnTriggerEnter(Collider other){
if(other.comparetag("Player")) {
other.getcomponent().isMoving = false;
destroy(this.gameObject);
}
}
Answer by CodeMasterMike · Jan 14, 2013 at 08:22 AM
I would use Slerp/Lerp instead to move between two different positions. Something like this (pseudo-code!):
Vector3 goalPosition = Vector3.zero;
Vector3 startPosition = Vector3.zero;
float timeMovement = 10.0f;
float currentTimeProcentage = 0.0f;
float startTime = 0.0f;
float endTime = 0.0f;
Update
{
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if(Input.GetButtonDown("Fire1"))
{
if(Physics.Raycast (ray, out hit, 1000))
{
if(hit.collider.gameObject.tag == "Terrain")
{
goalPosition = hit.point;
startPosition = this.transform.position;
isMoving = true;
startTime = Time.time;
endTime = (startTime + timeMovement);
}
}
}
if(isMoving == true)
{
Move ();
}
}
void Move()
{
currentTimeProcentage = (endTime / Time.time);
transform.Translate.position = Vector3.Slerp(startPosition, goalPosition, currentTimeProcentage);
if(currentTimeProcentage >= 1.0f)
{
isMoving = false;
}
}
As you can see in the code, I update the start and goal position every time the button is pressed so that the Slerp/Lerp function knows where to start and where to go to. I also reset the procentage counter for the Slerp/Lerp function.
This is a very simple example I just wrote out of my head. It is not tested so don't just copy it. But it hopefully shows a way for you to easially move a character between two points when the mouse button is pressed.
Good luck!
Your answer
![](https://koobas.hobune.stream/wayback/20220613090915im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Input System binding with one modifier composite value problem 1 Answer
C# Touch Script - Fix GameObject touch. 0 Answers
Using Oculus headgear position as input. 0 Answers