- Home /
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.
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.
so how would i get the playerDist before going into the if statement?
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.
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?