Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 /
avatar image
0
Question by Major · Sep 06, 2014 at 05:50 AM · physicsrigidbodycharactercharactercontrollercontroller

RigidBody Character Controller Bug

I have been working on this character controller for a while, but there is a bug that has stumped me for several weeks now. What I am trying to achieve is when the character falls, say off of a ledge, it will fall over and when it hits the ground roll around a bit slow down and finally get up. What happens at the moment is that the character can fall of a ledge, act like a rigidbody, but when it hits the ground it just snaps back up instantly. I don't have any idea what is going on here. Code:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerControl : MonoBehaviour {
 
     public float acceleration = 10;
     public float maxSpeed = 5;
     public float jump = 150;
     public bool grounded;
     public Transform camera;
     public float lerp = 1;
     public float fallSpeed = 10;
     public float health = 100;
     public float fallDamage = 0.1f;
     public bool fall;
     public float damage;
 
     float mouseSensitivity;
     float horizontalRot = 0.0f;
     float currentYRot;
     float yRotV;
     public float maxSlope = 70.0f;
     Vector2 horizontalMove;
 
 
     void Start () 
     {
         mouseSensitivity = camera.GetComponent<CameraControl>().mouseSensitivity;
     }
     
     void Update () 
     {
         if(Physics.Raycast(transform.position, new Vector3(0, -1, 0), 1))
         {
             Grounded();
             fall = false;
             Move();
         }
         else
         {
             grounded = false;
             Fall();
         }
         
 
         //if(rigidbody.velocity.magnitude >= maxSpeed + 3)
         //{
         //    Fall();
         //}
 
         //horizontalRot += Input.GetAxis("Mouse X") * mouseSensitivity;
         //currentYRot = Mathf.SmoothDamp(currentYRot, horizontalRot, ref yRotV, smoothDamp);
         if(!fall)
         {
             transform.rotation = Quaternion.Euler(transform.rotation.x, camera.transform.rotation.eulerAngles.y, transform.rotation.z);
         }
 
 
         horizontalMove = new Vector2(rigidbody.velocity.x, rigidbody.velocity.z);
         if(horizontalMove.magnitude > maxSpeed)
         {
             horizontalMove = horizontalMove.normalized;
             horizontalMove *= maxSpeed;
         }
         rigidbody.velocity = new Vector3(horizontalMove.x, rigidbody.velocity.y, horizontalMove.y);
     }
 
     void Fall()
     {
         rigidbody.constraints = RigidbodyConstraints.None;
         grounded = false;
         fall = true;
 
         //while(rigidbody.velocity.magnitude <= 0.2f)
         //{
         //    gameObject.transform.eulerAngles = new Vector3(0, gameObject.transform.eulerAngles.y, 0);
         //    fall = false;
         //}
     }
 
     void Grounded()
     {
         rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
         grounded = true;
     }
 
     void Move()
     {
         if(grounded && !fall)
         {
             rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * acceleration, 0, Input.GetAxis("Vertical") * acceleration);
             
             if(Input.GetKeyDown(KeyCode.Space))
             {
                 transform.rigidbody.AddForce(Vector3.up * jump);
             }
         }
     }
 }

Any answer that leads in the right direction is appreciated, thanks!

Comment
Add comment · Show 4
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 zharik86 · Sep 06, 2014 at 07:27 AM 0
Share

In your condition "if(!fall)" you correct not work with Quaternion. Quaternion is four component vector. Each its value has the range from 0 to 1. But function Quatenion.Euler(x, y, z) converts rotation angles of Euler in degrees to a quaternion. In most cases, the value range x, y and z will be from 0 to 360.

avatar image Major · Sep 06, 2014 at 06:13 PM 0
Share

The problem with that method is that the player won't rotate at all any more. Physics will act on it, but when the player starts rotating it gets rotation set back to 0. It just ends up as a bunch of jittering.

avatar image zharik86 · Sep 06, 2014 at 06:50 PM 0
Share

Certainly, player won't rotate. You as Grounded() forbid rotations through rigidbody.

avatar image Major · Sep 06, 2014 at 06:58 PM 0
Share

Even if I don't freeze the rotation, the player won't rotate.

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

22 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

Related Questions

jet pack physics with character controller 1 Answer

Character Controller Jittering 0 Answers

Character Controller children of a car: Inertia effect, delay effect, what??? 1 Answer

Character controller jittering up and down 2 Answers

Rigidbody character controller issues 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