- Home /
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.
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;
}
}
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?
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.
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?
Debug.Log("Card selected:"+hit.collider.name);
should tell you what card you selected by clicking. The card should move if Summon is true.
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;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613140318im_/https://answers.unity.com/themes/thub/images/avi.jpg)