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 SebastianRSP · Feb 09, 2014 at 03:07 PM · c#movementtransformtranslatevector2

Simple C# Vector2D Moving Question

Hey There! I am having some problems creating a C# script to move my object in a 2D space using Vector2, this is how it looks at the moment, but it gives me a bunch of errors, in line 13. If i change "transform.Translate(Vector2...." to: "Vector2.Up" It works with no errors, but this does not work for me as my object is rotating.

 using UnityEngine;
 using System.Collections;
 
 public class PControl : MonoBehaviour {
 
 
     public float moveSpeed = 10f;
 
     void Update () {
     
         if(Input.GetKey(KeyCode.UpArrow))
         {
             transform.Translate(Vector2(0, 1) * moveSpeed * Time.deltaTime);
             Debug.Log ("UP");
         }
     }
 }
 

Any help is appreciated!

Comment
Add comment · Show 3
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 ankush_Kushwaha · Feb 09, 2014 at 04:21 PM 0
Share

Try transform.Translate(new Vector3(0, 1,0) * moveSpeed * Time.deltaTime); or Try transform.Translate(Vector3(0, 1,0) moveSpeed Time.deltaTime);

avatar image DajBuzi · Feb 09, 2014 at 05:10 PM 1
Share

Translate is changing the position of an object using Vector3. I would recommend you to not use Translate because it is working in the same way as you would just change position values in the editor. Try to use rigidbody2D.velocity or rigidbody2D.AddForce().

avatar image SebastianRSP · Feb 14, 2014 at 02:52 PM 0
Share

@DajBuzi Thank you! I managed to get it working even better with .AddForce, thanks for the help :)

1 Reply

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

Answer by robertbu · Feb 09, 2014 at 05:27 PM

You need the 'new' keyword in front of Vector2() in C#. Line 13:

 transform.Translate(new Vector2(0, 1) * moveSpeed * Time.deltaTime);

Note that the Vector2 class has a Vector2.up, so you can do:

 transform.Translate(Vector2.up * moveSpeed * Time.deltaTime);

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

20 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

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

how do i slowly translate a object to a other objects position 2 Answers

How can I move an object a certain distance, once? 2 Answers

Code Not Working 1 Answer

Why is my character strafing around the mouse (2d) 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