- Home /
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.
you must be getting errors here - Vector2 positionA = Vector2(Town1);
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
Thanks! It solved it. I don't know how to exactly confirm this answer. It says I need 15 reputation or something.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
I need help !!! -3 Answers
How would I get camera bobbing? 0 Answers
Click To Revert to Original Texture else Destroy script help 1 Answer
How to have animation play correctly 1 Answer