Be the first to post for 'Vectors, Classes and Objects'!

If you’re reading this, there probably aren’t very many posts yet. But don’t worry, you can be the first! Either create a new post or just reply to this one to say ‘hi’.

hi, I just did this part like this, still not sure so i continue the lecture after posting:

// Use this for initialization
void Start()
{
    Vector2 location = new Vector2(2.1f, 3.0f); // Distance in meter
    Vector2 homeLocation = new Vector2(-1.3f,-5f);
    Vector2 distance = homeLocation - location;
    print("welcome to Go Home");
    print("A game to bring you home");
    print("Distance= " + Vector2.SqrMagnitude(distance));
    //if (location > homeLocation)
    //{
    //    print("go Backwards");
    //}
    //else if (location < homeLocation)
    //{
    //    print("go forward");
    //}
    //else
    //{
    //    print("you are at home");
    //}

    // print( location > homeLocation ? "go Backwards"  : ((location < homeLocation ? "go Forwards" : "you are at home" )));
}

// Update is called once per frame
void Update() {
     if (Input.GetKeyDown(KeyCode.RightArrow))
     {
         print("Right key Pressed");
     }
     if (Input.GetKeyDown(KeyCode.LeftArrow))
    {
        print("Left key pressed");
    }   
  }
}

Well done for having the courage to post your answer before seeing the solution!

[Solved] no long printing to console? i’ve only change the float to vectors

I see no words printed to console
I expect to see at least hello word still printing to console
I tried creating another basic hello world script along side this script…
I tried commenting out any lines with variables
I checked console buttons only one press is clear on play

Code>>
public class Printtext : MonoBehaviour {

// Use this for initialization
void Start() {
    Vector2 location = new Vector2(2.0f, 3.0f);        //distance in meters
    Vector2 homelocation = new Vector2(5.1f, 1.0f);      //homelocation 
    Vector2 pathToHome = homelocation - location;
    print("Hello World!");
    print("my find home");
    print("Path home : " + pathToHome);
    print("Distance to home: " + pathToHome.magnitude);
    if (location == homelocation)
    {
        print("at home");
    }
    

}

[Solved] I dont know how but next day I noticed reloading i had lost my game object…“create game emtpy”. so the script was not attached to any thing?

I’m using Unity3D on Mac platform and Microsoft Visual Studio not supported on Mac except Microsoft Studio Code. I don’t get MonoDevelop IDE complain of error Vector2(2.0f,3.0f) without the word “new”.

I have done this, not sure if all correct though, better watch the solution! It’s not very visual but the path coordinates comes out as (-0.6, 1.5) - so it seems right to me. :v::slight_smile:

// Use this for initialization
void Start () {
	//The location of the player
	Vector2 location = new Vector2(2.0f, 3.0f);

	//The location of home
	Vector2 homeLocation = new Vector2(1.4f, 4.5f);

	//The distane to home from the player
	Vector2 pathHome = homeLocation - location;

	//This is our Prints
	print ("Welcome to Go Home!");
	print ("A game were you need to find your way back home." + "\nGood Luck!");
	print ("Follow these coordinates: " + pathHome + " to find your way.");

	//Display message when you are home
	if (location == homeLocation) {
		print ("You have found your way home!" + "\nCongratulations");
	}
}

EDIT:
Neat trick with the .magnitude suffix!

Revisioned line:
print (“You are " + pathHome.magnitude + " metres from home.”);

Returns:
[You are 1.615549 metres from home.]

void Start () {
   
    Vector2 location = new Vector2(2.0f, 3.0f);
    Vector2 homeLocation = new Vector2(2.1f, 2.1f);
    Vector2 pathToHome = homeLocation - location;
   

    print("I always wondered what it was like out there...");
	print("On the other side of that invisible barrier");
	print("It is pretty great out here.");
	print("But it can be really scary");
	print("There are loud monsters that can run really fast");
	print("It is getting dark and cold now");
	print("But I wandered so far that I'm not sure of my way back");
	print("The sooner I get home, the sooner I can take a cat nap");
    if (location == homeLocation)
    {
        print("You are home!");
    }
    else
    {
        print("Distance:" + pathToHome);
    }

}

Changed code:

Result:

Privacy & Terms