- Home /
How to allow movement of game object to certain points only
Hey guys, I am new to Unity and I am building a 2D game. The purpose of the following two scripts is to check if and only if the user clicks on a node (specific clickable points on the screen), a "bat" should spawn at a given location and move to the node's location. I attempted this through using a script (MoveTo, attached to node) which contains a boolean that is set to true upon being clicked, and instantiates the bat at a given at 0,0,0. After doing that, the node is deleted. The other class, BatMoveC (attached to the bat) will then check if the boolean is set to true and rotate and animate the bat to that location. Unfortunately, this isnt working. I this the best way to do this? Do you recommend another method or is it fixable and I'm just being a noob :P
Here is MoveTo:
using UnityEngine;
using System.Collections;
public class MoveTo : MonoBehaviour {
public GameObject Bat;
public bool move;
// Use this for initialization
void Start () {
move = false;
}
// Update is called once per frame
void Update () {
}
void OnMouseDown()
{
Instantiate (Bat, new Vector3 (0, 0, 0), Quaternion.identity);
move = true;
Destroy (this.gameObject);
}
}
Here is the BatMoveC script:
using UnityEngine;
using System.Collections;
public class BatMoveC : MonoBehaviour {
public float speed = 10f;
public float rotationSpeed = 5000f;
private Quaternion qTo;
private Vector3 pos;
private bool moveable;
// Use this for initialization
void Start () {
pos = transform.position;
qTo = transform.rotation;
moveable = false;
}
// Update is called once per frame
void Update () {
GameObject go = GameObject.Find ("MoveTo");
MoveTo moveTo = GetComponent<MoveTo> ();
moveable = moveTo.move;
if (moveable){
pos = Input.mousePosition;
pos.z = transform.position.z - Camera.main.transform.position.z;
pos = Camera.main.ScreenToWorldPoint (pos);
Vector3 dir = pos - transform.position;
if (dir != Vector3.zero) {
qTo = Quaternion.FromToRotation (transform.right, dir) * transform.rotation;
transform.rotation = Quaternion.RotateTowards (transform.rotation, qTo, Time.deltaTime * rotationSpeed);
}
transform.position = Vector3.MoveTowards (transform.position, pos, Time.deltaTime * speed);
}
}
}
Thanks a million for your help <3
I was wondering, should I do a FS$$anonymous$$ approach? Im scared that will be out of my skill level however. (I really dont want to buy a gui oriented fsm just yet, I want to get better at this first).
I can see $$anonymous$$oveTo is the name of the script, but is the node called $$anonymous$$oveTo too? As GameObject.Find() is finding a gameobject, it cannot search for a script attached to an gameobject. Plus, if the node is destroyed, it cannot be searched.
Yeah, its searching for a script attached to a game object. Is it possible to get the vector coordinates of the node and pass them to the Bat$$anonymous$$oveC script somehow? That should work too, ins$$anonymous$$d of using a boolean to check ability to move.
There is a way.. I posted an answer but it need approval by a moderator... In fact you can pass value to a gameobject when you create it. Something like this:
GameObject myBat = Instantiate (Bat, new Vector3 (0, 0, 0), Quaternion.identity);
myBat.GetComponent<Bat$$anonymous$$oveC>().target = transform.position;
//target need to be public
Answer by SilverRush · Aug 02, 2014 at 03:24 PM
public class MoveTo : MonoBehaviour {
public GameObject Bat;
void OnMouseDown()
{
GameObject myBat = Instantiate (Bat, new Vector3 (0, 0, 0), Quaternion.identity);
myBat.GetComponent<BatMoveC>().target = transform.position;
Destroy (gameObject);
}
}
public class BatMoveC : MonoBehaviour {
public Vector3 target = Vector3.Zero;
void Update () {
if (target != Vector3.Zero){
//Move to target
}
}
Try these. There maybe some spelling mistakes but I guess the logic works.
I love you man, I think it will work. Let me give it a shot to let you know how it goes, ill try it as soon as I wake up tomorrow!
Okay, after messing around a lot I got it! Its a little different than the code you wrote, there is an issue with passing variables from On$$anonymous$$ouseDown. But still, you helped a lot in getting the final answer! Thanks!