void FixedUpdate() { if (isFlying && !isChasing && movementSpeed == 0f) { GetComponent <Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezePositionX; } if (isChasing && !entering) { Transform target = mgr.GetTarget(); float targetX = target.position.x; bool goLeft = targetX < transform.position.x; float force = (goLeft ? -1 : 1) * movementSpeed * 10; if (isFlying) { force = startDirection * movementSpeed * 10f; } else { spriteRenderer.flipX = !goLeft; } if (rigidbody.velocity.magnitude < maxSpeed) { rigidbody.AddForce(new Vector2(force, 0.01f)); } } }
void Start() { rigidbody = GetComponent <Rigidbody2D>(); animator = GetComponent <Animator>(); spriteRenderer = GetComponent <SpriteRenderer>(); mgr = GameSceneManager.Instance; animator.SetBool("IsEntering", true); if (gameObject.name == "Slug") { rigidbody.isKinematic = true; } else { AnimationComplete(); } Transform target = mgr.GetTarget(); float targetX = target.position.x; startDirection = targetX < transform.position.x ? -1f : 1f; if (startDirection >= 0f) { spriteRenderer.flipX = true; } }