• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

How to use character controller to push down hinge joint properly 1 Answer

Character controller jittering up and down 2 Answers

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

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges