// Update is called once per frame void FixedUpdate() { RaycastHit hit; //the rocket goes forward every frame transform.Translate(Vector3.forward * velocity * Time.deltaTime); //Destroy the rocket if it strikes other geometry if (Physics.Raycast(transform.position, transform.forward, out hit, velocity * Time.deltaTime)) { transform.position = hit.point; Vector3 reflected = Vector3.Reflect(transform.forward, hit.normal); RocketSFX(hit, reflected); if (hit.transform.gameObject.layer == 12) //12 is the destructible layer { hit.transform.GetComponent <DestructibleScript>().LoseHealth(damage); } if (hit.transform.gameObject.layer == 13) { hit.transform.GetComponent <EnemyHealthScript>().LoseHealth(damage); } Destroy(gameObject); } //Destroy the rocket if it reaches the end of its life if (Time.time > lifeTimer + life) { Destroy(gameObject); } }
private void SummonBullet(int BulletIndex, float CertainSpeed) { Vector3 RelativeSumPoint = new Vector3(SummonPoint.x * GetComponent <Collider2D>().bounds.size.x, SummonPoint.y * GetComponent <Collider2D>().bounds.size.y, 0); Vector3 position; Quaternion rotation; bool FacingRight = GetComponent <PlayerController>().FacingRight; if (FireDirection.y < 0 && FireDirection.x == 0) { position = FacingRight ? transform.position + RelativeSumPoint : transform.position + Vector3.Reflect(RelativeSumPoint, Vector3.right); } else { position = _aimindic.transform.position; } rotation = FacingRight ? Quaternion.Euler(0f, 0f, Vector2.SignedAngle(Vector2.right, FireDirection)) : Quaternion.Euler(0f, 180f, Vector2.SignedAngle(FireDirection, Vector2.left)); var bulletInstance = Instantiate(Bullets[BulletIndex], position, rotation); bulletInstance.GetComponent <Rigidbody2D>().velocity = CertainSpeed * FireDirection; bulletInstance.gameObject.GetComponent <BulletInfo>().playerOrigin = gameObject; }
private static void LoadMapGroup(MapGroup group, GameObject parent = null) { if (group.groupName.Contains("fnc0000")) { return; } GameObject groupRootObject = new GameObject(group.groupName); if (parent == null) { groupRootObject.GetComponent <Transform>().position = Vector3.Reflect(group.groupTransform.translation, Vector3.left); groupRootObject.GetComponent <Transform>().rotation = Quaternion.Euler(Vector3.Reflect(group.groupTransform.rotation.ToVector3(), Vector3.left)); groupRootObject.GetComponent <Transform>().localScale = Vector3.Reflect(group.groupTransform.scale, Vector3.left); } else { groupRootObject.GetComponent <Transform>().SetParent(parent.GetComponent <Transform>()); groupRootObject.GetComponent <Transform>().localPosition = group.groupTransform.translation; groupRootObject.GetComponent <Transform>().localRotation = group.groupTransform.rotation; groupRootObject.GetComponent <Transform>().localScale = group.groupTransform.scale; } groupRootObject.SetActive(true); if (group.entries != null && group.entries.Count > 0) { foreach (MapModelEntry entry in group.entries) { if (DebugCustomLoad) { if (!debugCustomLoadList.Contains(entry.modelId)) { continue; } } Mesh[] meshes = _modelMeshes[entry.modelId]; GameObject obj = AddMeshToNewGameObject(meshes, true); obj.GetComponent <Transform>().SetParent(groupRootObject.GetComponent <Transform>()); obj.GetComponent <Transform>().localPosition = entry.transform.translation; obj.GetComponent <Transform>().localRotation = entry.transform.rotation; obj.GetComponent <Transform>().localScale = entry.transform.scale; obj.SetActive(true); } } if (group.groups != null && group.groups.Count > 0) { foreach (MapGroup subGroup in group.groups) { if (subGroup != null) { LoadMapGroup(subGroup, groupRootObject); } } } }
/// <summary> /// This function calculates the contact points of the sphere before it moves in order to improve performance /// </summary> private void ComputeCoords() { if (_coordinates.Count == 0) { return; } var direction = _direction; var origin = _coordinates[0]; for (var i = 1; i <= 500; i++) { RaycastHit _hit; if (!Physics.Raycast(origin, direction, out _hit, 100f)) { continue; } direction = Vector3.Reflect(direction.normalized, _hit.normal); origin = _hit.point + 0.01f * direction; _coordinates.Add(origin); } }
private void WaveData() { for (var j = 0; j < _wavesList.Count; j++) { var direction = _wavesList[j].Direction; var origin = _wavesList[j].Origin; var sphereScript = _spheresList[j].GetComponent <Sphere>(); for (var i = 1; i <= totalBounce; i++) { if (!Physics.Raycast(origin, direction, out _hit, _wavesList[j].MaxDistance, layers)) { continue; } direction = Vector3.Reflect(direction.normalized, _hit.normal); origin = _hit.point + lineOffset * direction; sphereScript.WaveCoordData.Add(origin); } sphereScript.StartMovement(); } }
private void DrawRaycast() { for (int i = 0; i < _spheresList.Count; i++) { var sphereScript = _spheresList[i].GetComponent <Sphere>(); if (sphereScript.NbBounce > 0) { var direction = (sphereScript.WaveCoordData[1] - sphereScript.WaveCoordData[0]).normalized; var origin = sphereScript.WaveCoordData[0]; for (var j = 1; j <= sphereScript.NbBounce; j++) { if (!Physics.Raycast(origin, direction, out _hit, 100, layers)) { continue; } Debug.DrawLine(origin, _hit.point, sphereScript.RayColor); direction = Vector3.Reflect(direction.normalized, _hit.normal); origin = _hit.point + lineOffset * direction; } } } }
public void OnTriggerEnter(Collider other) { if (other.gameObject.name == "Hitbox2D") { return; } if (isTriggered == false) { isTriggered = true; hitPos = other.ClosestPointOnBounds(transform.position); normalVector = hitPos - transform.position; normalVector.y = 0f; newestVector = Vector3.Normalize(Vector3.Reflect(other.GetComponent <Rigidbody>().velocity, normalVector)); if (newestVector == null) { return; } newestVector.y = 0f; ///todo network this movement other.transform.GetComponentInParent <Rigidbody>().AddForce(newestVector * power, ForceMode.Impulse); rend.material.SetColor("_BaseColor", Random.ColorHSV()); } }
// ************************************************* // Main logic // ************************************************* void Update() { if (ReachedGameOverState()) { _gameOverGroup.SetActive(true); return; } if (ReachedWinState()) { _winScreenGroup.SetActive(true); return; } if (!_mouse_down && Input.GetMouseButtonDown(0)) { _mouse_down = true; } if (_mergeAnimationsRunning && LerpAnimationsCompleted()) { _mergeAnimationsRunning = false; if (_mergeTarget.ReachedMaxValue()) { RemoveBallFromGrid(_mergeTarget.GetGridXCoord(), _mergeTarget.GetGridYCoord()); TriggerExplosion(_mergeTarget.GetGridXCoord(), _mergeTarget.GetGridYCoord()); // Trigger animation Destroy(_mergeTarget.gameObject); NextTurn(); } else { MergeBalls(_mergeTarget.GetGridXCoord(), _mergeTarget.GetGridYCoord()); _mergeAnimationsRunning = !LerpAnimationsCompleted(); } if (!_mergeAnimationsRunning) { RemoveFreeFloatingBall(); _mergeTarget = null; } } if (!CanShoot()) { return; } if (_mouse_down) { Vector3 mouseCoordsWorldSpace = Camera.main.ScreenToWorldPoint(Input.mousePosition); mouseCoordsWorldSpace.z = 0; Vector3 shootDirection = (mouseCoordsWorldSpace - _ballShooter.GetPosition()).normalized; Ray ray = new Ray(_ballShooter.GetPosition(), shootDirection); List <Vector3> linesToDraw = new List <Vector3>(); linesToDraw.Add(_ballShooter.GetPosition()); GameObject hitBall; RaycastHit hitInfo; List <Vector3> animationPath = new List <Vector3>(); if (!IntersectsBalls(ray, out hitBall, out hitInfo)) { float closestDistance = Single.PositiveInfinity; Plane closestPlane = _boundsPlanes[0]; Vector3 closestHitPoint = new Vector3(); bool didHitPlane = false; // => max 1 reflection // track nearest intersection point and store "next ray" here, then after the loop we do 1 more intersection test vs balls for (int i = 0; i < _boundsPlanes.Length; ++i) { Plane plane = _boundsPlanes[i]; if (plane.Raycast(ray, out float enter)) { didHitPlane = true; Vector3 hitPoint = ray.GetPoint(enter); float distance = Vector3.Distance(_ballShooter.GetPosition(), hitPoint); if (distance < closestDistance) { closestDistance = distance; closestPlane = plane; closestHitPoint = hitPoint; } } } if (didHitPlane) { Vector3 reflect = Vector3.Reflect(ray.direction, closestPlane.normal); // add plane intersection point to path animationPath.Add(closestHitPoint); linesToDraw.Add(closestHitPoint); Ray nextRay = new Ray(closestHitPoint, reflect); IntersectsBalls(nextRay, out hitBall, out hitInfo); _ballShooter.HidePreviewBall(); } } if (hitBall) { linesToDraw.Add(hitInfo.point); _lineRenderer.positionCount = linesToDraw.Count; for (int i = 0; i < linesToDraw.Count; ++i) { _lineRenderer.SetPosition(i, linesToDraw[i]); } var hitBallComp = hitBall.GetComponent <Ball>(); var gridPosition = _ballSpawner.GeneratePosition(hitBallComp.GetGridXCoord(), hitBallComp.GetGridYCoord()); var centerToHitDir = (hitInfo.point - gridPosition).normalized; if (PlaceOnGrid(hitBallComp.GetGridXCoord(), hitBallComp.GetGridYCoord(), centerToHitDir, out var gridX, out var gridY)) { Vector3 nextPosition = _ballSpawner.GeneratePosition(gridX, gridY); _ballShooter.ShowPreviewBall(nextPosition); // add next ball position to path animationPath.Add(nextPosition); if (Input.GetMouseButtonUp(0)) { _canShoot = false; _ballShooter.HidePreviewBall(); StartCoroutine(ShootBallAnimation(_ballShooter.GetCurrentBall().GetComponent <Ball>(), gridX, gridY, animationPath)); _lineRenderer.positionCount = 0; } } else { _ballShooter.HidePreviewBall(); } } else { _lineRenderer.positionCount = 0; } }
public void OnHit(RaycastHit hit, Laser laser) { laser.CastChild(hit.point, Vector3.Reflect(laser.transform.forward, hit.normal)); }