• 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
1
Question by Zukas · Dec 19, 2013 at 05:43 PM · newbievector2click to move

How to retrieve GameObject's Vector2 coordinate and move another object to it's place

So, I'm trying to make script detect GameObject's coordinate/vector2 and then use it to move to it's spot. So those GameObject's are like nodes to which you can travel. This is how my script looks so far, I think it will explain what I want it to be like, and I know that it's wrong but I believe you'll get the point.

 using UnityEngine;
 using System.Collections;
 
 public class WorldMapMovement : MonoBehaviour
 {
     public float smooth;
     public GameObject Town1;
     public GameObject Town2;
 
     private Vector2 travelPosition;
 
     void Awake ()
     {
         travelPosition = transform.position;
         Town1 = GameObject.Find("Town1");
         Town2 = GameObject.Find("Town2");
     }
 
     void Update ()
     {
         TravelChanging();
     }
 
     void TravelChanging ()
     {
         Vector2 positionA = Vector2(Town1);
         Vector2 positionB = Vector2(Town2);
 
         if(Input.GetKeyDown(KeyCode.Q))
             travelPosition = positionA;
         if(Input.GetKeyDown(KeyCode.E))
             travelPosition = positionB;
 
         transform.position = Vector2.Lerp(transform.position, travelPosition, smooth * Time.deltaTime);
 }
 

Thanks in advance! Zukas.

Comment
Add comment · Show 1
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 Sundar · Dec 19, 2013 at 06:27 PM 0
Share

you must be getting errors here - Vector2 positionA = Vector2(Town1);

1 Reply

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

Answer by highpockets · Dec 19, 2013 at 07:02 PM

Generally you should use smaller case on the first letter of the first word of a variable.. Town1 should be town1. That's up to you though. Under the TravelChanging function switch the first 2 lines to:

Vector2 positionA = town1.transform.position

Vector2 positionB = town2.transform.position

Comment
Add comment · Show 1 · 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 Zukas · Dec 19, 2013 at 08:12 PM 1
Share

Thanks! It solved it. I don't know how to exactly confirm this answer. It says I need 15 reputation or something.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

20 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

Related Questions

Multiple Cars not working 1 Answer

I need help !!! -3 Answers

How would I get camera bobbing? 0 Answers

Make the first person controller transparent? 1 Answer

What Am I Doing Wrong? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges