public void Move(float x, float y) { if (Mathf.Approximately(x, 0f) && Mathf.Approximately(y, 0f)) { return; } float degree = Mathf.Atan2(y, x) * Mathf.Rad2Deg; Vector2 direction = new Vector2(1f, 0f).Rotate(degree); Vector3 translation = new Vector3(direction.x, direction.y, 0f) * Speed * Time.deltaTime; if (translation.x > 0f) { var collider = Physics2D.OverlapCircle(rightSensor.position, sensorRadius, 1); if (collider != null) { translation.x = 0f; } } if (translation.x < 0f) { var collider = Physics2D.OverlapCircle(leftSensor.position, sensorRadius, 1); if (collider != null) { translation.x = 0f; } } if (translation.y > 0f) { var collider = Physics2D.OverlapCircle(topSensor.position, sensorRadius, 1); if (collider != null) { translation.y = 0f; } } if (translation.y < 0f) { var collider = Physics2D.OverlapCircle(downSensor.position, sensorRadius, 1); if (collider != null) { translation.y = 0f; } } transform.localPosition = transform.localPosition + translation; body.localEulerAngles = new Vector3(0f, 0f, degree); sprite.SetDirection(degree); }
// Update is called once per frame void Update() { transform.rotation = Quaternion.identity; if (!m_oSpriteComponent) { return; } var angle = Mathf.Atan2(transform.parent.transform.up.x, transform.parent.transform.up.y) * Mathf.Rad2Deg; m_oSpriteComponent.SetDirection(angle); }