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 gargleyark · Aug 12, 2013 at 08:27 PM · javascriptcharactercharactercontrollerspeedsimplemove

How can I implement a SimpleMove max speed?

I am using the SimpleMove() function to create a click to move character controller and it works fine as long as the user clicks near to the player, but if the player clicks far in the distance then they move very far very quickly. I would like to implement a maximum speed and have tried to create an if statement to limit the value being entered into the SimpleMove() function, but to no avail, any help would be appreciated!

The code is as follows:

 var speed: float = 5; // Determines how quickly object moves towards position
 
 private var targetPosition:Vector3; // current target pos
 private var character: CharacterController;
 
 function Start(){
     character = GetComponent(CharacterController);
     targetPosition = transform.position;
 }
 
 function Update () {
     if(Input.GetKey(KeyCode.Mouse0)){
         // create a logical horizontal plane at the player position:
         var playerPlane = new Plane(Vector3.up, transform.position);
         var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         var hitdist = 0.0;
         // find point clicked in the plane (if any):
         if (playerPlane.Raycast (ray, hitdist)) {
             // update targetPosition to the clicked point: 
             targetPosition = ray.GetPoint(hitdist);
             var dir = targetPosition - transform.position;
             dir.y = 0; // keep only the horizontal direction
         }
     }
     // always try to move the character to targetPosition:
     character.SimpleMove(dir * speed); // move taking gravity into account
 }
Comment
Add comment · Show 1
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 ZenithCode · Aug 12, 2013 at 08:36 PM 0
Share

Can you post the Simple$$anonymous$$ove method ?

1 Reply

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

Answer by robertbu · Aug 12, 2013 at 09:16 PM

To get your controller to move at a fixed speed, you want to normalize your dir vector. Below line 22 you would put:

 dir.Normalize();

You may only want to fix the speed if the distance is above some threshold. That code would look like:

 if (dir.magnitude > someValue)
    dir = dir.normalized * someValue;
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 gargleyark · Aug 13, 2013 at 10:53 AM 0
Share

Perfect! Thanks very much, for anyone else with interest in this answer - I found 5 to be a good value for running speed

avatar image Linus · Aug 13, 2013 at 11:11 AM 0
Share

Do not forget to mark question as answered

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

17 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

Related Questions

Run speed in world-based character controller 0 Answers

Increasing Camera "Bobbing" speed when character is sprinting 1 Answer

How to fix? Character grabbing system 1 Answer

How can I change the speed of a character controller on the fly? 1 Answer

Move() function doesn't work on character controller! 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