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 Persious · Feb 16, 2015 at 09:50 AM · raycasttransformcharactercontroller

Unity Character Controller messing up.

I'm trying to make a click-to-move system, but it seems like whenever I press somewhere on the map, the character goes to the place, but doesn't stop, yet goes crazy. (Moving around in circles etc.)

If I increase the number in the vector3.Distance, it'll eventually stop going crazy, but the position will be far off.

 // Current selected player
     public GameObject curPlayer;
     CharacterController controller;
 
     public float speed = 5.0f;
     public Vector3 position;
 
     void Start ()
     {
         controller = curPlayer.GetComponent<CharacterController>();
 
         position = curPlayer.transform.position;
     }
 
     void Update()
     {
         if (GameManager.gameRunning) 
         {
             if (Input.GetMouseButtonUp(1))
             {
                 controller = curPlayer.GetComponent<CharacterController>();
 
                 locatePosition();
             }
             moveToPosition();
         }
     }
 
     void locatePosition()
     {
         RaycastHit hit;
         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 
         if (Physics.Raycast(ray, out hit))
         {
             position = hit.point;
         }
     }
 
     void moveToPosition()
     {
         if (Vector3.Distance(curPlayer.transform.position, position) > 1)
         {
             Quaternion newRotation = Quaternion.LookRotation(position-curPlayer.transform.position, Vector3.forward);
 
             newRotation.x = 0f;
             newRotation.z = 0f;
 
             curPlayer.transform.rotation = Quaternion.Slerp (curPlayer.transform.rotation, newRotation, Time.deltaTime * 10);
             controller.SimpleMove(curPlayer.transform.forward * speed);
         }
     }
Comment
Add comment · Show 3
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 meat5000 ♦ · Feb 16, 2015 at 01:21 AM 0
Share

The chances of hitting an exact float based Vector3 are pretty slim; floating point inaccuracy and that.

You also kinda need to tell it to stop, I imagine. Like turning off the engine when you've reached the shops :P

avatar image Wolfdog · Feb 16, 2015 at 10:48 AM 0
Share

as meat5000 already said, you need something like

 if (your_current_position is < 1 meter from the target) {
     stop_the_movement
 }

Checking if the position is exactly your target position is almost impossible. Your player 'goes crazy' because he over-shoots his target, turns around to get to it again, but over-shoots it again. It then does this over and over and over again.

avatar image Persious · Feb 16, 2015 at 01:50 PM 0
Share

Aha, I'll try that. Thanks!

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Want to move object slowly to where the mouse clicks? 1 Answer

Difference between hit.collider.gameObject vs hit.transform.gameObject 2 Answers

Look at enemy 1 Answer

How to teleport a character to a given position? 2 Answers

Set forward vector of an object to be EXACTLY equal to another vector. 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