x


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.

more ▼

asked Oct 16 '10 at 12:00 AM

RaulDiaz gravatar image

RaulDiaz
2 3 3 5

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Oct 16 '10 at 12:30 AM

Murcho gravatar image

Murcho
2.7k 12 23 53

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

Oct 16 '10 at 12:36 AM RaulDiaz

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 determine what item was clicked, then check the distance to the player.

Oct 16 '10 at 03:53 AM Jesse Anders

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?

Oct 16 '10 at 07:53 PM RaulDiaz
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3811
x373
x155
x84

asked: Oct 16 '10 at 12:00 AM

Seen: 600 times

Last Updated: Nov 10 '10 at 10:47 PM