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 Ochreous · Aug 10, 2013 at 06:07 AM · c#charactercontrollersimplemove

C# CharacterController.SimpleMove Goes on Forever

Hi everyone, I'm having a bit of trouble with CharacterController.SimpleMove . It goes on Forever and I'm trying to come up with an if statement to stop it. But I'm having a hard time coming up with one. I want CharacterController.SimpleMove to stop once transform.position is at the same spot as targetPosition. I've tried if(transform.position != targetPosition) but that's not stopping it like I hoped. Any idea what's wrong with my script?

 using UnityEngine;
 using System.Collections;
  
 public class moveOnMouseClick : MonoBehaviour {
 public float speed = 5; // Determines how quickly object moves towards position
 
 public Vector3 targetPosition; // current target pos
 public CharacterController character;
 public Vector3 dir;
 void Start(){
     //character = gameObject.GetComponent(CharacterController);
     targetPosition = transform.position;
 }
 
 void Update () {
     if(Input.GetKeyDown(KeyCode.Mouse0)){
         // create a logical horizontal plane at the player position:
         Plane playerPlane = new Plane(Vector3.up, transform.position);
         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         float hitdist = 0.0f;
         // find point clicked in the plane (if any):
         if (playerPlane.Raycast (ray, out hitdist)) {
             // update targetPosition to the clicked point: 
             targetPosition = ray.GetPoint(hitdist);
             dir = targetPosition - transform.position;
             dir.y = 0; // keep only the horizontal direction
             transform.rotation = Quaternion.LookRotation(dir);
         }
     }
     // always try to move the character to targetPosition:
     if(transform.position != targetPosition){
     character.SimpleMove(dir); // move taking gravity into account
         }
     }
 }
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
1
Best Answer

Answer by robertbu · Aug 10, 2013 at 06:19 AM

The chance that you will hit the target exactly is very small. That is, at each frame your character controller take a discrete step forward in the direction of the target. In one frame is will be before the target, in the next frame it will be beyond the target. One solution is to change your check to some threshold:

 if ((transform.position - targetPosition).sqrMagnitude > some_small_threshold) {
     character.SimpleMove(dir); // move taking gravity into account
 )
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 Ochreous · Aug 10, 2013 at 06:26 AM 0
Share

What would be the closest amount I could get to the actual target? I gave the threshold 0.1f and that seems to be pretty close

 if ((transform.position - targetPosition).sqr$$anonymous$$agnitude > 0.1f)
avatar image robertbu · Aug 10, 2013 at 07:12 AM 0
Share

How close will depend on your your frame rate and speed. If you were not using Simple$$anonymous$$ove(), you could detect when you would step beyond the end point and just set it to the end point, but Simple$$anonymous$$ove does its own Time.deltaTime calculations.

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

14 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

Related Questions

C# FP_Camera not following Character Controller 0 Answers

C# Need Help converting from Rigidbody to Character Controller 0 Answers

C# Character Controller Collision If Statement 2 Answers

Converting C# Controller from WASD Controls to Click-Based 1 Answer

C# How to Access Character Controller's Properties without Declaring a 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