void OnCollisionEnter2D(Collision2D other) { Debug.Log(other.gameObject.tag); if (other.gameObject.tag == "Player") { Vector3 dectectPoint = transform.position; float y3 = y + 1, x3 = x + 1; if ((int)dectectPoint.y == 0) { y3 = 0; x3 = start.x; } else if ((int)dectectPoint.y < 0) { y3 = y - 1; x3 = -(dectectPoint.x - start.x) * (dectectPoint.y - y) / (y3 - dectectPoint.y) + dectectPoint.x; } else if ((int)dectectPoint.y > 0) { y3 = y + 1; x3 = -(dectectPoint.x - start.x) * (dectectPoint.y - y) / (y3 - dectectPoint.y) + dectectPoint.x; } destination = new Vector3(x3, y3, destination.z); float rot = HelperUtils.AngleBetweenVector2(destination, dectectPoint); transform.Rotate(0, 0, rot); if (destination.x > 0) { transform.Rotate(0, 0, 180 + rot); } } }
//Update is called once per frame void Update() { updateSpeed(); timerCounter += Time.deltaTime * Speed; var oldPosition = transform.position; GameObject gameZone = GameObject.FindGameObjectWithTag("Zone"); if (gameZone == null) { return; } float x = Mathf.Cos(timerCounter) * (gameZone.GetComponent <Renderer>().bounds.size.x / 2f); float y = Mathf.Sin(timerCounter) * (gameZone.GetComponent <Renderer>().bounds.size.y / 2f); var newPosition = transform.position; transform.position = new Vector3(x, y, -1.0f); //Debug.Log(transform.position); float angle = HelperUtils.AngleBetweenVector2(gameZone.transform.position, transform.position); transform.rotation = Quaternion.Euler(0, 0, angle - 90); //transform.Rotate(0, 0, worldDegrees); //Draw circle }
void Start() { destination = new Vector3(0, 0, transform.position.z); start = transform.position; Camera cam = Camera.main; float height = 2f * cam.orthographicSize; float width = height * cam.aspect; x = width / 2; y = height / 2; float rotation = HelperUtils.AngleBetweenVector2(start, destination); transform.Rotate(0, 0, rotation); }