Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This question was closed Sep 20, 2014 at 12:09 PM by galaboy for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by galaboy · Jul 09, 2014 at 08:19 AM · scriptingbasics

need explanation for a line of code.

hi, i need explanation for a line of code.

 public Vector2 speed = new Vector2(50, 50);
 vector2 movement = new Vector2(speed.x * inputX,speed.y * inputY);

i know what it does. but i couldn't able to understand the concept here

 **vector2 movement = new Vector2(speed.x * inputX,speed.y * inputY);**

need help in expaning.

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

2 Replies

  • Sort: 
avatar image
2
Best Answer

Answer by CHPedersen · Jul 09, 2014 at 08:36 AM

First of all, correct the code. It's wrong, and won't compile. It should be:

 public Vector2 speed = new Vector2(50, 50);
 Vector2 movement = new Vector2(speed.x * inputX,speed.y * inputY);

Notice the capital "V", there.

Second of all, while studying the code, try to simply say out loud (to yourself!) what the code does, line by line, dot by dot, comma by comma. This is known as rubber duck debugging, and often helps understanding because saying things out loud forces your mind to process what the code does.

Here's an example, based on your code:

"Okay, line 1: Declare a Vector2 called 'speed'. It's two-dimensional, and it's a field variable, since it has an access modifier, which is set to public. Initialize this variable, 'speed', to hold a Vector2 whose values are set to x = 50, and y = 50. Okay, so speed is a two-dimensional vector holding 50, 50. Cool."

"Okay, line 2: Declare a new Vector2, called 'movement'. This one is a local variable, since it does not have an access modifier, and because it references 'speed' later on. Initialize 'movement' to a new Vector2 holding values for x and y which are multiplications of the x and y of 'speed', and of the inputX and inputY variables. Okay, so, inputX and inputY are probably read by input from the user, and their values will change based on what the user does. But the values of speed are constants - I know that, because I just set both of them to 50. Ah! Okay, so movement is just a vector that defines the direction of movement as per whatever the user does, and then the magnitude of the vector is scaled up using the speed numbers!".

End of story. :)

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 Armand · Jul 09, 2014 at 08:45 AM 0
Share

For clarity and readability, I also suggest always putting a space after a comma :)

avatar image galaboy · Jul 09, 2014 at 10:01 AM 0
Share

thanks for the help. got it. i was a bit confused.

avatar image
0

Answer by jamesflowerdew · Jul 09, 2014 at 08:33 AM

input on the x and y will be between 1 and -1 depending on the player's actions (0 if they are not touching anything ;)). mutiplying these by set values will convert these actions into movement at in this case the max speed of 50.

this is kind-of crude, and does not create acceleration decelleration. If I wantes smooth play, I'd think of : public int speed=50;//they are same, so why bother with vector2 private Vector2 movement=new Vector2(0,0); movement+=new Vector2(speed inputX,speed inputY)Time.deltaTime; //reach optimal speed in roughly a second movement=(1-Time.deltaTime); //slow down in roughly a second.

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 CHPedersen · Jul 09, 2014 at 08:43 AM 0
Share

That's mostly correct, but you don't know what values inputX and inputY might have based on his code. They could be declared like this:

         float inputX = Input.GetAxis("$$anonymous$$ouse X");

The mouse can return values greater than 1 and less than -1.

avatar image galaboy · Jul 09, 2014 at 09:32 AM 0
Share

thanks for the help.

Follow this Question

Answers Answers and Comments

24 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 avatar image avatar image avatar image avatar image

Related Questions

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

Photon Unity Networking:How do i set it to only let players control themselves? 0 Answers

How to import the object from server to unity 2 Answers

Why does "Missing (Mono Script)" keep happening? 3 Answers

Material doesn't have a color property '_Color' 4 Answers


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