• 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
Question by awplays49 · Sep 19, 2015 at 02:45 AM · physicsgravityjitterjitteringjittery

Gravity controller is buggy

This is a controller I scripted:

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent (typeof (PlayerControllerHandler))]
 public class PlayerController : MonoBehaviour {
 
     public float gravity;
     public float moveSpeed;
     private Vector3 velocity;
     private float currentGravity;
     private PlayerControllerHandler pch;
     
     void Start () {
         pch = GetComponent <PlayerControllerHandler> ();
     }
     
     void Update () {
         currentGravity = velocity.y;
         velocity = Vector3.zero;
         velocity.y = currentGravity;
         if (Input.GetKey (KeyCode.W))
         {
             velocity += transform.forward;
         }
         else if (Input.GetKey (KeyCode.A))
         {
             velocity -= transform.right;
         }
         if (Input.GetKey (KeyCode.D))
         {
             velocity += transform.right;
         }
         else if (Input.GetKey (KeyCode.S))
         {
             velocity -= transform.forward;
         }
         velocity.y += gravity * Time.deltaTime;
         pch.Move (velocity * Time.deltaTime);
     }
 }
 

And this is a controller handler:

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent (typeof (CapsuleCollider))]
 public class PlayerControllerHandler : MonoBehaviour {
 
     public LayerMask collisionMask;
     private CapsuleCollider cc;
     public int collisions;
     
     void Start () {
         cc = GetComponent <CapsuleCollider> ();
     }
     
     public void Move (Vector3 velocity) {
         Collide (ref velocity);
         transform.Translate (velocity);
     }
     
     void Collide (ref Vector3 velocity) {
         RaycastHit hit;
         Vector3 point1 = transform.position + Vector3.up * cc.height / 4;
         Vector3 point2 = transform.position + Vector3.up * cc.height * 0.75f;
         float rayDistance = Vector3.Distance (transform.position, transform.position + velocity);
         if (Physics.CapsuleCast (point1, point2, cc.radius, velocity, out hit, rayDistance))
         {
             Debug.Log (hit.point.y);
             if (Mathf.Abs (transform.position.x - hit.point.x) < Mathf.Abs (velocity.x))
             {
                 collisions ++;
                 velocity.x = transform.position.x - hit.point.x;
             }
             if (Mathf.Abs (transform.position.y - hit.point.y) < Mathf.Abs (velocity.y))
             {
                 collisions ++;
                 velocity.y = transform.position.y - hit.point.y;
             }
             if (Mathf.Abs (transform.position.z - hit.point.z) < Mathf.Abs (velocity.z))
             {
                 collisions ++;
                 velocity.z = transform.position.z - hit.point.z;
             }        
         }
     }
 }

Set the capsule collider to radius 1, height 4. y axis. And youll see he jittters higher and higher. Until hes floating.

Comment

People who like this

0 Show 10
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 awplays49 · Sep 19, 2015 at 02:10 PM 0
Share

It just doesn't make sense

avatar image awplays49 · Sep 19, 2015 at 03:04 PM 0
Share

I've been staring at a screen for hours now. I think I'll leave it to the community of Unity Answers to help me take this one.

avatar image Glurth · Sep 19, 2015 at 03:43 PM 0
Share

Shot in the dark: It doesn't look like you are taking cc.center, nor cc.direction into account when computing point1 and point2. If either of these are not what you are assuming, that could be a problem.

avatar image awplays49 · Sep 19, 2015 at 03:56 PM 0
Share

@Glurth well, now I have this and it doesn't register until I'm below the ground.

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent (typeof (CapsuleCollider))]
 public class PlayerControllerHandler : MonoBehaviour {
 
     public LayerMask collisionMask;
     private CapsuleCollider cc;
     public int collisions;
     
     void Start () {
         cc = GetComponent <CapsuleCollider> ();
     }
     
     public void Move (Vector3 velocity) {
         Collide (ref velocity);
         transform.Translate (velocity);
     }
     
     void Collide (ref Vector3 velocity) {
         RaycastHit hit;
         Vector3 point1 = transform.position + cc.center + Vector3.down * cc.height / 4;
         Vector3 point2 = transform.position + cc.center + Vector3.up * cc.height / 4;
         Debug.Log (point1 + " " + point2);
         float rayDistance = Vector3.Distance (transform.position, transform.position + velocity);
         if (Physics.CapsuleCast (point1, point2, cc.radius, velocity, out hit, rayDistance))
         {
             if (Mathf.Abs (transform.position.x - hit.point.x) < Mathf.Abs (velocity.x))
             {
                 collisions ++;
                 velocity.x = transform.position.x - hit.point.x;
             }
             if (Mathf.Abs (transform.position.y - hit.point.y) < Mathf.Abs (velocity.y))
             {
                 collisions ++;
                 velocity.y = transform.position.y - hit.point.y;
             }
             if (Mathf.Abs (transform.position.z - hit.point.z) < Mathf.Abs (velocity.z))
             {
                 collisions ++;
                 velocity.z = transform.position.z - hit.point.z;
             }        
         }
     }
 }
 
avatar image Glurth awplays49 · Sep 19, 2015 at 04:09 PM 0
Share

Ah! You are also not taking transform.scale, nor rotation into account, for the points. (http://docs.unity3d.com/ScriptReference/Transform.TransformPoint.html)

 Vector3 point1 = transform.TransformPoint( cc.center + Vector3.down * (cc.height -cc.radius) );

Side note: I see you take transform.position into account when computing the ray distance, though it is not actually needed there:

 float rayDistance = Vector3.Distance (transform.position, transform.position + velocity);

is equivalent to

  float rayDistance = Vector3.Distance (Vector3.zero, Vector3.zero + velocity);

is equivalent to

 float rayDistance = velocity.magnitude;
avatar image awplays49 · Sep 19, 2015 at 04:12 PM 0
Share

I'm not sure what you meant, but What I want to do is predict where my character will be next frame, and if its gonna be in a wall, move right against the wall.

Show more comments

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

29 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

Related Questions

Jittery camera motion in GearVR when moving camera - nothing seems to work. 2 Answers

Movement jitter every few seconds 1 Answer

Raycasting problem 2 Answers

jump and land quicker 2 Answers

Horizontal gravity physics on 3d game 1 Answer


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