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 car104 · Apr 30, 2013 at 08:42 PM · javascriptmovementcharactercontrollercontrollerclicktomove

Character Controller moves forever after colliding with object

I am working on a click-to-move character controller. I'm trying to learn the concepts behind it. So far, everything I have works perfectly, save for one thing. In my scene, I have three cube gameObjects on the scene. One Cube has my script connected to it, the other two are there just as obstacles.

The problem I have is, at some times, if my cube I'm controlling collides with another cube, when it finally gets around it, it at times continues travelling forever.

Please forgive me for not knowing how to format the code to highlight syntax and such.

Here is my code I have:

 var speed : int = 3;
 var targetPosition : Vector3;
 var travelDistance : float = 0.0;
 var curSpeed : float;
 var defaultSpeed : int = 3;
 
 
 function Start () {
     targetPosition = transform.position;
 }
 
 @script RequireComponent(CharacterController)
 function Update () {
     var character : CharacterController = GetComponent("CharacterController");
     travelDistance = Vector3.Distance(transform.position, targetPosition);
     
     if(travelDistance < .75){
         speed = 0;
     }
     else{
         speed = defaultSpeed;
     }
     //On Mouse left Click
     if(Input.GetMouseButton(0)){
         //Create a plane for the RayCast to hit
 
         var playerPlane : Plane = new Plane(Vector3.up, transform.position);
         var myRay : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         
         //variable for Raycast hit distance
         var hitDist : float = 0.0;
         
         //check if the Raycast hits the plane
         if(playerPlane.Raycast(myRay, hitDist)){
             //create the target Vector where the plane hit
             targetPosition = myRay.GetPoint(hitDist);
             transform.LookAt(targetPosition);
         }
 }
 
     if(travelDistance > .75){
             curSpeed = speed * Time.deltaTime;
             var forward : Vector3 = transform.TransformDirection(Vector3.forward);
             character.Move(forward * curSpeed);
         }
     Debug.Log(travelDistance);
 
 }

I don't know why it's doing that. If anyone can help me understand why sometimes the cube will just travel forever after colliding with another, I'd really appreciate it.

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
4
Best Answer

Answer by car104 · May 01, 2013 at 02:07 PM

Just in case anyone ever comes across this, because I hate finding resolved issues with no answer provided. I have solved this problem. The issue was, when the character(cube) collided with another object, it would offset itself in one direction or the other to get around the object. As my code shows, the movement is controlled by finding a point the ray hit, turn the object to face that point and then move forward until it reaches that point, or within .5m of it. If the character's position is offset more than .5m after colliding with an object, it never gets within .5m distance of the destination, and thus continues travelling forever. I only had the object turn to look at the point the ray hit on mouse click, and thus it didn't update as it moved itself. To remedy this, I told the character to rotate toward the destination point every update.

Now if the position of the character gets offset by a collision, it'll rotate toward the point and continue moving toward it and finally stopping.

Comment
Add comment · 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

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

13 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

Related Questions

CharacterController unexpectedly moving up 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Gradual Speed Increase with Variable Input 0 Answers

How to move to specific point with character controller 1 Answer

CharacterController NullReferenceException dispite attached to object 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