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;
}
}