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 jack_sparrow · Apr 23, 2014 at 11:25 AM · charactercontrollerdistancemove

change character controller's position on click

i am trying to move character controller towards an object(target) but i want keep distance from the target. i have used following code..in this code the character controller moves and reaches to the target. how can i keep distance between character controller and target ??

 void MoveTowardsTarget(Vector3 target) {
         var cc = GetComponent<CharacterController>();
         var offset = target - transform.position;
            
         //Get the difference.
 
         if(offset.magnitude > .1f) {
             //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;
              
             //normalize it and account for movement speed.
                            
             cc.Move(offset * Time.deltaTime);
             //actually move the character.
 
             }
     }

Thanx.

Comment
Add comment · Show 2
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 Jellezilla · Apr 23, 2014 at 11:29 AM 0
Share

Haven't used it myself, but try to have a look at joints, especially the FixedJoint I suppose.

avatar image jack_sparrow · Apr 23, 2014 at 11:40 AM 0
Share

i don't think joint will help me. when i click my character controller moves but it move direct to target, i want some distance between target and character to view target.

1 Reply

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

Answer by Ed unity · Apr 23, 2014 at 06:09 PM

I see that you have the answer to your question already in the code, you just weren't aware that it was what you are trying to achieve.

 if(offset.magnitude > .1f)

Assuming that this function is called each frame, change the .1f to some distance that you want the player character to stop from the destination point.

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 jack_sparrow · Apr 24, 2014 at 09:23 AM 0
Share

it worked thanks.. one more thing i want to make it work is that character controller covers distance according to the value of moveSpeed is set.

how can i smoothly move character controller to the object

avatar image Ed unity · Aug 21, 2014 at 09:16 PM 0
Share

@jack_sparrow my appologies for missing your comment. So long as you are using Time.deltaTime for the movement of your character and the above function is called each frame, the character should move to the destination smoothly. If you move speed is set very high, the character may appear to jitter as it moves along the surface, due to the large change in position each frame.

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

How to make a player move around a tower at a set distance? 1 Answer

‘CharacterController’ does not contain a definition for ‘Move’ 0 Answers

Using Input.GetAxis on a 2 Player Game 2 Answers

Natural Jump algorithm 2 Answers

CharacterController.Move not working in X and Z axis at the same time 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