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 Hims · Apr 24, 2014 at 10:36 AM · movecharacter controller

move character controller smoothly to the object

character controller moves towards the object when that object is clicked but it does not go near that object , it stops according to the value set in moveSpeed variable.. please go through following code. i want to move it to the object , i have set its magnitude point and it stops at specific distance. but if the object is far then i have to click more then one times to bring the character to the magnitude point.

     void MoveTowardsTarget(Vector3 target) {
         var cc = GetComponent<CharacterController>();
         var offset = target - transform.position;
 
         //target = transform.position(0,0,-distance)*moveSpeed;
         //Get the difference.
         if(offset.magnitude > 6.0f) {
 
             transform.position = targetPosition;
 //            motor.desiredMovementDirection = Vector3.zero;
             //If we're further away than .1 unit, move towards the target.
             //The minimum allowable tolerance varies with the speed of the object and the framerate. 
             // 2 * tolerance must be >= moveSpeed / framerate or the object will jump right over the stop.
             offset = offset.normalized * moveSpeed;
             //offset = offset.normalized * moveSpeed - distance + target.position;
         
             //normalize it and account for movement speed.
             //offset = Vector3.MoveTowards(offset, target , moveSpeed);
             
             cc.Move(offset * Time.deltaTime);
             //actually move the character.
             print ("in function");
 
 
             }
 
 
     }

in short the character controller covers the distance according to the value set in moveSpeed variable..it does not reaches to the objects' magnitude point..!! how can i make it reach to the object in one click.??

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

Answer by Fornoreason1000 · Apr 24, 2014 at 11:09 AM

how do you call MoveTowardsTarget(target)? i'm guessing and hoping you've set it up to run for more than one frame. the way i see it, you character might be limiting is velocity(character motor.js has limiters in it, try setting max forward speed to 9999 see if it helps) or moveSpeed is messing up dues to some coding flaws. another possible reason is the switching mechanic you designed to make the character move is being turned off too early for whatever reason.

when you character stops does you Debug.Log statement also stop running? i'd check all the things i mentioned and probably debug some of the variables like move speed and offset to see where and why they are not doing what they are supposed to.

also just a helpful tip, use SqrMagnitude instead of magnitude. the reason being is that Cartesian distances uses SquareRoots and computers do not like Square root functions(they're slow). SqrMagnitude skips the Square rooting saving your Overhead a little. just remember to compare them with the square distance e.g for your case you want a distance of 6 so you would compare SqrMagnitude with 36.

Comment
Add comment · Show 2 · 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 Hims · May 02, 2014 at 02:51 PM 0
Share

not worked...i want to count distance between the character controller and the mouse click so using that distance i can set that distance as the moveSpeed.. i want to set it as moveSpeed because character controller covers distance according to the value set in moveSpeed. please help further if u can or let me know if u want to check rest of the code. thanks..

avatar image Fornoreason1000 · May 26, 2014 at 05:32 AM 0
Share

yeah i need to know the when $$anonymous$$oveTowardsTarget is called, its kinda of hard to tell whats calling it, you need to run long enough so it reaches the target in time.

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

21 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

Related Questions

Character Keep On Moving Without Pressing Button 0 Answers

Character Controller Move returning player to the start 1 Answer

Problems with CharacterController.Move 1 Answer

Character controller move not working 0 Answers

Jumping with Character Controller?! 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