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 Xeong-Hu · Mar 15, 2014 at 06:34 PM · guitagmovetowards

Moving GameObject Towards Spot when clicking a GUI Function

Hello Unity PPl I'm still trying to make an Awesome Card game here, but dang it's so hard!

So I was wondering. Say I had a card in my hand and I want to summon it onto my field.

Alright so in my game when I click a card, I get this GUI Button Function named "Summon"

When I click Summon it'll move the card towards a position on the field Zone.

Here's the problem..

My Summon Button Script isn't attached to every single Card GameObject. It used to be like that but when I had it like that, whenever I clicked a gameobject that is tagged as card. It'll show the Button "Summon" Multiplied times the number of cards that are in the Game World.

Right now my Summon Button script is in an Empty GameObject.

So what I need help with is.. How do I make the Game know that I clicked that certain card that I want to summon?

Like say I clicked a card named "Knighlimphic" and the Summon button appears over it. and I click the Summon button.

Since my script isn't attached to the card and it's attached to an Empty GameObject. How will it know that I clicked, Knighlimphic as the card I want to summon?

Here's what my code looks like. In the EmptyGameObject.

The main part of the Code is in the GUI Function area and below that. The other part of the code just tells me which spot is occupied.

Special Thanks to: Robertbu ( He gave me this GUI Script )

 private var showButton = false;
 private var pos : Vector2;
 private var hit : RaycastHit;
 
 function Update(){
     if (Mathf.Approximately(this.transform.position.x, Zone1 )){
 Debug.Log("Monster Summoned in MonsterZone 1");
     Summon = false;
     Occupied1.active = true;
 }
 else{
     Occupied1.active = false;
 }
 
     if (Mathf.Approximately(this.transform.position.x, Zone2 )){
 Debug.Log("Monster Summoned in MonsterZone 2");
     Summon = false;
     Occupied2.active = true;
 }
 else{
     Occupied2.active = false;
 }    
 
     if (Mathf.Approximately(this.transform.position.x, Zone3 )){
 Debug.Log("Monster Summoned in MonsterZone 3");
     Occupied3.active = true;
     Summon = false;
 }
 else{
     Occupied3.active = false;
 }
 
 
     if (Mathf.Approximately(this.transform.position.x, Zone4 )){
 Debug.Log("Monster Summoned in MonsterZone 4");
     Occupied4.active = true;
     Summon = false;
 }
 else{
     Occupied4.active = false;
 }
 
 
 
 
     if (Mathf.Approximately(this.transform.position.x, Zone5 )){
 Debug.Log("Monster Summoned in MonsterZone 5");
     Occupied5.active = true;
     Summon = false;
 }
 else{
     Occupied5.active = false;
 }
 }
 
 function OnGUI() {
     var e = Event.current;
  
     var x = pos.x - 50;  // Calc x and y to center of button
     var y = pos.y - 10;
 
     if (showButton && GUI.Button (Rect (x,y - 95, 100, 20), "Summon")) {
         Summon = true;
         showButton = false;
     } else {
        if (e.type == EventType.MouseDown) {
          CheckClick();
        }
     }
     
 }
  
 function CheckClick() {
     var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  
     if (Physics.Raycast(ray, hit)) {
        if (hit.collider.tag == "Card") {
          pos = Camera.main.WorldToScreenPoint(hit.transform.position);
          pos.y = Screen.height - pos.y;  // convert from Screen to GUI
          showButton = true;
        }
     }
     else {
        showButton = false;
     }
 }

Please help me out. I don't like being stuck on one thing for too long lol. I'd really appreciate it.

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
Best Answer

Answer by jbecana · Mar 16, 2014 at 08:31 AM

On top of your script, next to your other vars:

    private var selectedCard : GameObject;
 

Try this on your Update()

     if (Input.GetMouseButtonDown (0) && !showButton)
     {
          CheckClick ();
     }
     if (Summon)
     {
         selectedCard.transform.position = Vector3.MoveTowards(transform.position, target1.position, step);
     }

Your OnGUI() should be:

 function OnGUI() 
 {
     var e = Event.current;
  
     var x = pos.x - 50;  // Calc x and y to center of button
     var y = pos.y - 10;
  
     if (showButton && GUI.Button (Rect (x,y - 95, 100, 20), "Summon")) 
     {
         Summon = true;
         showButton = false;
     }
 }
 

Your CheckClick()

 function CheckClick() 
 {
     var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  
     if (Physics.Raycast(ray, hit)) 
     {
        if (hit.collider.tag == "Card") 
        {
          selectedCard = hit.collider.gameObject;
          pos = Camera.main.WorldToScreenPoint(hit.transform.position);
          pos.y = Screen.height - pos.y;  // convert from Screen to GUI
          showButton = true;
        }
     }
     else 
     {
        showButton = false;
     }
 }
 
Comment
Add comment · Show 18 · 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 Xeong-Hu · Mar 16, 2014 at 04:54 PM 0
Share

Alright. I tried it out and I still don't know how exactly I can do this.

  if (Summon == true)
      var step = speed * Time.deltaTime;
      {selectedCard.transform.position = Vector3.$$anonymous$$oveTowards(transform.position, target1.position, step);}
 
 }
 
 function OnGUI() {
     var e = Event.current;
  
     var x = pos.x - 50;  // Calc x and y to center of button
     var y = pos.y - 10;
 
     if (showButton && GUI.Button (Rect (x,y - 95, 100, 20), "Summon")) {
         Summon = true;
         showButton = false;
     } else {
        if (e.type == EventType.$$anonymous$$ouseDown) {
          CheckClick();
        }
     }
     
 }
  
 function CheckClick() {
     var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
      var selectedCard : GameObject;
     if (Physics.Raycast(ray, hit)) {
        if (hit.collider.tag == "Card") {
         selectedCard = hit.collider.gameObject; //Store my selection
          pos = Camera.main.WorldToScreenPoint(hit.transform.position);
          pos.y = Screen.height - pos.y;  // convert from Screen to GUI
          showButton = true;
        }
     }
     else {
        showButton = false;
     }
 }


Up on the top I made a function that woul let my selected ard move towards a designated spot. But it still doesn't want to work.

And uh I also got confused with the code you typed in here. $$anonymous$$yCardGameObjectHere selectedCard; am I supposed to put a ":" or a "=" in between the 2?

avatar image jbecana · Mar 16, 2014 at 05:44 PM 0
Share

It's just a var declaration, sorry, I'm not familiar with JavaScript. I would move your check on mouse button to Update() like this:

 if (Input.Get$$anonymous$$ouseButtonDown (0))
 {
   CheckClick();
 }

and remove the else block from the OnGUI() if GUIButton sentence:

 if (showButton && GUI.Button (Rect (x,y - 95, 100, 20), "Summon")) {
         Summon = true;
         showButton = false;
     }


Include a Debug.Log sentence in the raycast if sentence just to make sure it's not working.

avatar image Xeong-Hu · Mar 16, 2014 at 09:31 PM 0
Share

No problem with the Apology dude. Alright cool here's my new code. But.

 if (Input.Get$$anonymous$$ouseButtonDown (0))
 {
   CheckClick();
 }
 }
 
 function OnGUI() {
     var e = Event.current;
  
     var x = pos.x - 50;  // Calc x and y to center of button
     var y = pos.y - 10;
 
     if (showButton && GUI.Button (Rect (x,y - 95, 100, 20), "Summon")) {
         Summon = true;
         showButton = false;
     } 
     var Summoning = false;
 }
  
 function CheckClick() {
     var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
       var selectedCard : GameObject;
                    var step = speed * Time.deltaTime;
     if (Physics.Raycast(ray, hit)) {
     Debug.Log("Something happened");
        if (hit.collider.tag == "Card") {
         selectedCard = hit.collider.gameObject; //Store my selection
       Summoning = true;
      
          pos = Camera.main.WorldToScreenPoint(hit.transform.position);
          pos.y = Screen.height - pos.y;  // convert from Screen to GUI
          showButton = true;
        }
     }
     else {
        showButton = false;
     }
 }

I tried at your code and now when ever I click on an object my Debug("Something Happened") Just appears up. Is that right?

avatar image jbecana · Mar 16, 2014 at 09:51 PM 0
Share
 Debug.Log("Card selected:"+hit.collider.name); 

should tell you what card you selected by clicking. The card should move if Summon is true.

avatar image Xeong-Hu · Mar 16, 2014 at 10:15 PM 0
Share

Alright thanks. Now it's telling me which object I clicked, so I decided to make 2 Cards. Now that I did that when ever I click on my Duplicated Card named "Card" (The Original Card is named $$anonymous$$night.) It just $$anonymous$$oves it towards the Game Object Card named "$$anonymous$$night". Did I use my $$anonymous$$oveTowards function right?

And um. When I do click on my card it moves a little. Hmm.. I once got it to move towards the spot. but I would have to click summon. and then click my Card game object. and I'd have to constantly click it until it reaches the spot I want it to be at.

Btw I really appreciate the help.

Damn I tried bolding where my $$anonymous$$ovetowards function wwas but it didn't work. It's right in the Check Click Function and under if (Summon == true)

 if (Input.Get$$anonymous$$ouseButtonDown (0))
 {
   CheckClick();
   
 }
 }
 
 function OnGUI() {
     var e = Event.current;
  
     var x = pos.x - 50;  // Calc x and y to center of button
     var y = pos.y - 10;
 
     if (showButton && GUI.Button (Rect (x,y - 95, 100, 20), "Summon")) {
         Summon = true;
         showButton = false;
     } 
     var Summoning = false;
 }
  
 function CheckClick() {
     var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
       var selectedCard : GameObject;
                    var step = speed * Time.deltaTime;
     if (Physics.Raycast(ray, hit)) {
     Debug.Log("Card selected:"+hit.collider.name); 
        if (hit.collider.tag == "Card") {
         selectedCard = hit.collider.gameObject; //Store my selection
       Summon = true;
       step = speed * Time.deltaTime;
       if (Summon == true);{
       **selectedCard.transform.position = Vector3.$$anonymous$$oveTowards(transform.position, target1.position, step);**
       }
      
          pos = Camera.main.WorldToScreenPoint(hit.transform.position);
          pos.y = Screen.height - pos.y;  // convert from Screen to GUI
          showButton = true;
        }
     }
     else {
        showButton = false;
     }
 }
Show more comments

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

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Setting Scroll View Width GUILayout 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to make spaces in between Gui boxes being made in for loops? 1 Answer

How to create an agenda or timetable system? 1 Answer

XBox 360 controller for the GUI 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