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 RaulDiaz · Oct 16, 2010 at 12:00 AM · guidistanceif-statementsray

SORPG: Distance problem?

So im making a SORPG (singpleplayer offline role playing game) and im currently working on a harvest tree type of resource gathering.

What i m trying to do is when you are within 8 "distance" from the tree you can bring up the menu by left clicking (right click is reserved for my camera script)

upon clicking ( and i know that he, meaning my player1, is close enough to the tree the menu does not appear)

Code:

//Menu Settings var MenuActive1:boolean = false; var timer : float = 5;

//Find closest Tree/Click on tree? function Update () { //Finding Closest Tree var waypoints: GameObject[] = GameObject.FindGameObjectsWithTag("ClickAble"); var closest: GameObject; var closestDist = Mathf.Infinity;

 for (waypoint in waypoints) { 
     var dist = (transform.position - waypoint.transform.position).sqrMagnitude;         
         if (dist < closestDist) { 
             closestDist = dist; 
             closest = waypoint; 
         } 
 }

 //LookAt Closest (not using)
 //transform.LookAt(closest.transform); 
 ///////////////////////////////////////
 //Click Code
 if (Input.GetMouseButton(0)) {
     var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     var hit: RaycastHit;
     var wantedPosition= Vector3(hit.point.x, hit.point.y, hit.point.z);
     var playerDist =    (transform.position - wantedPosition).magnitude;
         Debug.Log( playerDist ); // (display distance from nearest "clickable" object)
     if(playerDist <= 10 && timer == 5 ){        
     timer = 0;
         if (Physics.Raycast(ray, hit)) {                
             //clickable object names
             switch ( hit.collider.name ){
                 case "HarvestTree": //(going to add more case "object name here as progress")               
                         MenuActive1 = !MenuActive1;             //(not showing up!!!)           
                         break; 
             }
         }
     }
     else {timer += 1;}          
 }   

}

function OnGUI () {

 if (MenuActive1 == true) 
 {      
    if (MenuActive1 == true){ 
      // Make a background box 
    GUI.Box (Rect ((Screen.width-31)/2,190,220,180), "Large Tree\n\n   \n  .\n\n  .");
         //Second button returns to main menu 
         if (GUI.Button (Rect((Screen.width-31+200)/2,285,80,20), "Accept")) { 
         MenuActive1=!MenuActive1; 

         } 
         //Second button returns to main menu 
         if (GUI.Button (Rect((Screen.width-31+200)/2,320,80,20), "Cancel")) { 
         //Closes the menu
         MenuActive1=!MenuActive1; 
         } 
    } 
 } 

}

The var playerDist = (transform.position - wantedPosition).magnitude; is calculating the distance between where the ray hits and the player... but for some reason does not activate the if statement below it. the timer is always at 5 unless you click something so thats not the problem.

Thanks for the help, Raul

PS. don't point out horrible structure im a newbie.

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 Murcho · Oct 16, 2010 at 12:30 AM

You're testing against nothing with your magnitude, as wanted position hasn't been setup properly. You're using the values from hit, however you haven't performed the Raycast at that point, so hit doesn't actually contain any data yet. I believe by default that means you're actually always setting wanted position to 0,0,0 which will always fail your distance test.

Comment
Add comment · Show 3 · 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 RaulDiaz · Oct 16, 2010 at 12:36 AM 0
Share

so how would i get the playerDist before going into the if statement?

avatar image Jesse Anders · Oct 16, 2010 at 03:53 AM 0
Share

Remember that aside from statements that change the flow of control, the code will be executed in order, statement by statement. In order to get your code to do what you're wanting, you need to think about the logical flow of events and what depends on what, and then write your code accordingly. I'm not completely clear on what you're trying to do, but just as an example, if whether the menu comes up is dependent on whether the item that was clicked is within a certain distance of the player, you need to deter$$anonymous$$e what item was clicked, then check the distance to the player.

avatar image RaulDiaz · Oct 16, 2010 at 07:53 PM 0
Share

I understand but how would i go about doing this i rewrote the code but it no longer works. it only outputs 0 as the distance? an ideas?

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

Help! Hiding/showing GUITextures! 1 Answer

Shooting at mousePosition 1 Answer

How can i tell the distance from a raycast? 1 Answer

Making a button desplay after pressing a button? 2 Answers

Add a Player distance on GUI Display Script + on Rigibody move? 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