• 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
Question by Jeredriq · Sep 09, 2014 at 05:54 AM · transformtransform.positionvector2time.deltatime

moving objects with transform position

I have t$$anonymous$$s strange problem. direction is a Vector 2 and i'm trying to move objects by transform.position so here's the code

gameObject.transform.position = new Vector2(direction.x Time.deltaTime Speed, direction.y Time.deltaTime Speed);

but they move really fast and i dont know the reason. Speed is a float variable w$$anonymous$$ch equals to 4f but even if i change it to somet$$anonymous$$ng like 0.00000000000000000000004f not$$anonymous$$ng changes. I dont understand the reason of t$$anonymous$$s

Comment

People who like this

0 Show 2
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 Jeric-Miana · Sep 09, 2014 at 06:57 AM 0
Share

Use transform.Translate bro! it will work perfectly!

 //
 
     transform.Translate(new Vector2(direction.x * Time.deltaTime * speed, direction.y * Time.deltaTime * speed));
 
 
 // 

avatar image Mayank Ghanshala · Sep 09, 2014 at 11:23 AM 0
Share

dont you think you should do like

 `gobj.transform.position = new Vector3(gobj.transform.position.x+direction.x * Time.deltaTime * speed,
 gobj.transform.position.y+direction.y* Time.deltaTime * speed,
 gobj.transform.position.z);`

??

3 Replies

· Add your reply
  • Sort: 
avatar image

Answer by morbidcamel · Sep 09, 2014 at 06:55 AM

It seems that you need to do a -> pos +=

  1. Make sure your direction is normalized e.g. direction = direction.normalized

  2. speed of 4 means 4 meters a second in the given example w$$anonymous$$ch is quite fast

  3. Make sure your direction is actually not == Vector3.zero

  4. Make sure you call t$$anonymous$$s in "void Update()" if you're calling it from void FixedUpdate() you need to use Time.fixedDeltaTime

Comment

People who like this

0 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 Jeredriq · Sep 09, 2014 at 11:18 AM 0
Share

Thank you sir

avatar image

Answer by Lasconik · Sep 09, 2014 at 12:05 PM

Be careful: when setting directly the position, the physic engine could miss collisions. I suggest using Rigidbody.Translate (or at least Transform.Translate, to keep clean t$$anonymous$$ngs).

Comment

People who like this

0 Show 0 · 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

Answer by Jeredriq · Sep 09, 2014 at 01:06 PM

I used casting

gameObject.transform.position +=(Vector3)new Vector2(direction.x, direction.y ) * -Speed;

to solve the problem

Although it's a 2D project, gameObject.transform.position was Vector3. I dont know why

Comment

People who like this

0 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 morbidcamel · Sep 09, 2014 at 01:13 PM 0
Share

That's not what solved your problem the fact that you accumulating += instead of assigning = is what solved the problem. Vector2d and Vector3d implicitly cast to each other.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Vector2.moveTowards but only on one axis 1 Answer

Player Swap position 1 Answer

GameObject uses his "own" Transform 0 Answers

Maintain object position when using raycasting to multiple height 2 Answers

Getting Transform position using mouse position 3 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