• 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 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
Add comment · 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 assu$$anonymous$$g, 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 : $$anonymous$$onoBehaviour {
 
     public Layer$$anonymous$$ask collision$$anonymous$$ask;
     private CapsuleCollider cc;
     public int collisions;
     
     void Start () {
         cc = GetComponent <CapsuleCollider> ();
     }
     
     public void $$anonymous$$ove (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 ($$anonymous$$athf.Abs (transform.position.x - hit.point.x) < $$anonymous$$athf.Abs (velocity.x))
             {
                 collisions ++;
                 velocity.x = transform.position.x - hit.point.x;
             }
             if ($$anonymous$$athf.Abs (transform.position.y - hit.point.y) < $$anonymous$$athf.Abs (velocity.y))
             {
                 collisions ++;
                 velocity.y = transform.position.y - hit.point.y;
             }
             if ($$anonymous$$athf.Abs (transform.position.z - hit.point.z) < $$anonymous$$athf.Abs (velocity.z))
             {
                 collisions ++;
                 velocity.z = transform.position.z - hit.point.z;
             }        
         }
     }
 }
 
Show more comments
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.

avatar image Glurth · Sep 19, 2015 at 04:26 PM 0
Share

I edited the heck out of my last comment, initial post was bad. Does it make more sense now?

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

@Gurth perhaps I'm misunderstanding capsule cast, but doesn't it build a capsule out of 2 spheres, and shoot that capsule in a direction?

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

And now its not registering at all, but heres my code.

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent (typeof (CapsuleCollider))]
 public class PlayerControllerHandler : $$anonymous$$onoBehaviour {
 
     public Layer$$anonymous$$ask collision$$anonymous$$ask;
     private CapsuleCollider cc;
     public int collisions;
     
     void Start () {
         cc = GetComponent <CapsuleCollider> ();
     }
     
     public void $$anonymous$$ove (Vector3 velocity) {
         Collide (ref velocity);
         transform.Translate (velocity);
     }
     
     void Collide (ref Vector3 velocity) {
         Vector3 point1 = transform.TransformPoint (cc.center + Vector3.down * cc.height / 4);
         Vector3 point2 = transform.TransformPoint (cc.center + Vector3.up * cc.height / 4);
         Debug.Log (point1 + " " + point2);
         float rayDistance = velocity.magnitude;
         RaycastHit hit;
         if (Physics.CapsuleCast (point1, point2, cc.radius, velocity, out hit, rayDistance))
         {
             Debug.Log ("Collision");
             if ($$anonymous$$athf.Abs (transform.position.x - hit.point.x) < $$anonymous$$athf.Abs (velocity.x))
             {
                 collisions ++;
                 velocity.x = transform.position.x - hit.point.x;
             }
             if ($$anonymous$$athf.Abs (transform.position.y - hit.point.y) < $$anonymous$$athf.Abs (velocity.y))
             {
                 collisions ++;
                 velocity.y = transform.position.y - hit.point.y;
             }
             if ($$anonymous$$athf.Abs (transform.position.z - hit.point.z) < $$anonymous$$athf.Abs (velocity.z))
             {
                 collisions ++;
                 velocity.z = transform.position.z - hit.point.z;
             }        
         }
     }
 }
 

avatar image awplays49 · Sep 19, 2015 at 08:09 PM 0
Share

@Gurth its been 3 hours are you still there?

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

30 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

Related Questions

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

Movement jitter every few seconds 1 Answer

Edit this script so that on the second jump change the gravity to 5 2 Answers

How can I make a game object move in parabolic motion as if it were under gravity? 2 Answers

Change direction of gravity for a specific instance of a prefab 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