We are trying to get the player caught by a net thats dropping from above.
We have tried numerous approaches. When the net hits it goes though the player and the player can ether goes up or down through the net. But he never goes in the net. We adjusted the animation many times. It Works sporadicly. We would like to have it work everytime.
1. The player has a box collider2D and the net has 3 box Colliders forming the shape of a upside down U (note: We saw on a digital tutors tutorial that multiple colliders shouldn’t cause a problem). The net is suppose to catch the player and the player dies. But, the player pushes past the nets colliders (Note: The net and player are both being moved by rigid bodies) and keeps moving up with the force of the players mouse click and then down and dies. To fix this we’ve tried stopping the players rigidbody using viva c# with :
using UnityEngine;
using System.Collections;
public class NetsManager : MonoBehaviour {
public Animator Netanimator; //Animator for the nets that will capture the player.
public Vector2 movePlayer;
public Collider2D NetCollider;
void Start()
{
NetCollider.enabled = true;
}
public void OnTriggerEnter2D(Collider2D other)
{
//Debug.Log ("Turtle has entered the coillider");
// if the player has collided with the net.
if (other.tag == "Player")
{
NetCollider.enabled = true;
other.GetComponent<Rigidbody2D> ().MovePosition(Vector2.down - movePlayer * Time.unscaledDeltaTime); //Stop the velocity of the object taged player.
//Debug.Log("Turtle is caught in net");
//other.GetComponent<Rigidbody2D> ().drag = 10; //increase the drag appiled to the rigidbody of the player.
Invoke("StopNetAnimator", 2f); //start the void stop net animator in 2 sceonds.
}
}
public void StopNetAnimator ()
{
NetCollider.enabled = true;
Netanimator.enabled = false; //the net's animation is now disabled. Since player is caught, the net's animator has no reason to countine to the end of the animation clip.
//Debug.Log("Net animator is disabled");
}
}
2. Also we have tried this script from the unity wiki for a object not being able to go through colliders:
using UnityEngine;
using System.Collections;
public class DontGoThroughWalls : MonoBehaviour {
// Careful when setting this to true - it might cause double
// events to be fired - but it won't pass through the trigger
public bool sendTriggerMessage = false;
public LayerMask layerMask; //make sure we aren't in this layer
public float skinWidth = 0.1f; //probably doesn't need to be changed
private float minimumExtent;
private float partialExtent;
private float sqrMinimumExtent;
private Vector3 previousPosition;
private Rigidbody2D myRigidbody;
public Collider2D MyCollider;
//initialize values
void Start ()
{
myRigidbody = GetComponent<Rigidbody2D> ();
previousPosition = myRigidbody.position;
minimumExtent = Mathf.Min(Mathf.Min(MyCollider.bounds.extents.x, MyCollider.bounds.extents.y), MyCollider.bounds.extents.z);
partialExtent = minimumExtent * (1.0f - skinWidth);
sqrMinimumExtent = minimumExtent * minimumExtent;
}
void FixedUpdate()
{
//have we moved more than our minimum extent?
Vector3 movementThisStep = (Vector3)myRigidbody.position - previousPosition;
float movementSqrMagnitude = movementThisStep.sqrMagnitude;
if (movementSqrMagnitude > sqrMinimumExtent)
{
float movementMagnitude = Mathf.Sqrt(movementSqrMagnitude);
RaycastHit hitInfo;
//check for obstructions we might have missed
if (Physics.Raycast(previousPosition, movementThisStep, out hitInfo, movementMagnitude, layerMask.value))
{
if (!hitInfo.collider)
return;
if (hitInfo.collider.isTrigger)
hitInfo.collider.SendMessage("OnTriggerEnter", MyCollider);
if (!hitInfo.collider.isTrigger)
myRigidbody.position = hitInfo.point - (movementThisStep / movementMagnitude) * partialExtent;
}
}
previousPosition = myRigidbody.position;
}
}
3. Another attempt that was unsuccessful was when we were trying to add force to the nets rigidbody, in theory pulling the player down and keeping him in the net.
using UnityEngine;
using System.Collections;
public class MoveNetCoillsions : MonoBehaviour {
public Rigidbody2D NetColliders; // rigidbody of the net.
public float NetCoillidersSpeed = -2f; // speed of the coillders on the net as they fall towards the player.
// Update is called once per frame
void Update ()
{
NetColliders.velocity = new Vector2(0, NetCoillidersSpeed * Time.deltaTime); // Add movement to the net's colliders.
NetColliders.AddForce(new Vector2(0, -4)* NetCoillidersSpeed * Time.deltaTime); // Addforce to the net's colliders to ensure that the player doesn't go though the colliders.
}
}
4. Plus, we tried to make the net like as if it was a magnet attraching the player when caught within a certain radius.
using UnityEngine;
using System.Collections;
public class NetMagnet : MonoBehaviour {
public LayerMask m_MagneticLayers;
public Vector3 m_Position;
public float m_Radius;
public float m_Force;
void FixedUpdate ()
{
Collider[] colliders;
Rigidbody rigidbody;
colliders = Physics.OverlapSphere (transform.position + m_Position, m_Radius, m_MagneticLayers);
foreach (Collider collider in colliders)
{
rigidbody = (Rigidbody) collider.gameObject.GetComponent (typeof (Rigidbody));
if (rigidbody == null)
{
continue;
}
rigidbody.AddExplosionForce (m_Force * -1, transform.position + m_Position, m_Radius);
}
}
void OnDrawGizmosSelected ()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere (transform.position + m_Position, m_Radius);
}
}
5. Then we tried to use the newer feature the point effector to keep the player in the net. That didn’t work.
6. We also tried using another type of collider, the edge collider. There was one point we we had multiple edge colliders on the net. That didn’t work ether.