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 drodrii · Dec 01, 2014 at 05:24 PM · movementtransformpositionvector3

Moving an Object - The right way. 2D

Hello everyone, hope you're doing good!

So, I came across a problem of mine. I wanted to addForce to an Object. Here's the Question:

Now, I did a Debug.Log on the ball's velocity when it's moving but I found out it was (0,0)

So my question is this, is this the best way to move an object?

The idea is that the gameObject(Ball) goes towards the player when inRadius.

Here's how Im moving:

 void Update (){
 
         transform.position = Vector3.MoveTowards(transform.position, playerPos, moveSpeed * Time.deltaTime);
 
         if (transform.position == playerPos) {
             
             GetPlayerPos();
         }
 
 
         if (Time.time - _startTimer >= 5) {
 
             GetPlayerPos();
         }
                         
 
     }


So, timer is there so It doesn't constantly get the same position over and over.

GetPlayerPos does this:

 public void GetPlayerPos (){
 
         playerPos = GameObject.Find ("Player").transform.position;
         _startTimer = Time.time;
 
     }


Now 2 quick questions:

1)Am I moving my object the best way towards my player?(I want to be able to use physics and such...) 2)If Im doing this the right way, why does the object don't have any velocity?

Thank you for your time!!!

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
0
Best Answer

Answer by Kiwasi · Dec 01, 2014 at 05:33 PM

The right way to move an object with physics is RigidBody.AddForce or RigidBody.velocity.

Using transform.position causes an object to 'teleport' and will override any physics you use.

Comment
Add comment · Show 3 · 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 drodrii · Dec 01, 2014 at 05:54 PM 0
Share

@Bored$$anonymous$$ormon

So, in this particular case how would you translate movetowards to velocity?( rigidbody.velocity = desiredPosition; ??

avatar image drodrii · Dec 01, 2014 at 05:59 PM 0
Share

@Bored$$anonymous$$ormon

Ok , I tried this:

this.rigidbody2D.velocity = Vector3.$$anonymous$$oveTowards(transform.position, playerPos, moveSpeed * Time.deltaTime);

The object moves towards another way haha. ;/ I think I did this totally wrong. haha

avatar image drodrii · Dec 01, 2014 at 06:51 PM 0
Share

@Bored$$anonymous$$ormon

Ok, I did some research , rigidBody2D.movePosition(vector2) will move towards that position so what I did was this: float tempY = transform.position.y; float tempX = transform.position.x; Vector2 tempPosit = new Vector2(tempX - 0.1f,tempY); //transform.position = Vector2.$$anonymous$$oveTowards(transform.position, tempPosit, speed * Time.deltaTime); rigidbody2D.$$anonymous$$ovePosition(tempPosit);

But the object isnt moving :/

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

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

Related Questions

How to smooth movement object in one-axis? 1 Answer

2D Background Stop Moving on BoundingBox Exit? 2 Answers

Setting target position in Vector3.MoveTowards 2 Answers

Input.mousePosition returns coordinates as integers. 1 Answer

Lerp not working 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