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 /
  • Help Room /
avatar image
0
Question by hectorales · Jan 31, 2017 at 11:43 PM · rotationcharactercontrollerprogrammingmovement script

Stop player going up when camera looks up.

Hello, i'm having a little problem, I have a personal third person character controller, made with multiple tutorials. I want it to be like the ones on Zelda or Super Mario, where de camera follows the player and the player rotates around the camera.

All of that is already done but I came up with a problem in the movement of the player, it moves at where the camera is looking at, which is good but when the camera looks up, the player starts going up like flying, it also goes slower when the camera looks down.

By experimentation its clearly the code of movement, so my question is, how do I impede the player from going up or down when the camera looks up and down?

Here's the code:

 void FixedUpdate()
     {
         CheckGrounded(); //CHeck if player is on ground or not.
 
         //Inputs
         horizontal = Input.GetAxis("Horizontal");
         vertical = Input.GetAxis("Vertical");
         jumpInput = Input.GetButtonDown("Jump");
 
         /*
             This if statment is how tolerant we are on changing the direction based on where the camera is looking.
             For example, if the player is moving to the left/right of where the camera is looking and then he rotates the camera
             so it looks towards where he is going, we will keep moving at the same direction as before
         */
         storDir = cam.right;        //This means, the player can keep moving in the same direction they were before even if they change the camera angle
         anim.SetBool("OnAir", onGround);
         if (Input.GetKey(KeyCode.LeftAlt))
         {
             speed = 0.3f;
         }
         else
             speed = 0.8f;

          rigidBody.AddForce((storDir * horizontal) + (cam.forward * vertical) * speed / Time.deltaTime);

         if (onGround)        //Jump!, does not rotate
         {
             //Physics.gravity = Vector3.down * 9.8f;
 
             //Jump controls
             if (jumpInput && onGround)
             {
                 
                 anim.SetTrigger("Jump");
                 rigidBody.velocity = new Vector3(rigidBody.velocity.x, 1 * jumpPower, rigidBody.velocity.z);      //ForceMode.Impulse (I think) gives all the force to the jump for only one frame.
             }
         }
         else
         {
             rigidBody.AddForce(Vector3.down * extrGravity, ForceMode.Force);
             //Physics.gravity = Vector3.down * extrGravity;
         }
        
             
 
         /*Rotates the Character*/
         //Find a position in front of where the camera is looking
 
 
         directionPos = transform.position + (storDir * horizontal) + (cam.forward * vertical);
         //Find the direction from that position
         Vector3 dir = directionPos - transform.position;
         Vector3 dir2 = - directionPos + transform.position; //Evade rotation when walking bacwards
         dir2.y = 0;
         dir.y = 0;  //The player should not be bouncing up and down.
 
         // Turn the input animator values, since our blend three is directoinal and can only go up to 1
         float animvalue2 = horizontal;
        float animValue = vertical;
         if (Input.GetKey(KeyCode.LeftAlt))
         {
             animValue *= 0.5f;
             animvalue2 *= 0.5f;
         }
         else
         {
             animValue = vertical;
             animvalue2 = horizontal;
         }
         // Pass the value of the animator
         anim.SetFloat("Forward", animValue, .1f, Time.deltaTime);
         anim.SetFloat("Rotate", animvalue2, .1f, Time.deltaTime);
 
         //If the player has been given input, we move!
         if (horizontal != 0 || vertical != 0)
         {
             //find the angle, between the character's rotation and where the camera is looking
             float angle = Quaternion.Angle(transform.rotation, Quaternion.LookRotation(dir));
 
             if (vertical < 0)
                 rigidBody.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(dir2), turnSpeed * Time.deltaTime);
             else
             // if angle it's not zero (to avoid a warning)
             if (angle != 0)   //look towards the camera
             {
                 rigidBody.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(dir), turnSpeed * Time.deltaTime);
             }
         }
     }




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 Scoutas · Feb 01, 2017 at 12:03 AM

Line 24: rigidBody.AddForce((storDir * horizontal) + (cam.forward * vertical) * speed / Time.deltaTime);

instead of just adding cam.forward create a new vector new Vector2 (cam.forward.x, 0, cam.forward.z).normalized and then multiply by the vertical input. This way you are ensuring that the direction vector is only on the x and z axis.

Before, when you were looking up or down, the player was given a certain force on the y axis. And if you look up, or down, the x and z components of that vector are really small, so that's why the player moved so slowly when looking down (and probably up).

Comment
Add comment · Show 1 · 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 hectorales · Feb 01, 2017 at 04:21 AM 0
Share

Thank you so much, that actually works! You are the real $$anonymous$$VP.

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

117 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 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

[SOLVED] - Issues with Character Controller and Moving Platform 1 Answer

i'm using a code to make my cube walk with character controller but when i play the cube spins. 0 Answers

Rotating, moving and keep on going with constant velocity in direction of click 1 Answer

NPCs to look in the direction the target is 0 Answers

Character Controller Rotate to turn at a bend 0 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