Multiple Interaction
Goto page 1, 2  Next
 
Post new topic   Reply to topic    Unity Community Index // Scripting
View previous topic :: View next topic  
Author Message
linkthewise



Joined: 26 Sep 2009
Posts: 74

PostPosted: Mon Oct 26, 2009 5:33 am    Post subject: Multiple Interaction Reply with quote
I got the following code:
Code:
var interaction;
function CloseEnough(interactTag : String, distToInteract : float) : boolean {
   var allObjs : GameObject[] = GameObject.FindGameObjectsWithTag(interactTag);
   
   for (var obj : GameObject in allObjs) {
      var pos = obj.transform.position ;
      var dist = Vector3.Distance(pos, transform.position);
       
      if (dist <= distToInteract)
         return true;
   }
   
   return false;
}

function Update() {
   if(CloseEnough("People", 80.0)){
   SendMessage ("Focus", true);
   interaction=true;}
   else
   {SendMessage ("Focus", false);
   interaction=false;}

  // if(CloseEnough("Ladder", 0.1)) {
      // code to interact with ladders
  // }
}
function Focus (target1 : boolean) {
    var people=GameObject.FindWithTag ("People");
   if (target1==true)
people.light.enabled=true;
else
people.light.enabled=false;
}

I want to have multiple instances, so if I closer to an object with the people tag, I can interact. The problem appears when I have to instances with the tag in the range of action, how can I find wich object is closer, so I can locked it and interact.
Back to top
View user's profile Send private message
dreamora



Joined: 05 Apr 2008
Posts: 8170
Location: Zürich, Switzerland

PostPosted: Mon Oct 26, 2009 6:09 am    Post subject: Reply with quote
check all and find the nearest
_________________
Available for contract work at fair rates
Technologies: Unity Pro, iPhone Advanced, ...
Site: GayaSoft & Home of PatchLaunch.
Blogs:Company & Another World
Back to top
View user's profile Send private message Visit poster's website
linkthewise



Joined: 26 Sep 2009
Posts: 74

PostPosted: Mon Oct 26, 2009 6:39 am    Post subject: Yeah, but I dont know how to do that. Reply with quote
Yeah thats the idea, but I don't know how to script that.
Back to top
View user's profile Send private message
dreamora



Joined: 05 Apr 2008
Posts: 8170
Location: Zürich, Switzerland

PostPosted: Mon Oct 26, 2009 7:09 am    Post subject: Reply with quote
you would find out all objects that are of interest and then calculate the distance between them basing on their positions.

What you need to check in the script reference for the second part is Transform and Vector3

_________________
Available for contract work at fair rates
Technologies: Unity Pro, iPhone Advanced, ...
Site: GayaSoft & Home of PatchLaunch.
Blogs:Company & Another World
Back to top
View user's profile Send private message Visit poster's website
linkthewise



Joined: 26 Sep 2009
Posts: 74

PostPosted: Tue Oct 27, 2009 6:47 am    Post subject: As you can see Reply with quote
In my code with some help I manage to get the distance of one object, with an specific tag, the problem appear when there are multiple objects close with the same tag. As you sujest I was thinking in an array, thus I it seams cannot reach that with my code. Could anyone ilustrate that, i was trying with raycasting, but I dont know how to first place distance and name in an array. I've seen some FPS tutorials for targeting and lock on, the problem is that my game is a third person, and I use the wiimote for the targeting.
Back to top
View user's profile Send private message
andeeee
Forum Moderator


Joined: 19 Jul 2005
Posts: 2919
Location: Blackpool, United Kingdom

PostPosted: Tue Oct 27, 2009 6:01 pm    Post subject: Re: Multiple Interaction Reply with quote
Your CloseEnough function is already almost what you want. You just need to modify it to keep track of the closest element so far:-
Code:
function CloseEnough(interactTag : String, distToInteract : float) : boolean {
   var closest: float = 1000000.0;
   var closestName: string = "";
   var allObjs : GameObject[] = GameObject.FindGameObjectsWithTag(interactTag);
   
   for (var obj : GameObject in allObjs) {
      var pos = obj.transform.position ;
      var dist = Vector3.Distance(pos, transform.position);
       
       if (dist < closest) {
            closest = dist;
            closestName = obj.name;
       }
   }
    if (dist <= distToInteract)
         return closestName;
    else
         return "";
}

_________________
None of you understand... I'm not logged-in here with you - YOU'RE LOGGED-IN HERE WITH ME!
Back to top
View user's profile Send private message
linkthewise



Joined: 26 Sep 2009
Posts: 74

PostPosted: Wed Oct 28, 2009 5:51 pm    Post subject: Reply with quote
I try to implement what andee saids, but I got some warnings, so I modify the code to something like this

Code:

var interaction;
var closestName:String;
function CloseEnough(interactTag : String,  distToInteract : float) : boolean{

 
   var allObjs : GameObject[] = GameObject.FindGameObjectsWithTag(interactTag);
   
   for (var obj : GameObject in allObjs) {
      var pos = obj.transform.position ;
      var dist = Vector3.Distance(pos, transform.position);
     if (dist<=distToInteract)
     closestName = obj.name;
     else
     closestName=" ";
   }
   
    if (dist <= distToInteract)
       return true;
    else
         return false;

}

function Update() {
print(closestName);
   if(CloseEnough("People",50.0)){

   
   interaction=true;}
   else
   {

   interaction=false;}

  // if(CloseEnough("Ladder", 0.1)) {
      // code to interact with ladders
  // }
}

The problem is that if I got two objects with the People tag, I can only se the last object created name in the print, for example I got a capsule, and is the only object with the tag, so it works I got the print right. But if I create a Box with the same tag, I get close and the box name is print, but if I get close to the capsule, doesnt print anything.
Back to top
View user's profile Send private message
sinisa



Joined: 30 Oct 2009
Posts: 18

PostPosted: Sat Oct 31, 2009 3:03 am    Post subject: Reply with quote
I think you have to check for all objects around fixed distance and then filter what you need.

checkaround(distance){
return names
}

update(){
if checkaround(5.0=="people"){
for each obj in people{
which one has smallest distance
}
}
}


something like that Smile
Back to top
View user's profile Send private message
linkthewise



Joined: 26 Sep 2009
Posts: 74

PostPosted: Sat Nov 07, 2009 7:03 am    Post subject: I've been trying the filtering as you suggest, but Reply with quote
I can get this code to work properly, sinisa you are saying that my first function "closest" seams to be right, but I need to do a filtering, I tried to implement something like you suggest, but I cant make it worked.
What I tryed is to make another function, that do the filtering, like the checkaround that you suggest, then in my cycle first I compare distance with function CloseEnough and then Checkaround, but I only get null in the print of the name.
I know I have to first with my function CloseEnough check if the player in the range of interaction, if this is true I have to use CheckAroud, wich its a filter where I store [name,distance], and then compare within this array, but I cant make this algorythm, could someone help me?
Back to top
View user's profile Send private message
linkthewise



Joined: 26 Sep 2009
Posts: 74

PostPosted: Fri Dec 11, 2009 7:05 am    Post subject: I've been trying to make mods to my code Reply with quote
I've been trying to mod my code, but I couldn't make it work. Does anyone have another way to do it?. What I wanto to achieve is: When I press a key change to first person, in this view a target is created, but when an enemy is close and I press the same key I want to lock the target on the enemy. Something like LOZ, please help me Im really stuck in here
Back to top
View user's profile Send private message
linkthewise



Joined: 26 Sep 2009
Posts: 74

PostPosted: Fri Dec 18, 2009 1:12 am    Post subject: Reply with quote
I see what I need but I cant implement it it seams that what I need is create a temporal distance and a temporal object reference, so if im close to an object, the function is activated, but if Im close to two reference find the closest of the two objects.
Here is the code I have:

Code:

static var interaction;
var others;
var other;

var cross:Transform;

function CloseEnough(interactTag : String, distToInteract : float) : boolean {
   var allObjs : GameObject[] = GameObject.FindGameObjectsWithTag(interactTag);
 var close;
 var oldDist;
   for (var obj : GameObject in allObjs) {
      var pos = obj.transform.position ;
    others = obj.transform.name;
     other = obj.transform;
      var dist = Vector3.Distance(pos, transform.position);
      if (dist <distToInteract)
      return true;
   }
   
   return false;
}

function Update() {
var joystick: Movementwii=GetComponent(Movementwii);
   if(CloseEnough("People", 80.0)){
   SendMessage ("Focus", true);
   interaction=true;
   if ( joystick.fromNunchuckZ==true){
cross.gameObject.active=true;
cross.position=other.transform.position+Vector3(0,20,0);
}
else
{
cross.gameObject.active=false;}
}
   else
   {SendMessage ("Focus", false);
   interaction=false;
   cross.gameObject.active=false;}


if(CloseEnough("Push", 80))
interaction=true;
else
interaction=false;
}
function Focus (target1 : boolean) {
   if (target1==true){
other.light.enabled=true;
print("Close to:" + others);}
else
other.light.enabled=false;
}

Could anyone tell me how can I do that?
Back to top
View user's profile Send private message
andeeee
Forum Moderator


Joined: 19 Jul 2005
Posts: 2919
Location: Blackpool, United Kingdom

PostPosted: Fri Dec 18, 2009 5:40 pm    Post subject: Reply with quote
The problem is that you are returning from the CloseEnough function as soon as you have found an object within the distance, but you need to check them all to find the closest:-
Code:
var closest: GameObject;
var closestDist: float;
for (var obj : GameObject in allObjs) {
      var pos = obj.transform.position; 
      var dist = Vector3.Distance(pos, transform.position);

      if (dist < closestDist) {
          closestDist = dist;
          closest = obj;
      }
}

if (closestDist < distToInteract) {
    return obj;
} else {
    return null;
}

_________________
None of you understand... I'm not logged-in here with you - YOU'RE LOGGED-IN HERE WITH ME!
Back to top
View user's profile Send private message
linkthewise



Joined: 26 Sep 2009
Posts: 74

PostPosted: Fri Dec 18, 2009 10:49 pm    Post subject: Try that it didnt worked Reply with quote
I manage to solve all the errors, but seams not to do anything.It still doesnt work properly, when the distance is bigger than 100 there are problems, and it doesnt find the closest object
Code:

static var interaction;
var others;
var other;

var cross:Transform;

function CloseEnough(interactTag : String, distToInteract : float) : boolean {
   var allObjs : GameObject[] = GameObject.FindGameObjectsWithTag(interactTag);
 var close:GameObject;
 var oldDist=0;
 var temp:GameObject;
   for (var obj : GameObject in allObjs) {
      var pos = obj.transform.position ;
      var dist = Vector3.Distance(pos, transform.position);
     if(dist<oldDist||oldDist==0){
     oldDist=dist;
     close=obj;
     }
      if (oldDist <distToInteract&&close!=null) {
       others = close.transform.name;
     other = close.transform;
     return true;
     }
      
   }
   
   return false;
}

function Update() {
var joystick: Movementwii=GetComponent(Movementwii);
   if(CloseEnough("People", 180.0)){
   SendMessage ("Focus", true);
   interaction=true;
   if ( joystick.fromNunchuckZ==true){
cross.gameObject.active=true;
cross.position=other.transform.position+Vector3(0,20,0);
}
else
{
cross.gameObject.active=false;}
}
   else
   {SendMessage ("Focus", false);
   interaction=false;
   cross.gameObject.active=false;}


if(CloseEnough("Push", 80))
interaction=true;
else
interaction=false;
}
function Focus (target1 : boolean) {
   if(other!=null){
   if (target1==true){
other.light.enabled=true;
print("Close to:" + others);}
else
other.light.enabled=false;
}
}

As you can see in the image the player is closer to the capsule, but the target goes to the cube, thats the kind of problem I'm having with the script



Untitled.png

Back to top
View user's profile Send private message
linkthewise



Joined: 26 Sep 2009
Posts: 74

PostPosted: Sat Dec 19, 2009 2:11 pm    Post subject: I'm confused.. Reply with quote
Well I was trying to solve the problem, so I get to this code.
Code:

var attackRange = 200;
var target : GameObject;

function Start() {
    var targets = gameObject.FindGameObjectsWithTag("TARGET");
   print( "Targets found:" +targets.length );
}


function Update(){   

var targets = gameObject.FindGameObjectsWithTag("People");

var mytarget : GameObject;
var maxdistance : int = 180; // dummy large distance

if (targets.length>0)
{
   var curdist : float = attackRange;

   for (var enemy : GameObject in targets)
   {
      //work out which item is the closest and shoot at it, or find out
      //which enemy has the most armour left or the most power and shoot it.
      print ("Checking enemy: "+enemy.transform.name);
      var distance = Vector3.Distance(enemy.transform.position, transform.position);

      if (distance < (curdist-2)) {
          curdist = distance;
          target = enemy;
      }
       
      if (target == enemy) {
        print("My closest selected enemy so far is: "+target.transform.name);
      }
}
   
   if (target == null)
      return;
         
}
}

I found this code in the forum, I adapted and I seams to work. But when I adapt my code to this one. I reach this:
Code:

static var interaction=false;

var cross:Transform;
var target : GameObject;

function CloseEnough(interactTag : String, distToInteract : float) : boolean {
   var targets = gameObject.FindGameObjectsWithTag(interactTag);
 var mytarget:GameObject;
if (targets.length>0){
 var curdist : float = distToInteract;
   for (var enemy : GameObject in targets) {
      var distance = Vector3.Distance(enemy.transform.position, transform.position);
     if(distance<curdist){
     curdist=distance;
     target=enemy;
     return true;
     }
      if (target==null) {
          return false;
     }
   }
}
}

function Update() {
var joystick: Movementwii=GetComponent(Movementwii);
   if(CloseEnough("People", 120.0)){
   SendMessage ("Focus", true);
   if ( joystick.fromNunchuckZ==true){
cross.gameObject.active=true;
cross.position=target.transform.position+Vector3(0,20,0);
}
else
{
cross.gameObject.active=false;}
}
   else
   {SendMessage ("Focus", false);
   cross.gameObject.active=false;}

}
function Focus (target1 : boolean) {
   if(target!=null){
   if (target1==true){
target.light.enabled=true;
interaction=true;}
else
{
target.light.enabled=false;
interaction=false;}
}

}

This code was working fine in the first two test, but the third test it stops working, as it should, an I reach the same point I was before. Please help me
Back to top
View user's profile Send private message
linkthewise



Joined: 26 Sep 2009
Posts: 74

PostPosted: Sat Dec 19, 2009 6:37 pm    Post subject: Fixed Reply with quote
Thanks for all your help.
Back to top
View user's profile Send private message
Post new topic   Reply to topic    Unity Community Index // Scripting All times are GMT + 1 Hour
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum