Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 Looseshoulders · Mar 09, 2019 at 11:47 PM · collisionmovementrigidbodyfrictionslide

Buggy Collision

I'm trying to make a game where the player can move in 4 directions, up, down, left, and right. My game is basically a slider puzzle. I've managed to make it work, however when it comes to collision with other objects the game bugs a bit. I'll be using a box collider, using a cube as the player and obstacles, and if i move up and just happen to brush aside another none moving cube the player stops either on the beggning of its edge or halfway through it. I've changed the collider from a box to sphere and this friction doesn't happen as much, however it still exists. I've tried putting a material on it and setting it's friction to 0 and it still continues. I have no ideia how to fix this, what can I do now?

This is my code

Blockquote

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class PcTest : MonoBehaviour { // Start is called before the first frame update public float x, y, speed = 20f, pressed, pres1 = 1f, pres2 = 1f, pres3 = 1f, pres4 = 1f, distance = 1f; public bool up, left, down, right, moving, limitup, limitdown, limitleft, limitright, collided, clicked, stopped, cantup, cantdown, cantright, cantleft, tester; public Rigidbody rb; public Vector2 vel; public float timer = 1f; public float lastx, lasty; public bool elevator; public int counter; public GameObject door2; public bool coli; public Vector3 stopPos; // Use this for initialization void Start() { stopped = true; rb = GetComponent(); lastx = transform.position.x; lasty = transform.position.y; }

 // Update is called once per frame
 void Update()
 {
 
     if (coli)
     {
         counter += 1;
         coli = false;
     }
     if (counter >= 2) { Destroy(door2); }
     x = Mathf.Round(rb.velocity.x * 100) / 100;
     y = Mathf.Round(rb.velocity.y * 100) / 100;

    
     if (!moving)
     {
         if (Input.GetKey("up") && !cantup )
         {
             rb.isKinematic = false;

             down = false;
             up = true;
             left = false;
             right = false;
             stopped = false;
             moving = true;
         }
         else
         if (Input.GetKey("down") && !cantdown  )
         {
             rb.isKinematic = false;

             down = true;
             up = false;
             left = false;
             right = false;
             stopped = false;
             moving = true;
         }
         else
         if (Input.GetKey("right") && !cantright  )
         {
             rb.isKinematic = false;

             down = false;
             up = false;
             left = false;
             right = true;
             stopped = false;
             moving = true;

         }
         else
         if (Input.GetKey("left") && !cantleft  )
         {
             rb.isKinematic = false;

             down = false;
             up = false;
             left = true;
             right = false;
             stopped = false;
             moving = true;

         }
     }

     if (!stopped )
     {
         if (up && !cantup)
         {
             //  rb.velocity = new Vector3(0, 15, 0);

             rb.AddForce(transform.up * speed);
             rb.constraints = RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezeRotationZ
                 | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationX;
           

         }

         if (down && !cantdown)
         {
             //  rb.velocity = new Vector3(0, -15, 0);

             rb.AddForce(-transform.up * speed);
             rb.constraints = RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezeRotationZ
                                | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationX;

         }

         if (right && !cantright)
         {

             rb.AddForce(transform.right * speed);
             //  rb.velocity = new Vector3(15, 0, 0);

             rb.constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezeRotationZ
                                | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationX;


         }



         if (left && !cantleft)
         {

             rb.constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezeRotationZ
                                | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationX;

             rb.AddForce(-transform.right * speed);
             // rb.velocity = new Vector3(-15, 0, 0);

           

         }
     }


     if (x == 0f && y == 0f)
     {
         stopped = true;
         moving = false;
         down = false;
         up = false;
         left = false;
         right = false;
         clicked = false;

     }
     else { stopped = false; }




 }

 

 void OnCollisionEnter(Collision col)
 {

     if (col.gameObject.tag == "Player")
     {
         rb.isKinematic = true;
         //rb.velocity = Vector3.zero;
         stopped = true;
         moving = false;
         down = false;
         up = false;
         left = false;
         right = false;
         clicked = false;
     }

     if (col.gameObject.tag == "key")
     {


         right = false;
         left= false;
         up = false;
         down = false;



     }

 }

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Major · Mar 10, 2019 at 07:06 AM

This should address your issues.

Comment
Add comment · Show 3 · 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 Looseshoulders · Mar 10, 2019 at 08:23 PM 0
Share

I expressed my self incorrently, sorry about that. By slider i meant like an ice puzzle so no overlapping. $$anonymous$$y issue is the friction of the objects, whenever i try to go up he sort of collides with it like so

alt text

annoying-cubes.png (99.8 kB)
avatar image Major Looseshoulders · Mar 11, 2019 at 12:51 AM 1
Share

From your code it would seem that you zero the player's velocity when a collision occurs. Even just brushing up against another collider can still cause a collision to exist, ergo your player will stop moving when it touches another collider. What I proposed would still work for you, you would just have to search a little farther in each direction, rather than just 1 tile.

If that doesn't work for you, then you might consider ray casting in the direction of movement to deter$$anonymous$$e if the movement direction is viable.

avatar image Looseshoulders Major · Mar 12, 2019 at 01:06 AM 0
Share

I deactivated the zero velocity code, all though It's much better than what it was it's still colliding with the edges from time to time. I tried using rb.velocity and add force with an INT and he stills collides on the edges from time to time, even with every object on the scene with 0 friction. As for the raycast I used it in the beggining but I don't see how it could avoid this friction from happening. Either way you helped a lot already, thanks!

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

213 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 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

Make characters go through each other 1 Answer

Problem with softbodies collisions 0 Answers

Can't use Rigidbody or Character Controller 0 Answers

Rigidbody Version of RotateAround 2 Answers

Projectile Ricochet Issue. Travels along the surface rather than reflecting off of it 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