- Home /
I want to click on my screen and make my character move there, having trouble with raycasts.
So im trying to make a top down rpg style wc3 kind of project. So i want my character to follow my mouse when i click on the ground, however i am having porblems with Raycast as i am a noobie.
Heres the code. using UnityEngine; using System.Collections;
public class ClickToMove : MonoBehaviour {
public Transform movepoint;
float turnSpeed = 10.0f;
float moveSpeed = 1;
private bool isMoving = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Debug.DrawRay(transform.position, Vector3.down);
if(Input.GetMouseButtonDown(0))
{
//Here i check if i click the left mousbutton and where i click it.
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit = new RaycastHit();
if (Physics.Raycast(ray,out hit)) {
print ("Your Cursor hit at " + hit.point + "!");
GameObject toKill = GameObject.FindGameObjectWithTag("MoveLocation");
GameObject.Destroy(toKill);
Instantiate (movepoint, new Vector3(hit.point.x,hit.point.y + 1,hit.point.z), transform.rotation);
isMoving = true;
}
}
if (isMoving == true){
GameObject toMove = GameObject.FindGameObjectWithTag("MoveLocation");
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(new Vector3(toMove.transform.position.x, toMove.transform.position.y,toMove.transform.position.z) - new Vector3(transform.position.x,transform.position.y,transform.position.z)), turnSpeed * Time.deltaTime);
transform.position = Vector3.MoveTowards(transform.position, new Vector3(toMove.transform.position.x, toMove.transform.position.y, toMove.transform.position.z) , moveSpeed * Time.deltaTime);
if (Vector3.Distance(transform.position,toMove.transform.position) <= 1) {
isMoving = false;
print("Im here.");
}
}
}
}
i attach this script to my player and i create a empty gameobject that is called movepoint and a plane with the tag MoveLocation. I think i might have messed up on those so im gonna provide some pictures.
In the album theres also the error i get when i left click.
Answer by sarcasteak · May 08, 2014 at 06:39 PM
RaycastHit hit = new RaycastHit();
No need to do this you can just do
RaycastHit hit;
What line is your actual line 27? Is it if (Physics.Raycast(ray,out hit)) {
Or something else like this?
GameObject to$$anonymous$$ill = GameObject.FindGameObjectWithTag("$$anonymous$$oveLocation");
GameObject.Destroy(to$$anonymous$$ill);
Whatever is on that line is null, and is causing your problem.
Oh alright! Line 27 is Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
How did you see at what line the problem were? Also thankyou :D!
You probably don't have a camera tagged as main then, make sure your camera says "$$anonymous$$ainCamera" in top on inspector where it says Tag. It shows the line number in the console screens you provided. Null pointer means that the object it is trying to run the method on doesn't exist.
Thank you so much, this has been troubling me for quite some time even though i actually managed to figure out what the problem was when you said that line 27 was the problem. Im Super appreciative of you taking the time to answer this, thankyou:)!