/// <summary> /// Shoots multiple rays out to check collisions along one line with a direction. /// </summary> /// <param name="direction"></param> /// <param name="a"></param> /// <param name="b"></param> /// <param name="amount"></param> /// <param name="spacing"></param> /// <returns>Data of what happen with collision</returns> private CollisionProperties CheckCollision(Vector2 direction, Vector2 a, Vector2 b, int amount, float spacing, float rayLength, bool debug = false) { Vector2 spacingDir = (b - a).normalized; CollisionProperties properties = new CollisionProperties(); properties.collided = false; for (int i = 0; i < amount; i++) { RaycastHit2D hit = Physics2D.Raycast(a + (spacingDir * spacing * i), direction, rayLength, collisionMask); if (hit.collider != null && (!(hit.collider.gameObject.tag == "Oneway Platform" && direction != Vector2.down) && !(hit.collider.gameObject.tag == "Oneway Platform" && PassthroughOneways))) { if (debug) { Debug.DrawRay(a + (spacingDir * spacing * i), direction * rayLength, Color.green); } properties.collided = true; properties.hit = hit; return(properties); } else { if (debug) { Debug.DrawRay(a + (spacingDir * spacing * i), direction * rayLength, Color.red); } } } return(properties); }
public Triangle(Face face, List <Vector3> vertexes, List <Vector3> normals) { ColProperties = new CollisionProperties(); SndProperties = new SoundProperties(); VertexIndices = new List <int>(); VertexIndices = face.Indices; Vector3[] vertexData = new Vector3[3]; vertexData[0] = vertexes[VertexIndices[0]]; vertexData[1] = vertexes[VertexIndices[1]]; vertexData[2] = vertexes[VertexIndices[2]]; GetNormalTangentData(vertexData, normals); Unknown1 = 0x8000; Unknown2 = 0; }
public Triangle(EndianBinaryReader reader) { ColProperties = new CollisionProperties(); SndProperties = new SoundProperties(); VertexIndices = new List <int>(); VertexIndices.AddRange(new int[] { (int)reader.ReadInt16(), (int)reader.ReadInt16(), (int)reader.ReadInt16() }); NormalIndex = (int)reader.ReadInt16(); Edge1TangentIndex = (int)reader.ReadInt16(); Edge2TangentIndex = (int)reader.ReadInt16(); Edge3TangentIndex = (int)reader.ReadInt16(); PlanePointIndex = (int)reader.ReadInt16(); PlaneDValue = reader.ReadSingle(); Unknown1 = (int)reader.ReadInt16(); Unknown2 = (int)reader.ReadInt16(); }
/// <summary> /// Used to update collision booleans /// </summary> public void UpdateCollisions() { LastPosition = this.transform.position; UpdateRaycastPositions(); //Top collision detection and rebounds collisionTop = CheckCollision(Vector2.up, raycastPositions.topLeft, raycastPositions.topRight, WidthRaycastsCount, WidthRaycastSpacing, SkinThickness); collisionBottom = CheckCollision(Vector2.down, raycastPositions.bottomLeft, raycastPositions.bottomRight, WidthRaycastsCount, WidthRaycastSpacing, SkinThickness); collisionLeft = CheckCollision(Vector2.left, raycastPositions.bottomLeft, raycastPositions.topLeft, HeightRaycastsCount, HeightRaycastSpacing, SkinThickness); collisionRight = CheckCollision(Vector2.right, raycastPositions.bottomRight, raycastPositions.topRight, HeightRaycastsCount, HeightRaycastSpacing, SkinThickness); isGrounded = CheckCollision(Vector2.down, raycastPositions.bottomLeft, raycastPositions.bottomRight, WidthRaycastsCount, WidthRaycastSpacing, SkinThickness + 0.075f, false).collided; isCollidingTop = collisionTop.collided; isCollidingBottom = collisionBottom.collided; isCollidingLeft = collisionLeft.collided; isCollidingRight = collisionRight.collided; }
public void InitBullet(Vector3 bulletDirection, float kickBack) { collisionProperties = new CollisionProperties(bulletDirection, kickBack); }
public Triangle() { ColProperties = new CollisionProperties(); SndProperties = new SoundProperties(); VertexIndices = new List <int>(); }
//[JsonConstructor] public Triangle(List <int> VertexIndices, int NormalIndex, int TangentIndex, int BinormalIndex, int Unknown1Index, int PlanePointIndex, float PlaneDValue, int Unknown1, int Unknown2, CollisionProperties ColProperties, SoundProperties SndProperties) { this.VertexIndices = VertexIndices; this.NormalIndex = NormalIndex; this.Edge1TangentIndex = TangentIndex; this.Edge2TangentIndex = BinormalIndex; this.Edge3TangentIndex = Unknown1Index; this.PlanePointIndex = PlanePointIndex; this.PlaneDValue = PlaneDValue; this.Unknown1 = Unknown1; this.Unknown2 = Unknown2; this.ColProperties = ColProperties; this.SndProperties = SndProperties; }
void FixedUpdate() { // Active ( thrown ) ball update if (m_BallThrown) { float play_speed = Time.fixedDeltaTime; // Adjust velocity each frame based upon acceleration rate and gravity m_velocity += ((m_init_velocity * m_acceleration_rate) * play_speed); m_velocity += (m_gravity * play_speed); RaycastHit2D[] hit; Vector2 last_pos = transform.position; // acceleration falloff m_acceleration_rate *= (0.5f * play_speed); // Establish the layers we will collide with LayerMask layerMask = 1 << m_ObstaclesLayerMask; //bit shift to correctly apply mask // Get the distance covered this frame float distance = (last_pos - (last_pos + (m_velocity * play_speed))).magnitude; // Establish which type of collider we have and check for collisions over this frames distance CircleCollider2D circle = GetComponent <CircleCollider2D>(); BoxCollider2D box = GetComponent <BoxCollider2D>(); if (circle != null) { hit = Physics2D.CircleCastAll(last_pos, circle.radius, m_velocity * play_speed, distance, layerMask.value); } else if (box != null) { hit = Physics2D.BoxCastAll(last_pos, box.size, 0f, m_velocity, distance, layerMask.value); } else { hit = Physics2D.LinecastAll(last_pos, (last_pos + m_velocity * play_speed), layerMask.value); } // Hit counter int hits = 0; // Temp velocity to store all our collisions into Vector3 temp_velocity = new Vector3(0f, 0f, 0f); for (int i = 0; i < hit.Length; i++) { if (hit[i] && hit[i].collider != null) { if (!m_hit && hit[i] && hit[i].collider != this.GetComponent <Collider2D>()) { // Default damping factor for any collision float b_damping = m_damping; CollisionProperties col_prop = hit[i].collider.transform.gameObject.GetComponent <CollisionProperties>(); Rigidbody2D rigidbody = hit[i].collider.transform.gameObject.GetComponent <Rigidbody2D>(); if (col_prop) { // If the collided object has a CollisionProperties component we can grab the custom damping factor from this b_damping = col_prop.damping; } if (rigidbody) { // we've hit a rigid body, apply a force to his rigidbody based upon our velocity rigidbody.AddForceAtPosition(m_velocity * 10f, last_pos); } // Add each reflection vector into the temp_velocity temp_velocity += Vector3.Reflect(m_velocity * b_damping, hit[i].normal); hits++; } } } if (hits > 0) { // We've hit something last time, so we stop colliding until we are clear of collisions m_hit = true; } else { // We are clear of collisions, reset the hit flag m_hit = false; } // Extra check based upon the colliders array to clear the hit flag ( redundant? ) if (hit.Length == 0) { m_hit = false; } if (m_hit) { // If we hit we apply an averaged new ( reflected ) velocity based upon the number of collisions we had. m_velocity = (temp_velocity / hits); } // Apply to transform Vector3 velocity3 = new Vector3(m_velocity.x, m_velocity.y, 0); transform.position += (velocity3 * play_speed); } // Trajectory generation if (m_Pressed) { // Setup initial parameters based upon the mouse position. Vector2 last_pos = gameObject.transform.position; Vector2 velocity = keyVelocity;//transform.position - Camera.main.ScreenToWorldPoint(Input.mousePosition); Vector2 orignal_velocity = velocity; velocity *= m_power; acceleration_rate = velocity.magnitude * m_accel_rate; float travelled = 0f; // Default distance to check between trajectory points float dist_to_check = 1f; bool we_hit = false; bool hit_last_time = false; bool dead = false; int i = 0; // Loop through all the available trajectory points while (i < noOfTrajectoryPoints) { float play_speed = Time.fixedDeltaTime; // Apply the same velocity calculation which we apply per frame at runtime velocity += ((orignal_velocity * acceleration_rate) * play_speed); velocity += (m_gravity * play_speed); // acceleration falloff acceleration_rate *= (0.5f * play_speed); // Establish obstacle layermash LayerMask layerMask = 1 << m_ObstaclesLayerMask; // Distance travelled this loop float distance = (last_pos - (last_pos + (velocity * play_speed))).magnitude; if (i == 0) { // On the first loop set the distance_to_check to the distance we have travelled . // This gives the player a sense of force throughout the entire trajectory. dist_to_check = distance; // Makes the line look nicer dist_to_check *= 5; } // Increase our travelled variable by the distance this loop travelled += distance; RaycastHit2D[] hit; // Establish which type of collider we have and check for collisions over this frames distance CircleCollider2D circle = GetComponent <CircleCollider2D>(); BoxCollider2D box = GetComponent <BoxCollider2D>(); if (circle != null) { hit = Physics2D.CircleCastAll(last_pos, circle.radius, velocity * play_speed, distance, layerMask.value); } else if (box != null) { hit = Physics2D.BoxCastAll(last_pos, box.size, 0f, velocity, distance, layerMask.value); } else { hit = Physics2D.LinecastAll(last_pos, (last_pos + velocity * play_speed), layerMask.value); } we_hit = false; int hits = 0; Vector2 temp_velocity = new Vector2(0f, 0f); for (int j = 0; j < hit.Length; j++) { if (!hit_last_time && hit[j] && hit[j].collider != this.GetComponent <Collider2D>()) { // Default damping factor for any collision float b_damping = m_damping; CollisionProperties col_prop = hit[j].collider.transform.gameObject.GetComponent <CollisionProperties>(); if (col_prop) { // If the collided object has a CollisionProperties component we can grab the custom damping factor from this b_damping = col_prop.damping; } // Add each reflection vector into the temp_velocity temp_velocity += Vector2.Reflect(velocity * b_damping, hit[j].normal); we_hit = true; hits++; } } if (we_hit) { // We've hit something last time, so we stop colliding until we are clear of collisions // If we hit we apply an averaged new ( reflected ) velocity based upon the number of collisions we had. velocity = (temp_velocity / hits); hit_last_time = true; } else { // We've hit something last time, so we stop colliding until we are clear of collisions hit_last_time = false; } if (velocity.magnitude == 0f) { // stop the line since the ball has stopped dead = true; } // Apply the new velocity to our position last_pos += velocity * play_speed; // Every time we have travelled further than our distance to check we add a new trajectory point if ((travelled > dist_to_check - (dist_to_check * 0.25f)) && i < noOfTrajectoryPoints) { // Set the position trajectoryPoints[i].transform.position = last_pos; // Enable the render unless we are using a line renderer or dead if (!enableSolidLine && !dead) { trajectoryPoints[i].GetComponent <Renderer>().enabled = true; } else { trajectoryPoints[i].GetComponent <Renderer>().enabled = false; } // Reset travelled amount travelled = 0f; i++; } } // Now we have all the trajectory points we can calculate the angles to apply between them i = 0; while (i < noOfTrajectoryPoints) { int prev_index = i - 1; int next_index = i + 1; // Check for start and end points if (i == noOfTrajectoryPoints - 1) { next_index = i; } if (i == 0) { prev_index = i; } Vector2 next_pos = trajectoryPoints[next_index].transform.position; Vector2 old_pos = trajectoryPoints[prev_index].transform.position; if (i == 0) { // If the start point use the objects position for the old position old_pos = gameObject.transform.position; } Vector2 newVel = next_pos - old_pos; // Calculate and apply the angle to the sprite transform float angle = Mathf.Atan2(newVel.y, newVel.x) * Mathf.Rad2Deg; trajectoryPoints[i].transform.eulerAngles = new Vector3(0, 0, angle); if (!enableSolidLine) { trajectoryPoints[i].GetComponent <Renderer>().enabled = true; } if (enableSolidLine) { // line renderer positions Vector3 pos = new Vector3(trajectoryPoints[i].transform.position.x, trajectoryPoints[i].transform.position.y, 10f); m_line.SetPosition(i, pos); m_line.enabled = true; } i++; } } }