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 Connor5393 · Jan 03, 2013 at 09:56 PM · movearound

Make an object move toward a point, but stop when it hits another object?

I'm trying to make something that allows you to grab an object and it will gradually slide toward the mouse which I have done using Vector3.Lerp. This part works fine. I want the object to stop moving when it hits an obstacle in between it and the mouse point but still be able to move around it should the position of the mouse change.

In other words, if an object is moving toward the mouse and an obstacle blocks it, the object should sort of start to slide around the obstacle.

I have no idea how I can go about doing this.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Blacklink2001 · Aug 08, 2016 at 07:12 PM

You could try using a simple collider. Add colliders to the object moving towards the mouse and colliders to the object that should block it. Then you should add a function on the object moving towards the mouse saying something like

public bool Colliding = false; void OnCollisionEnter (Collision col) { %|-388435161_2|% %|-937706317_3|% %|949459639_4|% %|258082576_5|% } Then change the part that is moving it towards the mouse by adding an if(!Colliding) statement. The first part will keep track of when the object has hit another one, and by adding the if statement you can make it stop the movement when colliding is true. This might be very buggy but it should work.

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 Blacklink2001 · Aug 08, 2016 at 04:16 PM 0
Share

Oh sorry only just noticed how old this post is... This is probably never going to be noticed.

avatar image
0

Answer by villevli · Aug 08, 2016 at 07:13 PM

You should use Unity's physics components and use rigidbody.AddForce to move the object towards the mouse. The object and the obstacle both need colliders. The object also needs a Rigidbody so it can be moved by Unity's physics system. (Use the 2D variants of the components if your game is 2D). Depending on your game you may need to disable gravity in the rigidbody or in the physics manager. You can use this method to make AddForce work more like adjusting the transform position with Vector3.Lerp

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 nathanthesnooper · Aug 08, 2016 at 07:14 PM

Haven't tried it yet, but looks like its good.

Not doing these in editor so fix basic errors :X

if you want the simplest form of "pathfinding" (Just move towards an object). you can either be lazy with bad physics using

Vector3.MoveTowards(this.transform.position, Vector3 Target, float Speed);

or... Vector3 Force = Vector3.zero; Vector3 TmpForce = Vector3.MoveTowards(this.transform.position, Vector3 Target, float Speed); Force = new Vector3(TmpForce.x - this.transform.position.x, TmpForce.y - thi.... ....position.z); Rigidbody.velocity = Vector3.zero; Rigidbody.angularvelocity = 0; Rigidbody.Addforce(Force);

and if your fine with rotation

follow this

and

Rigidbody.Addforce(transform.forward); or in 2D Rigidbody2D.Addforce(transform.up);

:)

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

11 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

Related Questions

move around script help me 4 Answers

How to make an AI that moves around 1 Answer

How do I make the nose/whole of my spaceship move? 2 Answers

Camera move belong a certain path 1 Answer

Can I move component of one gameobject to other. 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