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

moving objects with transform position

I have this 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 which equals to 4f but even if i change it to something like 0.00000000000000000000004f nothing changes. I dont understand the reason of this

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

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 which is quite fast

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

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

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

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 things).

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

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
Add comment · 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 += ins$$anonymous$$d 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.

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

Why can't I assign transform.position to a Vector3 object? 3 Answers

Why is rotation offset 1 Answer

Sprite moving too far/fast 1 Answer

transform.position not setting position OR animation setting position even though it shouldn't 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