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 Optimusslime · Aug 01, 2014 at 10:07 PM · c#movementtransform

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

Comment
Add comment · Show 4
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 Optimusslime · Aug 01, 2014 at 10:23 PM 0
Share

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).

avatar image SilverRush · Aug 02, 2014 at 10:11 AM 0
Share

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.

avatar image Optimusslime · Aug 02, 2014 at 11:07 AM 0
Share

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.

avatar image SilverRush · Aug 02, 2014 at 02:14 PM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 2 · 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 Optimusslime · Aug 03, 2014 at 12:19 AM 0
Share

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!

avatar image Optimusslime · Aug 04, 2014 at 08:36 AM 0
Share

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!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

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

Making a bubble level (not a game but work tool) 1 Answer

Implement the equation as a code? 1 Answer

fixed distance for Vector3.moveTowards ? 1 Answer

Help regarding World-Local Transfrom Rotation? 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