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 Diemx · Mar 13, 2018 at 04:54 PM · scripting beginnerjumpingcamera follow

Camera judders when I press jump in midair

I'm a total beginner at Unity but I managed to frankenstein together a mostly functional 3D Player Controller script including jumping, camera following the player and the player moving and rotating relative to the camera position. However if I hit the jump (space) button while the player gameobject is in midair the camera suddenly jumps upwards. Every fix I tried prevented the player from jumping at all while a direction is pushed. Could someone please help?

  void Move(float lh, float lv, float jmp)
     { 
         
             movement.Set(lh, jmp, lv);
             movement = Camera.main.transform.TransformDirection(movement);
         
             bool isGrounded = CheckGrounded();
             // Check if the player is pressing the jump key
             if (jmp > 0f)
             {
                 // Make sure we've not already jumped on this key press
                 if (!pressedJump && isGrounded)
                 {
                     // We are jumping on the current key press
                     pressedJump = true;
 
                     // Jumping vector
                     Vector3 jumpVector = new Vector3(0f, jumpSpeed, 0f);
 
                     // Make the player jump by adding velocity
                     playerRigidBody.velocity = playerRigidBody.velocity + jumpVector;
 
                     
                 }
              
             }
             else
             {
                 // Update flag so it can jump again if we press the jump key
                 pressedJump = false;
                 
             }
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 TreyH · Mar 13, 2018 at 05:50 PM 0
Share

$$anonymous$$ight need a bit more info to help out. Where is your camera in the hierarchy? Where is this $$anonymous$$ove() function being called?

avatar image Diemx TreyH · Mar 13, 2018 at 10:06 PM 0
Share

Added a reply with more information

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Diemx · Mar 13, 2018 at 10:05 PM

Move is being called in FixedUpdate (I'm not 100% sure whether that's the right option compared to just Update):

 void FixedUpdate()
     {
         float lh = Input.GetAxisRaw("Horizontal");
         float lv = Input.GetAxisRaw("Vertical");
         float jmp = Input.GetAxis("Jump");
 
         Move(lh, lv, jmp);
     }

For the camera I'm using a script grabbed from elsewhere:

 using UnityEngine;
 using System.Collections;
 
 public class SmoothFollow : MonoBehaviour
 {
 
     // The target we are following
     public Transform target;
     // The distance in the x-z plane to the target
     public float distance = 10.0f;
     // the height we want the camera to be above the target
     public float height = 5.0f;
     // How much we 
     public float heightDamping = 2.0f;
     public float rotationDamping = 3.0f;
 
     // Place the script in the Camera-Control group in the component menu
     [AddComponentMenu("Camera-Control/Smooth Follow")]
 
     void LateUpdate()
     {
         // Early out if we don't have a target
         if (!target) return;
 
         // Calculate the current rotation angles
         float wantedRotationAngle = target.eulerAngles.y;
         float wantedHeight = target.position.y + height;
 
         float currentRotationAngle = transform.eulerAngles.y;
         float currentHeight = transform.position.y;
 
         // Damp the rotation around the y-axis
         currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
 
         // Damp the height
         currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);
 
         // Convert the angle into a rotation
         var currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);
 
         // Set the position of the camera on the x-z plane to:
         // distance meters behind the target
         transform.position = target.position;
         transform.position -= currentRotation * Vector3.forward * distance;
 
         // Set the height of the camera
         transform.position = new Vector3(transform.position.x, currentHeight, transform.position.z);
 
         // Always look at the target
         transform.LookAt(target);
     }
 }

The only change I made to the MainCamera in the Hierarchy is adding this script component and its variables and setting the tag to MainCamera

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

78 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

Related Questions

Does anyone know why this simple player camera lock script is not working? 0 Answers

Infinite jumping problem: Jump function (kinda) stops calling when camera follow script applied 0 Answers

Problems with Gyroscope VR Car Game 0 Answers

How to get the camera's size in world units 1 Answer

Camera rotating for rotating object 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