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 Crayz · Jun 29, 2014 at 01:57 AM · controllertop downclick to move

Top-down click to move with Rigidbody

I've been working at this for a few days now but just can't find a solution. I'm trying to create a top-down click to move Rigidbody controller that will work properly with the character moving along a 3d world (up or down slopes, steps, and falling off objects).

If the player is holding left mouse button the controller should move the entire time the mouse button is held down. If the player simply clicks once, store the location in a Vector3 and move the player to that location.

I'm able to get this to work for the most part using a Raycast to detect where the player clicked, then applying force in that direction (Player's position Vector3 subtracted from Target position Vector3), and everything seems to work properly up until I let go of the mouse button. I can't seem to find a good way to determine when to stop applying force as soon as the player has reached his target.

Right now I'm working with something like this:

 if(IsMoving() && grounded)
 {
     rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
 }
 
 private bool IsMoving() {
     Vector3 a = new Vector3(moveToHit.point.x, 0, moveToHit.point.z);
     Vector3 b = new Vector3(rigidbody.position.x, 0, rigidbody.position.z);
 
     float curDistance = Vector3.Distance(a, b);
 
     if(curDistance >= 0.2)
     {
         return true;
     }
     else
     {
         return false;
     }
 }

But this doesn't seem to work very well. It' unlikely I'll ever catch curDistance at exactly 0.0 in a single frame, and when the player is falling down or going up ramps things seems to bug out.

Any support on this is greatly appreciated. I'm also open for going at this in a completely different direction, such as with a CharacterController (which I tried before this Rigidbody, though had issues getting my gravity to work out correctly)

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
2

Answer by rbosse · Jul 04, 2015 at 07:29 AM

Use rigidbody.MovePosition

 rigidbody.MovePosition(Vector3.MoveTowards(transform.position, "hit position", moveSpeed * Time.deltaTime));

It's what I'm doing with my game right now.

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 unity_pq4f9m5BG-5Inw · Feb 24, 2020 at 06:41 AM

Hyy @Crayz you can achieve this easily by using navmesh agent. Check the video below - https://www.youtube.com/embed/DPBdqbP8MFI

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

23 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

Related Questions

MMD How to export model and animations to Unity as 3rd person controller? 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Want to instantiate projectile in direction character is moving. 1 Answer

3rd person rigidbody controller help :( 1 Answer

Pause Menu upon Idle (timer & button issues C#) 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