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 Phenelo · Dec 18, 2013 at 01:35 AM · character controlling

How to move Character Controller from point-to-point absolutely

I'm working on a game like Subway Surf. I'm having trouble when my character needs to change lane. It won't stop at the absolute point of the target lane. If I use the Translate to snap the character to the target position, it ignores the colliders resulting my character to go through the walls. So I used Move but can't seem to get it right. Can someone help me with this?

EDIT: So here's my workaround. The script for changing lane and the running movement were in a separate script.

 public class ChangeLane : MonoBehaviour {
 
     public bool toChangeLane, toLeft, toRight;
     public bool isChangingLane;
     public Vector3 targetPosition;
     public float laneDistance;
     
     
     private CharacterController dogCtrl;
     
     void Awake()
     {
         dogCtrl = GetComponent<CharacterController>();
         targetPosition = new Vector3(transform.position.x - 2, transform.position.y, transform.position.z);
     }
     
     void Update()
     {
         if(toChangeLane)
         {
             if(toLeft)
             {
                 targetPosition = new Vector3(transform.position.x - laneDistance, transform.position.y, transform.position.z);
                 toLeft = false;
             }
             else if(toRight)
             {
                 targetPosition = new Vector3(transform.position.x + laneDistance, transform.position.y, transform.position.z);
                 toRight = false;
             }
             isChangingLane = true;
             toChangeLane = false;
         }
         
         else if(isChangingLane)
         {
             MoveToPoint();
         }
     }
     
     void MoveToPoint()
     {
         Vector3 movDiff = targetPosition - transform.position;
         if(movDiff.magnitude > .1f)
         {
             movDiff = movDiff.normalized * 15f;
             dogCtrl.Move(movDiff * Time.deltaTime);
         }
         else
         {
             transform.position = targetPosition;
             isChangingLane = false;
         }
         Debug.Log(movDiff.magnitude);
         //targetPosition = new Vector3(targetPosition.x, transform.position.y, transform.position.z);
     }
     
 }

And here is the snippet from my controller that processes the running.

 ...
     Vector3 movement = moveDirection * runningSpeed + new Vector3 (0f, jumpingSpeed * 3f, 0f);
 //moveDirection = forward
         movement *= Time.deltaTime;
         collisionFlags = dogCtrl.Move(movement);
 ...

It works fine except:

  1. The character falls under the ground and jump right over the target position with the code below uncommented
    targetPosition = new Vector3(targetPosition.x, transform.position.y, transform.position.z);

  2. The character swipes to the target lane a few distance backwards. It's so noticeable since my camera follows my character

  3. And yeah, the character goes through the wall if I manipulate the transform instead of Move because my goal is to bounce back from the former lane if the character collided with an obstacle halfway its changing lane.

  4. The final position is not absolute so you can see the character not aligned in the center of the lane.

I can add more details if this is not enough and thank you for any response. :)

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 robertbu · Dec 18, 2013 at 02:02 AM 0
Share

Without your script and more information, it is difficult to give a focused answer. Here is some logic that might be in the right direction.

 var dir = lanePosition - transform.position;
 var moveDir = dir.normalized * Time.deltaTime * speed;
 if (moveDir.sqr$$anonymous$$agnitude > dir.sqr$$anonymous$$agnitude) {
     moveDir = dir;
 }

With forward movement and lane changes, your code will be more complicated. Posting your code might help someone give a focused answer to your question.

avatar image Phenelo · Dec 18, 2013 at 03:13 AM 0
Share

Edited my post. :)

1 Reply

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

Answer by robertbu · Dec 18, 2013 at 04:32 AM

Looking at just MoveToPoint() and taking the logic I posted into the comment and integrating it into your code:

    void MoveToPoint()
     {
        if (targetPosition == transform.position)
            return;
 
        Vector3 movDiff = targetPosition - transform.position;
        Vector3 movDir = moveDiff.normalized * 15f * Time.deltaTime;
        if(moveDir.sqrMagnitude < moveDiff.sqrMagnitude)
        {
          dogCtrl.Move(movDir);
        }
        else
        {
           dogCtrl.Move(movDiff)
        }
     }
Comment
Add comment · Show 6 · 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 Phenelo · Dec 18, 2013 at 08:21 AM 0
Share

If I understand this right, we move the character to the target position until the position of the character is equal to the target position. I'm quite confused about the dogCtrl.$$anonymous$$ove(movDiff). Does that mean that if the character exceeds the target position, we move back?

avatar image robertbu · Dec 18, 2013 at 08:44 AM 2
Share

'movDiff' is a vector from the current position to the end position. 'movDir' calculated move for the frame based on deltaTime. This code says that if a move the whole way (movDiff) is shorter than 'movDir', then we use the shorter move. In other words, if using the calculate move (movDir) would overshoot the end position, we use the shorter move directly to the end position.

avatar image Phenelo · Dec 18, 2013 at 08:55 AM 0
Share

This works perfectly. One problem left is that the z value here is somehow stuck until the end of the lane change (if I'm right). The running and change lane script runs separately. How can I tweak this. Should I just combine the script and do the move calculation once?

avatar image robertbu · Dec 18, 2013 at 09:02 AM 2
Share

Since you move over time, I'd update the 'z' parameter of targetPosition each frame. One script or two depends on the structure of your game.

avatar image Phenelo · Dec 18, 2013 at 09:06 AM 0
Share

Yes, it worked. Thanks. :))

Show more comments

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

18 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

Related Questions

Can't animate my legacy character 0 Answers

Character Controller's performance for massive groups 0 Answers

tweak animated character joints angles at runtime 0 Answers

Unable to locate Character Controller for sprinting. 1 Answer

character controller script 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