Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 joaopedroferreira0318 · Oct 07, 2020 at 08:55 PM · physicsforces

player enters in the gameObjects and get pushed

I'm having some problems with my game:

  • 1st- The player slightly enters inside others objects: https://youtu.be/yLlbnRivpME

  • 2nd- When the player pushes a box towards a wall and stop pushing, he receives an opposite force that only stops when the player hits another object. (in the video you can see me trying to go forwards but always getting pushed away.) https://youtu.be/jOuKTFYy3Gs

  • 3rd- When the players is pulling a box and touches a wall, the box receives an opposite force. https://youtu.be/p6BtOvL-eTI

Player controller script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [RequireComponent(typeof(Rigidbody))]
 
 public class PlayerController : MonoBehaviour
 {
     Rigidbody rb;
     public GameObject player;
     private GameObject box;
 
     public float speed = 5f;
     public float impulseY = 5f;
     public float x, y, z;
 
     public bool isPushing = false;
   
 
     void Start()
     {
         rb = GetComponent<Rigidbody>();
     }
 
     void FixedUpdate()
     {     
         if (Input.GetKey(KeyCode.D))
         {
             if (isPushing == false)
             {
                 player.transform.rotation = Quaternion.Euler(0, 0, 0);
                 transform.Translate(Vector3.right * speed * Time.deltaTime);
                 x = 1;
             }
             else
             {
                 if (player.transform.rotation == Quaternion.Euler(0, 0, 0))
                 {
                     transform.Translate(Vector3.right * speed * Time.deltaTime);
                 }
                 else
                 {
                     transform.Translate(Vector3.left * speed * Time.deltaTime);
                 }           
             }
         }
 
         if(Input.GetKey(KeyCode.A))
         {
             if(isPushing == false)
             {
                 player.transform.rotation = Quaternion.Euler(0, 180, 0);
                 transform.Translate(Vector3.right * speed * Time.deltaTime);
                 x = -1;
             }
             else
             {
                 if(player.transform.rotation == Quaternion.Euler(0, 180, 0))
                 {
                     transform.Translate(Vector3.right * speed * Time.deltaTime);
                 }
                 else
                 {
                     transform.Translate(Vector3.left * speed * Time.deltaTime);
                 }
             }
         }    
     }
 
     void Update()
     {
         //groundcheck
         Debug.DrawRay(player.transform.position + new Vector3(-0.2f, 0, 0), new Vector3(0, -1, 0), Color.red);
         Debug.DrawRay(player.transform.position, new Vector3(0, -1, 0), Color.red);
         Debug.DrawRay(player.transform.position + new Vector3(+0.2f, 0, 0), new Vector3(0, -1, 0), Color.red);
 
         //pushableCheck
         Debug.DrawRay(player.transform.position, new Vector3(x, y, z), Color.red);
         
 
         if (Input.GetKeyDown("space") && GroundCheck() && isPushing == false)
         {
             rb.AddForce(new Vector3(0, impulseY, 0), ForceMode.Impulse);
         }
 
         if (Input.GetKeyDown(KeyCode.LeftShift) && PushableObject() && GroundCheck())
         {
             box.GetComponent<Rigidbody>().isKinematic = false;
             box.gameObject.transform.parent = player.transform;
             isPushing = true;
            
         }
 
         if ((Input.GetKeyUp(KeyCode.LeftShift) && isPushing) || (isPushing && !PushableObject()))
         {
 
             if (!BoxGroundCheck(box))
             {
                 box.gameObject.transform.parent = null;
                 StartCoroutine("KeepNotKinematic");
             }
             else
             {
                 box.GetComponent<Rigidbody>().isKinematic = true;
                 box.gameObject.transform.parent = null;
             }
             isPushing = false;
         }
     }
 
     bool GroundCheck()
     {
         RaycastHit hit;
         float distance = 0.5f;
         Vector3 dir = new Vector3(0, -1);
         bool isGrounded;
        
         if(Physics.Raycast(player.transform.position, dir, out hit, distance) || Physics.Raycast(player.transform.position + new Vector3(-0.2f, 0, 0), dir, out hit, distance) || Physics.Raycast(player.transform.position + new Vector3(+0.2f, 0, 0), dir, out hit, distance))
         {
             isGrounded = true;
         }
         else
         {
             isGrounded = false;
         }
 
         return isGrounded;
     }
 
     bool BoxGroundCheck(GameObject bx)
     {
         RaycastHit hit;
         float distance = 0.5f;
         Vector3 dir = new Vector3(0, -1);
         bool isGrounded;
 
         if (Physics.Raycast(bx.transform.position, dir, out hit, distance) || Physics.Raycast(bx.transform.position + new Vector3(-0.2f, 0, 0), dir, out hit, distance) || Physics.Raycast(bx.transform.position + new Vector3(+0.2f, 0, 0), dir, out hit, distance))
         {
             isGrounded = true;
         }
         else
         {
             isGrounded = false;
         }
 
         return isGrounded;
     }
 
     bool PushableObject()
     {
         RaycastHit hit;
         float distance = 1f;
         bool isPushable;
 
         if (Physics.Raycast(player.transform.position, new Vector3(x, y, z), out hit, distance))
         {
             if (hit.collider.gameObject.tag == "Box")
             {
                 isPushable = true;
                 box = hit.collider.gameObject;
             }
             else
             {
                 isPushable = false;
             }
         }
         else
         {
             isPushable = false;
         }
 
         return isPushable;
     }
 
     IEnumerator KeepNotKinematic()
     {
         while(!BoxGroundCheck(box))
         {
             box.GetComponent<Rigidbody>().isKinematic = false;
             BoxGroundCheck(box);
             yield return null;
         }
     }
 }
     

Every gameObject has attached this: alt text

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
0

Answer by unity_ek98vnTRplGj8Q · Oct 07, 2020 at 09:34 PM

1st - To fix your player moving inside objects use Rigidbody.Move instead of transform.translate. When you use Transfrom.Translate, you are fighting against the physics system, because transform.translate will move you inside of an object, leaving it up to the physics system to try to push you out of the object. Using Rigidbody.Move will check for collisions as its moving you, so it never moves you inside of another object.


This may be enough to fix your other two issues. These are caused by the physics system pushing you out of the object that you are inside of, therefore applying force to you and sending you or the box sliding away from the wall. If this is still occurring you can always try bumping up the friction settings on your physics materials or even adding some linear drag to the rigidbodies.

Comment
Add comment · Show 1 · 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 joaopedroferreira0318 · Oct 11, 2020 at 12:30 PM 0
Share

I tried to use rigidbody.$$anonymous$$ovePosition, but it didn't work. Actually, it kept the same as translate, and the box needs to collide with the player to get pushed.

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

223 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

windszones 0 Answers

Projectile motion of an arrow after applying force 3 Answers

Trying to replicate Rigidbody.AddForceAtPosition 1 Answer

what is the reason for 'supposed' frictional force, when physics material of ground is configured frictionless. 0 Answers

Apply force to rigidbody based on child object acceleration causes jitter 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