Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 devFd · Apr 11, 2019 at 12:52 PM · collision3dcharactercontrollerboxcollider

My player collision is not accurate/precise enough

alt text


basically, whenever the player (pink cube) moves, it hits the collider but then only gets pushed back to this point (won't go further,i want it to like be closer to the collider if u know what i mean). It kind of improves if i change the speed but then it'd still be a problem for the other colliders. (and also not to mention it happens 100% of the time if i port it to android). Anyway to solve this? maybe my code for oncontrollercolliderhit is not accurate/good enough(i'm using charactercontroller). or maybe someone can suggest another way of moving my player which will work(i tried rigidbodies but idk what i did wrong it wouldn't work)


Here's basically the code responsible for everything

     public Swipe swipeControls;
     public Transform plr;
     private Vector3 desiredPosition;
     private Rigidbody rb;
     private CharacterController controller;
     private Vector3 startPos;
     private static int sceneCount;
     
     // Update is called once per frame
     private void Start()
     {
         desiredPosition = plr.transform.position;
         rb = plr.GetComponent<Rigidbody>();
         controller = plr.GetComponent<CharacterController>();
         startPos = plr.transform.position;
         sceneCount = SceneManager.GetActiveScene().buildIndex;
     }
     private void Update ()
     {
         if (swipeControls.SwipeLeft)
             desiredPosition += Vector3.left * 30;
             //plr.GetComponent<Rigidbody>().AddForce(Vector3.left);
             //plr.transform.Translate(-100f * Time.deltaTime, 0f, 0f);
         if (swipeControls.SwipeRight)
             desiredPosition += Vector3.right * 30;
              //plr.transform.Translate(100f * Time.deltaTime, 0f, 0f);
         if (swipeControls.SwipeUp)
             desiredPosition += Vector3.forward * 30;
         if (swipeControls.SwipeDown)
             desiredPosition += Vector3.back * 30;
         
 
         //plr.transform.position = Vector3.MoveTowards(plr.transform.position, desiredPosition,25f * Time.deltaTime);
         var dir = desiredPosition - plr.transform.position;
         var movement = dir.normalized * 21f * Time.deltaTime;
         var gravity = 9.8f;
         var gravityfps = 0f;
         gravityfps -= gravity * Time.deltaTime;
         movement.y = gravityfps;
         if (movement.magnitude > dir.magnitude)
         movement = dir;
 
         
         controller.Move(movement);
     }
 
 
 
     private void OnControllerColliderHit(ControllerColliderHit hit)
     {
         if(hit.gameObject.tag == "death")
         {
             Debug.Log("die");
             plr.transform.position = new Vector3(-2.66f, 2.017f, -10f);
             desiredPosition = plr.transform.position;
         }
         if(hit.gameObject.tag == "walls")
         {
             desiredPosition = plr.transform.position;
         }
         if(hit.gameObject.tag == "end")
         {
             sceneCount = SceneManager.GetActiveScene().buildIndex;
             sceneCount = sceneCount + 1;
             //PlayerPrefs.SetInt("sceneNo", sceneCount);
             SceneManager.LoadScene(sceneCount);
         }
         Debug.Log(sceneCount);
         Debug.Log(hit.collider.name);
     }
 }
 

help2.png (68.4 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Saiguru · Apr 12, 2019 at 10:20 AM

Hi @devFd , Try changing in the Rigidbody, "Collision Detection" from "Discrete" to "Continuous"

Comment
Add comment · Share
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
0

Answer by Bunny83 · Apr 12, 2019 at 10:42 AM

You use a CharacterController. A CharacterController is always a capsule. It's not a physics object and doesn't use physics for collision detection. You can not use any other collider. The CharacterController itself IS a capsule collider. So just select your player to see that capsule. If you don't want your player to be a capsule but to be a cube, you can not use the CharacterController.


If you use a Rigidbody and set rotation contraints for all axis, the cube should not rotate due to collisions. Of course you would need to heavily modify your current script in order to work with a rigidbody instead. Though you can simply assign your "movement" (without the deltaTime) to rb.velocity to get a similar behaviour.

Comment
Add comment · Show 2 · Share
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 devFd · Apr 14, 2019 at 01:41 AM 0
Share

I tried your suggestion and changed to rb.velocity but now, after the cube hits the invisible wall and goes forward, it flies off to the right (once it gets off the wall collider). it's like if gravity is on the right. I disabled my gravity for charactercontroller thing and it's still the same.

avatar image devFd devFd · Apr 14, 2019 at 12:05 PM 0
Share

I fixed the problem above but the current problems are:

  • gravity does not work (using rigidbody useGravity, object goes down but its very slow, i checked around and it has to do with scale but my scale of the cube is just 1,1,1)

  • After moving for a while the object slows down (i think it's something to do with friction) but it only happens once i keep sliding

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

184 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

Related Questions

3D Collision with Transform.Translate 0 Answers

CharacterController with BoxCollider 1 Answer

CharacterController and Normal rigidbody Box Collider Collision issues 0 Answers

horse with rigid body and boxcollider wont collide with anything i tried mesh collider with and without convex , i tried box collider with and without rigid body. 2 Answers

Jump on the ground 2 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