Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 DontKnow96 · Mar 11, 2020 at 04:41 PM · charactercontrollercharacter movementridigbody

Character Controller behaves differently when a Ridigbody is in the scene

Hello and thank you in advance for helping me out,

i have a weird bug where the Speed at which my character is moved, seems to change a little bit as soon as there is a Rigidbody in the scene. This bug still occurs when i run a built.

I am moving my Character in FixedUpdate() with with Time.fixedDeltaTime and Time.DeltaTime where appropriate.

 public class Controller : MonoBehaviour
 {
     public Transform playerTransform;
     public Camera cameraPlayer;
     public CharacterController characterController;
     public string[] walkTags;
 
     public float mouseSensitivity = 100f;
     public float gravity = -9.1f;
 
     public float jumpHeight = 1.7f;
     public float moveAcceleration = 50;
     public float moveSpeed = 10;
     public float constantDrag = 40;
 
     private float mouseX;
     private float mouseY;
 
     private float xRotation;
 
     public Vector3 velocity;
 
     protected bool jump = false;
     private bool isOnGround = false;
 
     private Vector2 moveDirection;
 
     void Start()
     {
         xRotation = playerTransform.rotation.x;
         Cursor.lockState = CursorLockMode.Locked;
     }
 
 
     void Update()
     {
 
         if (isOnGround && Input.GetKey("space"))
         {
             jump = true;
         }
 
         mouseX += Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
         mouseY += Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
 
         moveDirection.x = Input.GetAxis("Horizontal");
         moveDirection.y = Input.GetAxis("Vertical");
 
         moveDirection = Vector2.ClampMagnitude(moveDirection, 1);
     }
 
 
 
     void FixedUpdate()
     {
         if (jump)
         {
             velocity.y += Mathf.Sqrt(jumpHeight * -2 * gravity);
             jump = false;
         }
 
         //rotate camera and player
         xRotation -= mouseY;
         xRotation = Mathf.Clamp(xRotation, -90f, 90f);
         cameraPlayer.transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
         playerTransform.Rotate(Vector3.up * mouseX);
 
         mouseX = 0;
         mouseY = 0;
 
 
         //apply movement
         float verticalVelocity = velocity.y;
         velocity.y = 0;
 
 
         velocity += transform.forward * moveDirection.y * moveAcceleration * Time.fixedDeltaTime
                         + transform.right * moveDirection.x * moveAcceleration * Time.fixedDeltaTime;
 
         velocity = Vector3.ClampMagnitude(velocity, moveSpeed);
 
 
         velocity.y = verticalVelocity;
 
 
         //apply gravity
         velocity += transform.up * gravity * Time.fixedDeltaTime;
 
         //apply drag
         verticalVelocity = velocity.y;
         velocity.y = 0;
 
         float signX = Mathf.Sign(velocity.x);
         float signZ = Mathf.Sign(velocity.z);
 
 
         velocity -= constantDrag * Time.fixedDeltaTime * velocity.normalized;
 
 
 
         if (signX != Mathf.Sign(velocity.x))
             velocity.x = 0;
 
         if (signZ != Mathf.Sign(velocity.z))
             velocity.z = 0;
 
 
         velocity.y = verticalVelocity;
 
 
         //move character
         isOnGround = false;
 
         characterController.Move(velocity * Time.fixedDeltaTime);
     }
 
 
     void OnControllerColliderHit(ControllerColliderHit hit)
     {
         foreach (string tag in walkTags)
         {
             if (string.Equals(hit.collider.tag, tag))
             {
                 velocity = Vector3.ProjectOnPlane(velocity, hit.normal);
 
                 if ((characterController.collisionFlags & CollisionFlags.Below) != 0)
                 {
                     isOnGround = true;
                 }
             }
         }
     }
 }
 
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

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

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

Jumping with slight air control while retaining velocity 1 Answer

Character moves opposite ways.,My character moves perfectly to every direction except -z. 0 Answers

Issues with making a climbing script compatible with CharacterController 0 Answers

Can't flip the character other way around 1 Answer

How can I modify this to allow jumping with space? 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