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 Pani · Aug 09, 2010 at 03:44 PM · charactercontrollerclicktomove

Click To Move (should be an easy one)

I thought I'd get my hands dirty with some Unity Javascript but I seem to be falling at the first hurdle. I blatantly and unashamedly took this code from these very forums but need some help getting it to run.

I get an Object ref not set to object error around these lines: var ray = Camera.main.ScreenPointToRay (Input.mousePosition); if(Physics.Raycast(Camera.main.transform.position, ray.direction, hit, 1000, layerMask)) but can't for the life of me figure it out. I've attached this code to a character placeholder (couple of bricks) with the hopes of having my 3rd person click to move.

private var move : boolean = false;

function Update () { if (Input.GetButtonDown ("Mouse Left")) { var ray = Camera.main.ScreenPointToRay (Input.mousePosition); if(Physics.Raycast(Camera.main.transform.position, ray.direction, hit, 1000, layerMask)) { move = true ; } }

 if(move) 
 { 
     var wantedPosition= Vector3(hit.point.x, hit.point.y, hit.point.z); 
     transform.LookAt(wantedPosition); 
     transform.position = Vector3.Lerp(transform.position, hit.point, Time.time/200); 

     if(Vector3.Distance(transform.position, wantedPosition) < 0.01) 
     { 
         move = false; 
     } 
 } 

}

Any clues?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Eric5h5 · Aug 09, 2010 at 03:57 PM

Camera.main needs a camera tagged "MainCamera" in order to find anything.

Comment
Add comment · Show 1 · 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 Pani · Aug 09, 2010 at 04:02 PM 0
Share

And where should this $$anonymous$$ainCamera tagged camera be in relation to my Player Placeholder?

avatar image
0
Best Answer

Answer by Badger · Aug 09, 2010 at 08:21 PM

I got it to work with the following adjustments:

  • define a RaycastHit object for info to be stored in
  • get rid of the layermask (or define on)
  • also, you need an object where you click the mouse, so it can get a position off. Terrain won't do apparently...

    private var move : boolean = false; var hit : RaycastHit;

    function Update () { if (Input.GetButtonDown ("Fire1")) { var ray = Camera.main.ScreenPointToRay (Input.mousePosition); if(Physics.Raycast(Camera.main.transform.position, ray.direction, hit, 1000)) { move = true ; } }

    if(move) { var wantedPosition= Vector3(hit.point.x, hit.point.y, hit.point.z); transform.LookAt(wantedPosition); transform.position = Vector3.Lerp(transform.position, hit.point, Time.time/200);

    if(Vector3.Distance(transform.position, wantedPosition) < 0.01) 
    { 
        move = false; 
    } 
    

    }

    }

    Comment
    Add comment · Show 2 · 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 skovacs1 · Aug 09, 2010 at 10:34 PM 0
    Share

    If this answered your question, please mark it as the accepted answer.

    avatar image Pani · Aug 11, 2010 at 02:28 PM 0
    Share

    Your information got me the furthest I've gotten so far, so thank you very much.

    (particularly the bit about it not working on the terrain which baffles me).

    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

    No one has followed this question yet.

    Related Questions

    Click to move around obstacles 2 Answers

    Click to Move 2 Answers

    Character Controller moves forever after colliding with object 1 Answer

    Click to move a character in a terrain 1 Answer

    How to move to specific point with character controller 1 Answer


    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