Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Paramotion · Sep 26, 2017 at 09:44 AM · inputcharacter controllerrotate objectforward

Change forward control on mouse rotation

Hello there,

I'm currently developing a character controller. I have a camera that smooths follow the player, and a script to control that player, using a character controller.

I've added that when rotating the mouse, player face always look at the pointer. In what I have, "W" is always "Camera.Forward" but now I have some trouble telling the script that "W" is always where the face of the player is looking, and "A" "D" and "S" are relative.

I'm attaching the character code and some screen-shots. Any help is welcome.

     void Start () 
     {
         charController = GetComponent <CharacterController>();
         vertSpeed = minFall;
         camera = Camera.main;
     }
     
     void Update () 
     {
         Vector3 movement = Vector3.zero;
         
         float horInput = Input.GetAxis("Horizontal");
         float vertInput = Input.GetAxis ("Vertical");
         if (horInput !=0 || vertInput !=0)
         {
            
            if(Input.GetKey(KeyCode.LeftShift)) 
             {
             movement.x = horInput * runSpeed;
             movement.z = vertInput * runSpeed;
             movement = Vector3.ClampMagnitude(movement, runSpeed);
             } 
             else
             {
             movement.x = horInput * moveSpeed;
             movement.z = vertInput * moveSpeed;
             movement = Vector3.ClampMagnitude(movement, moveSpeed);
             }
             Quaternion tmp = target.rotation;
             target.eulerAngles = new Vector3 (0, target.eulerAngles.y, 0);
             movement = target.TransformDirection (movement);
             target.rotation = tmp;
             
             Quaternion direction = Quaternion.LookRotation(movement);
             transform.rotation = Quaternion.Lerp(transform.rotation, direction, rotSpeed * Time.deltaTime);
         }
         
         movement.y = vertSpeed;
         movement *= Time.deltaTime;
         charController.Move(movement);
 
         FixedControlMouse();
     }
 
       void FixedControlMouse ()
     {
         Ray ray = camera.ScreenPointToRay (Input.mousePosition);
         Plane groundPlane = new Plane (Vector3.up, Vector3.zero);
         float rayDistance;
 
         if (groundPlane.Raycast(ray, out rayDistance))
         {
             Vector3 point = ray.GetPoint(rayDistance);
             Debug.DrawLine(ray.origin, point, Color.green);
             LookAt(point);
         }
     }
 
     void LookAt (Vector3 lookPoint)
     {
         Vector3 hcPoint = new Vector3(lookPoint.x, transform.position.y, lookPoint.z);
         transform.LookAt(hcPoint);
     }

I'm thinking about some "transform.forward" but it's not working.

alt text

alt text

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 Paramotion · Sep 26, 2017 at 03:25 PM 0
Share

So, according to this: link text https://docs.unity3d.com/ScriptReference/CharacterController.$$anonymous$$ove.html I need to create some kinda function that checks if the character is facing the "X axis", to move "W" in the "Y axis", and the opposite if the character is facing "Y axis"?

I've seen that rotating and moving a rigidbody it's much easier, but the project requires a CharacterController.

0 Replies

· Add your reply
  • Sort: 

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

79 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Moving forward towards the cursor with LookAtMouse? 0 Answers

Character controller Joystick issues. 1 Answer

How to rotate a Character Controller on the X or Z axis ? (Not for movement) 0 Answers

move character controller forward using gui button. 1 Answer

Precise rotation based on joystick axis input for twin-stick controls 3 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