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 /
  • Help Room /
avatar image
0
Question by oddPoly · Feb 08, 2016 at 03:23 PM · 2d gameobjectsplatformerpushing

2D Game - Movable Object Prevents Being Pushed If Too Close To Player

Dear Unity Community,

-Disclaimer: This is my first post so I hope I meet the social consensus with my post.-

My scenario:

I'm in the process of creating a 2D puzzle/stealth game. The entire game and its objects are 2D, some of which are movable. You can move objects around by holding down the "interact-"key (in my scene, its "e").

My problem:

Whenever the distance between the object and the player (=collider) is 0, the object does not move. My theory is that the player needs space in order to be able to move into a direction, so that subsequently a movement vector can be applied to the affected object. I assume thats why a vector cannot be calculated (due to a length of 0 between A (collider) and B (object)). In my code I parent the object to the player (whenever "e" is pressed) so the object copies the players movement.

My goal:

I'd like to be able to move even though the distance between the object and the collider is 0. In other words: I want to be able to stand directly next to the object in order to push it. How can I achieve that?

PlayerController Script:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour 
 {
 
     public float maxSpeed = 10f;
     bool facingRight = true;
 
     Animator anim;
 
 
     //setup for the fall, since we're not doing a jump anim, but a midair anim
     bool grounded = false;
     public Transform groundCheck; 
     float groundRadius = 0.2f;
     public LayerMask whatIsGround;
     public float jumpForce = 700f;
 
 
     void Start () 
     {
         anim = GetComponent<Animator> ();
     }
     
 
     void FixedUpdate () 
     {
         //constant check if we're on the ground. if grounded = .. is true, we are on ground, and vice versa
         grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
         anim.SetBool ("Ground", grounded);
 
         anim.SetFloat ("vSpeed", GetComponent<Rigidbody2D>().velocity.y);
 
         // realistic jump (you cant alter direction mid-air
         // if (!grounded) return; 
 
         //capture side movement
         float move = Input.GetAxis ("Horizontal");
         //fill the speed parameter in the animator with a positive (=abs) number
         anim.SetFloat ("Speed", Mathf.Abs (move));
         //change left right, but not up down (x,y vector, where x changes, y stays constant)
         GetComponent<Rigidbody2D>().velocity = new Vector2 (move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
         //flip around if movement is positive/negative)
 
         if (!Globals.pullingObject) 
         {
             if (move > 0 && !facingRight)
                 Flip ();
             else if (move < 0 && facingRight)
                 Flip ();
         }
     }
 
     void Update()
     {
         if(grounded && Input.GetKeyDown (KeyCode.Space))
         {
             anim.SetBool ("Ground", false);
             GetComponent<Rigidbody2D>().AddForce(new Vector2(0, jumpForce));
         }
 
         if (Globals.pullingObject == true)
             maxSpeed = 4f;
         else if (Globals.pullingObject == false)
             maxSpeed = 10f;
     }
 
     void Flip ()
     {
 
             facingRight = !facingRight; // change facing
             Vector3 theScale = transform.localScale; // temp save vector3 localscale 
             theScale.x *= -1; //flip x coordinate of temp save
             transform.localScale = theScale; // override localscale with temp save
         }
 }
 


Box Pull Script:

 using UnityEngine;
 using System.Collections;
 
 public class BoxPull : MonoBehaviour 
 {
 
     public GameObject player;
     public bool pulling = false;
     Rigidbody2D rigbody; 
 
 
     // Use this for initialization
     void Start () 
     {
         rigbody = GetComponent<Rigidbody2D> ();
     }
 
     // Update is called once per frame
     void Update () 
     {
     
         
 
         if (pulling && Input.GetKey(KeyCode.E)) 
         {
                 Debug.Log ("Pressed E");
                 transform.parent = player.transform;
                 //rigbody.isKinematic = false;
                 Globals.pullingObject = true;
         }
         else 
         {
             transform.parent = null;
             //rigbody.isKinematic = true;
             Globals.pullingObject = false;
         }
     }
 
     void OnTriggerEnter2D ()
     {
         Debug.Log ("EnterTrigger");
         pulling = true;
     }
     void OnTriggerExit2D ()
     {
         Debug.Log ("LeaveTrigger");
         pulling = false;
     }
 }
 





Object Physics Script:

 using UnityEngine;
 using System.Collections;
 
 public class ObjectPhysics : MonoBehaviour {
 
     public float pushPower = 2.0F;
 
     void OnControllerColliderHit(ControllerColliderHit hit) {
         Rigidbody body = hit.collider.attachedRigidbody;
         if (body == null || body.isKinematic)
             return;
         
         if (hit.moveDirection.y < -0.3F)
             return;
         
         Vector3 pushDir = new Vector3(hit.moveDirection.x, 0, hit.moveDirection.z);
         body.velocity = pushDir * pushPower;
     }
 }
 


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

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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Detecting missing collision in 2D platformer to change direction for enemy - without Raycast 1 Answer

Jump Script 2 Answers

game like air control 0 Answers

When I delete an object, the box collider remains. 1 Answer

for some reason the isGrounded is always checked off for me 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