Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Haxxero · May 08, 2014 at 06:30 PM · raycastplayermouse

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.

http://imgur.com/a/J99wo

In the album theres also the error i get when i left click.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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;
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Haxxero · May 08, 2014 at 06:45 PM 0
Share

Okay Thankyou! However this does not solve my problem.

avatar image sarcasteak · May 08, 2014 at 06:53 PM 0
Share

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.

avatar image Haxxero · May 08, 2014 at 06:57 PM 0
Share

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!

avatar image sarcasteak · May 08, 2014 at 07:02 PM 0
Share

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.

avatar image Haxxero · May 08, 2014 at 09:02 PM 0
Share

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:)!

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Make the player shoot towards a mouse click 2d Platformer 1 Answer

how can i Find an object height, while click on that object? 1 Answer

Making an object clickable 2 Answers

Raycast Destroys player. 1 Answer

Getting a character to fly(follow cursor in 2D) in Unity4.3 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges