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 Lemmster · Sep 06, 2014 at 03:55 PM · movementvector3charactercontrollerquaternionrelative

CharacterController, Move toward mouse

Hi All,

I have a 2D top down game where the character needs to follow the mouse cursor, but also strafe in world space.

I have managed to do either one of these things, but putting the two together is causing some headaches.

I've almost managed to get it working but I'm stuck with how the CharacterController.SimpleMove uses its argument in world space.

Because the character follows the mouse, I need to add relative movement based on which way the character is facing.

//Code C#

     void Update () {
         cControl = GetComponent<CharacterController>();
         lookPoint = GameCore.lookTarget; //static val, contains mouse position in world space
         lookPoint.y = transform.position.y; //remove any y drift from distance    
         transform.LookAt(lookPoint);
         calMov();
     }
     
     void calMov(){
         float xAxis = (Input.GetAxisRaw("Horizontal") * walkSpeed);
         float zAxis = (Input.GetAxisRaw("Vertical") * walkSpeed);
         float mouseDist = Vector3.Distance(transform.position, lookPoint);
 
     if(Input.GetMouseButton(1)){
         if(mouseDist > 4){mouseDist = runSpeed;}
         else{mouseDist = walkSpeed;}
     }
     else{mouseDist = 0;}
 
         Vector3 movement = new Vector3(xAxis, 0, zAxis);
         Vector3 relForward = new Vector3 (0,0,mouseDist);
         relForward = transform.InverseTransformDirection(relForward);  //Problem here.            movement += relForward;
         movement = Vector3.ClampMagnitude(movement, runSpeed);
         cControl.SimpleMove(movement);
     }

I'm currently using InverseTransformDirection, but while this makes the character follow when parallel to the world Z axis. It inverts and moves back from the mouse when facing world X.

I've tried more solutions to this than I care to count and none produce the desired result. Effectively what I am trying to achieve is the equivalent of RigidBody.AddRelativeForce(new Vector3(0,0,forward)); This gives the desired moving toward mouse, but then I run into a whole heap of problems controlling speed.

Ideally I'd like to get to the following:

  1. The character to face and run after the mouse cursor.

  2. Strafe in world space via WASD regardless of direction character is facing.

  3. Clamp the velocity of the character so the total magnitude never exceeds runSpeed.

I've seen some examples of rotating a Vector3 to face a direction, but all I can get out of that approach is compiler errors.

Help with any of these goals would be greatly appreciated.

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
0

Answer by twobob · Nov 20, 2014 at 06:32 AM

Did you fix this.

Pretty sure this is covered in the new Sample Assets package coming up. The rotaton maths aspects most certainly are. I used the 3rd Party controller out of there to more or less do what you describe.

I had to add a strafe myself but it was something trivial IIRC like a call to controller.Move(strafeMagnitude,pos.y,pos.z)

Where strafeMagnitude was some factor I had set; then applied after taking note of players Vector3.forward, post the rotation calculations.

However, that was dirty of me, Generally it is not a great idea to mix physics and direct positioning calls (walk through walls etc) and should you wish to control via physics one way to keep down the velocity is to apply a %age of the exact opposite velocity when rigidbody.velocity.magnitude exceeds a certain threshold, this too works okay.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to get our character controller script to make our player move? 1 Answer

Rotate a calculated motion 1 Answer

3D Character Controller slowing down the higher the slope angle (both up and down) 1 Answer

Calculate vector exactly between 2 vectors 4 Answers

Object not moving in forward direction 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