• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

2D top down dash code 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Strange foreach behaviour 2 Answers

[RESOLVED! :D][C#] Friction/Deceleration - Compiler won't let me damp individual Rigidbody axes to 0. Force in opposite direction maybe? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges