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 Joseph Lerner · Jul 28, 2014 at 02:17 AM · vector2declaration

How do I make Vector2 X and Y values into a float? C#

How do I make Vector2 X and Y values into a variable in C#?

I have looked on the web including the scripting reference for about 2 hours (I'm new) and have found nothing.

I just want to figure out how to extract the X and Y values from a vector 2 and make them into a variable. I would be very grateful if you answered my question with a block of code in C#.

I don't know how to get the X value from a vector3 either, but I'm assuming they are almost identical for these purposes.

Comment
Add comment · Show 5
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 robertbu · Jul 28, 2014 at 02:19 AM 1
Share

Not sure where your problem is, but given a Vector3 like transform.position, you can do:

    float x = transform.position.x;

If this is your issue, I recommend reviewing a C# book. It will explain how to variables in classes and structures.

avatar image Joseph Lerner · Jul 28, 2014 at 02:28 AM 0
Share

Hmmm i understand my question is a bit confusing.

Let me put down what I am trying to ask in conext. I am currently saving my mouseclick transform into my mouse2 vector2.

using UnityEngine; using System.Collections;

 public class $$anonymous$$ove : $$anonymous$$onoBehaviour {
 
     public Vector2 mouse2;
 
 
     void Update() 
     {
         mouse2 = Input.mousePosition;
 
 
         }
 
     }
 
 

I want to be able to then take the X value of mouse2 and the Y value of mouse2 out of it and make it into specific floats so I can use them for other purposes.

avatar image robertbu · Jul 28, 2014 at 02:31 AM 0
Share
  float x = mouse2.x;
avatar image Joseph Lerner · Jul 28, 2014 at 02:39 AM 0
Share

I don't think that works, I tried it in my script and it stopped it from working. Should I not be putting it under the line that has my mouse2 vector2?

avatar image b1gry4n · Jul 28, 2014 at 03:45 AM 0
Share

to turn any value into a float its as simple as multiplying the number by 1.0

 int number = 25;
 float numbertofloat = number * 1.0f;


not necessarily an answer to your question, but a useful tip either way

2 Replies

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

Answer by Joseph Lerner · Jul 28, 2014 at 04:28 AM

Got it!

 using UnityEngine;
 using System.Collections;
 
 public class Move : MonoBehaviour {
     
     public Vector2 mouse2;
     public float x;
     
     void Update() 
 
     {
         mouse2 = Input.mousePosition;
         x = mouse2.x;
         
     }
     
 }
     

As you can see, I first had to make a new public float next to my new vector2, and then under the void update I made the floats value = the value of X in mouse2. Thanks for that last post.

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 sachinbirajdar · Mar 31, 2018 at 01:22 PM 0
Share

Wonderful!!

avatar image
1

Answer by Nition · Jul 28, 2014 at 02:45 AM

Robertbu's comment is correct. This will work:

 using UnityEngine;
 
 public class Move : MonoBehaviour {
     Vector2 mouse2;
 
     void Update() {
         mouse2 = Input.mousePosition;
         float mouseX = mouse2.x;
         float mouseY = mouse2.y;
 
         print("Mouse X is: " + mouseX + " Mouse Y is: " + mouseY);
     }
 }

If that doesn't work, then something is wrong somewhere else. You could tell us what error you're getting.

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Errors in Vector2 variables 1 Answer

Declaring a type in JavaScript arrays 3 Answers

Need to increase the gameobject value . 2 Answers

Vector2.Angle questions and confusion. 1 Answer

Vector2 is always (0,0) - HELP 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