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 Caelorum · Sep 24, 2014 at 10:16 PM · c#velocityvector2

Can anyone tell me why this code isn't working?

I'm trying to make a simple pong game using a tutorial I found online. The person in the tutorial is using javascript, but i'm trying to follow along using C#.

Here is the script i'm using:

 using UnityEngine;
 using System.Collections;
 
 public class playerScript : MonoBehaviour {
 
     public KeyCode moveUp;
     public KeyCode moveDown;
 
     public float speed = 10f;
 
     // Update is called once per frame
     void Update () 
     {
         Vector2 v = rigidbody2D.velocity;
         if(Input.GetKey(moveUp))
         {
             v.y = speed;
         }
         else if(Input.GetKey (moveDown))
         {
             v.y = speed * -1;
         }
         else
         {
             v.y = 0;
         }
     
     }
 }

I had an issue compiling at first, and had to make my Vector2 into a variable for some reason. I don't know why, but I guess you need a temporary value for rigibody2D.velocity. But the character won't move at all if I hit W or S, which are the keys the public variables are assigned to. Am I missing something due to a c# coding issue?

It seems logical. Two keys created, a speed created. Vector2 v is initialized as the rigidbody's velocity. If I hold w it should move in a positive 10 direction. Reverse for the S key. I'm not understanding the issue.

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

1 Reply

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

Answer by Kiwasi · Sep 24, 2014 at 10:25 PM

Vector2 is a struct. This means it is passed by value. This means that you need to reassign it back.

Add this line after all of your if/else clauses to reassign the variable back to velocity.

 rigidbody2D.velocity = v;
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 Caelorum · Sep 24, 2014 at 10:32 PM 0
Share

Thanks it worked. Not sure I fully understand WHY i have to do this, but as long as it works I know I can replicate it in future games.

avatar image Kiwasi · Sep 24, 2014 at 11:33 PM 0
Share

Google pass by value and pass by reference.

When you pass a class you pass the pointer to the class. This means that any changes you make to the class will be changed on the original. There is only one copy.

When you pass a struct you pass a copy of the struct. Every value contained is duplicated in your new location. This is more efficient for small data structures. But it means any changes you make on a copy are not passed back to the original.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

2D top down dash code 1 Answer

Create custom constructors for certain Value types/Objects (Vector3, Transform, etc...) 2 Answers

Velocity bug with hinge joints forces in C# 0 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