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
1
Question by BakeSlots · Oct 20, 2011 at 12:31 PM · physicsmovetowards

how to make Vector3.MoveTowards accept physics

hi, i need to make my G.O change position between 2 vectors but the problem is that he ignoring physics, i want him to stop if he collided with any other object, any ideas? thnx :)

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

2 Replies

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

Answer by yeoldesnake 1 · Oct 20, 2011 at 04:11 PM

Add this after the if (Physics.Raycast(ray, out hit, Mathf.Infinity, myLayerMask)) check.


        if (Input.GetMouseButtonDown(0)){
if (Physics.Linecast (transform.position, target.position,hitInfo)){
//movement code here , tell it to move to hitInfo.point
}
else
//tell it to move to target.position
}
Comment
Add comment · Show 7 · 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 Ziad · Oct 20, 2011 at 05:48 PM 0
Share

this doesn't make sense .... if he can't move while he is colliding how can he go away.

avatar image yeoldesnake 1 · Oct 20, 2011 at 05:50 PM 0
Share

He is not colliding all the time. He wants it to move unless it meets something in the way. Sometimes it will sometimes it wont , it depends on how he intends to use the movement. EDIT: So i see that the movement is for a character? I thought you were trying to move an object , i am going to update the answer with a solution that uses raycasting.

avatar image BakeSlots · Oct 20, 2011 at 05:57 PM 0
Share

i'm Already Using Raycast :

     Ray ray = myCamera.ScreenPointToRay(Input.mousePosition);

     RaycastHit hit;

     if (Physics.Raycast(ray, out hit, $$anonymous$$athf.Infinity, myLayer$$anonymous$$ask))
     {
         if (Input.Get$$anonymous$$ouseButtonDown(0))
             nextPos.position = hit.point;
     }

     transform.position = Vector3.$$anonymous$$oveTowards(transform.position, nextPos.position, speed * Time.deltaTime);



but i need to make him collide

avatar image yeoldesnake 1 · Oct 20, 2011 at 05:59 PM 0
Share

Take a look at my solution , i am using linecast from the character's position to the destination's position to see if there is anything in between. If there is , it will move to where the linecast hit the object , which is right behind the wall.

avatar image yeoldesnake 1 · Oct 21, 2011 at 10:09 AM 0
Share

Updated the code a bit.

Show more comments
avatar image
1

Answer by TSorbera · Oct 20, 2011 at 05:34 PM

If you want to use physics, you should probably be moving the character with physics. Try Rigidbody.AddForce or AddRelativeForce.

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 yeoldesnake 1 · Oct 20, 2011 at 05:37 PM 0
Share

That too , i was operating under the thought that he wanted it to not have a rigidbody.

avatar image BakeSlots · Oct 20, 2011 at 05:59 PM 0
Share

this wont work for me cus i'm moving the object from point A to point B

avatar image TSorbera · Oct 20, 2011 at 08:32 PM 0
Share

http://unity3d.com/support/documentation/ScriptReference/Physics.CapsuleCast.html See if that will work for you. If not, another option is to add a collider to your object if there's not one already, and use OnCollision/TriggerEnter/Stay/Exit: http://unity3d.com/support/documentation/ScriptReference/$$anonymous$$onoBehaviour.OnCollisionEnter.html

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

2D 360 degress platformer example needed 0 Answers

Why isn't my script to move particles working? 0 Answers

How to make physics like Angry Birds Space -1 Answers

2d side scrolling problem 5 Answers

Move RigidBody along curve 2 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