- Home /
Mouse click movement script issue.
Hello, I'm quite new to both coding and unity, yet I've decided to make an rpg game with some of my friends. I just started and I have an issue regarding character movement. I want to make my character move like in common isometric rpg games (like diablo, nvn etc).
So here's what I've done so far for this script:
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour {
public float _Distance;
public float _Speed = 10f;
// Use this for initialization
void Start () {
}
void LateUpdate () {}
// Update is called once per frame
void Update () {
Ray ray = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay(ray.origin, ray.direction * 9999999, Color.red);
RaycastHit hit;
Physics.Raycast(ray, out hit);
if (Input.GetMouseButtonDown(0)){
Vector3 _NewPosition = hit.point;
if (hit.collider.tag == "passable"){
this.gameObject.transform.LookAt(hit.point);
// _Distance = Vector3.Distance(this.gameObject.transform.position, hit.point);
this.gameObject.transform.position = Vector3.MoveTowards(this.gameObject.transform.position, _NewPosition, _Speed * Time.deltaTime);
}
}
}
}
Thing is that it moves my character for 1 unit of _Speed, instead of moving it to the end point. I've tried to add "while _Distance <= n" cycle, but it just freezed the program. Could you please help me with this?
Thanks a lot, both of you. I just realized huge mistake I've made - I put "$$anonymous$$oveTowards" into "if" section, thus object moved only when key was pressed. Silly me, sometimes I should analyze own code more.
Answer by Adamcbrz · Aug 05, 2013 at 06:24 PM
I have gone through and adjusted your code. I have left comments to help explains some of the changes.
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour
{
public float _Distance;
public float _Speed = 10f;
//Declare out side of update so we can retain it between frames
private Vector3 _NewPosition;
void Start ()
{
// set _NewPosition to the character position so we don't start moving torwards (0,0,0)
_NewPosition = transform.position;
}
void Update ()
{
if (Input.GetMouseButtonDown (0)) {
//Don't raycast every frame but only when you need it
Ray ray = Camera.mainCamera.ScreenPointToRay (Input.mousePosition);
Debug.DrawRay (ray.origin, ray.direction * 9999999, Color.red);
RaycastHit hit;
// put in if statement just in case we don't hit anything
if (Physics.Raycast (ray, out hit)) {
if (hit.collider.tag == "passable") {
//move into check so we don't change position unless it meets your rules
_NewPosition = hit.point;
}
}
}
// you don't need the this.gameObject. this is assumed and gameObject is a shortcut just like transform
// so you can use the transform shortcut
transform.LookAt (_NewPosition);
transform.position = Vector3.MoveTowards (transform.position, _NewPosition, _Speed * Time.deltaTime);
}
}
Answer by nixcs2512 · Aug 05, 2013 at 06:25 PM
You need 1 Vector3 variable to store the hit point by RayCast to let your character move to it.The code maybe looks like:
public Vector3 destination;
...
void Update(){
...
if (Input.GetMouseButtonDown(0))
{
destination = hit.point;
}
this.gameObject.transform.LookAt(destination);
this.gameObject.transform.position = Vector3.MoveTorwards(this.gameObject.transform.position, _NewPosition, _Speed * Time.deltaTime);
}
Your while loop don't have an "exit" so it will continue loop forever.
Your answer
Follow this Question
Related Questions
Smooth touch for map 0 Answers
2D Vector Movement,Vectorel Movement with Buttons 2 Answers
Roll a ball to fixed distance 3 Answers
Movement on prefab objects 0 Answers
Click to move script help 1 Answer