private IEnumerator SmoothMovement(Vector2?end) { _isMoving = true; float remainingDistance = (Rb.position - end.Value).sqrMagnitude; Sprite.flipX = false; if (_movement.x < 0) { Sprite.flipX = true; } while (remainingDistance > float.Epsilon) { Vector2 newPosition = Vector2.MoveTowards(Rb.position, end.Value, _moveSpeed * Time.fixedDeltaTime); Rb.position = newPosition; remainingDistance = (Rb.position - end).Value.sqrMagnitude; yield return(null); } ToggleFog(Vector3Int.FloorToInt(_movement)); _isMoving = false; }
/************** * Behaviours * **************/ void chase() { // Save data had_LOS = true; last_player_pos = player.position; //move toward the player, but not too close! if (Vector2.Distance(transform.position, player.position) >= social_distance) { transform.position = Vector2.MoveTowards(transform.position, player.position, movespeed * Time.deltaTime * TimeManager.Instance.getTimeMultiplier()); } //rotate to look at the player transform.right = player.position - transform.position; //shoot when possible if (shoot_timer <= 0f) { shoot(); shoot_timer = Random.Range(shoot_timer_length_min, shoot_timer_length_max); } else { shoot_timer -= Time.deltaTime * TimeManager.Instance.getTimeMultiplier(); } }
void Update() { //Must be the local player, or they cannot move if (!isLocalPlayer) { return; } //Ignore movement controls when typing in chat if (chatRegister.ChatWindow != null && chatRegister.ChatWindow.PlayerIsTyping()) { currentMovement.Set(0, 0); return; } if (Input.GetButtonDown("Toggle Run")) { isWalking = !isWalking; } // TODO: Implement gravity and grabbing // Calculate next movement // The vector is not normalized to allow for the input having potential rise and fall times float x = Input.GetAxisRaw("Horizontal"); float y = Input.GetAxisRaw("Vertical"); // Smoothly transition to next intended movement intendedMovement = new Vector2(x, y).normalized *(isWalking ? walkSpeed : runSpeed); currentMovement = Vector2.MoveTowards(currentMovement, intendedMovement, Time.deltaTime * (Mathf.Pow(ACCELERATION / 5f, 3) / 5)); absoluteMovement = new Vector3(absoluteMovement.x, 0, absoluteMovement.z); // Move the player if (currentMovement != Vector2.zero) { // Determine the absolute movement by aligning input to the camera's looking direction Vector3 absoluteMovement = currentMovement.y * Vector3.Cross(mainCamera.transform.right, Vector3.up).normalized + currentMovement.x * Vector3.Cross(Vector3.up, mainCamera.transform.forward).normalized; if (intendedMovement != Vector2.zero) { // Move (without gravity). Whenever we move we also readjust the player's direction to the direction they are running in. characterController.Move(absoluteMovement * (Time.deltaTime / 3.5f)); transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(absoluteMovement), Time.deltaTime * 10); } if (intendedMovement == Vector2.zero) { absoluteMovement = Vector3.Lerp(absoluteMovement, Vector3.zero, Time.deltaTime * 5); } characterController.Move(absoluteMovement * Time.deltaTime); } // animation Speed is a proportion of maximum runSpeed, and we smoothly transitions the speed with the Lerp float currentSpeed = characterAnimator.GetFloat("Speed"); float newSpeed = Mathf.LerpUnclamped(currentSpeed, currentMovement.magnitude / runSpeed, Time.deltaTime * (isWalking ? walkSpeed : runSpeed) * 3); characterAnimator.SetFloat("Speed", newSpeed); ForceHeightLevel(); }
// Update is called once per frame void Update() { if (Vector2.Distance(transform.position, target.position) > bufferArea) { transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime); } }
public void Chase(Transform target, float moveSpeed) { Vector3 difference = target.transform.position - transform.position; float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg; transform.rotation = Quaternion.Euler(0.0f, 0.0f, rotationZ); transform.position = Vector2.MoveTowards(transform.position, target.position, moveSpeed * Time.deltaTime); }
void patrol() { transform.position = Vector2.MoveTowards(transform.position, patrol_pos, movespeed * Time.deltaTime * TimeManager.Instance.getTimeMultiplier()); //rotate to look at the player transform.right = patrol_pos - (Vector2)transform.position; }
// Update is called once per frame void Update() { transform.position = Vector2.MoveTowards(transform.position, player.position, speed * Time.deltaTime); //transform.position = Vector2.Lerp(transform.position, player.position, speed * Time.deltaTime); if (transform.position.x == (target.x - 2)) { DestroyProjectile(); } }
private IEnumerator MoveCharacter(Vector2 toPosition) { while (_rigidbody2D.position != toPosition) { _rigidbody2D.position = Vector2.MoveTowards(_rigidbody2D.position, toPosition, Time.deltaTime * _movementSpeed); yield return(0); } _moving = false; _animator.SetBool("moving", false); }
private void Update() { if (movingBack == true) { transform.position = Vector2.MoveTowards(transform.position, currentPostion, 20f * Time.deltaTime); } if (transform.position.y == currentPostion.y) { movingBack = false; } }
private void Update() { // Looks at X movement mouse // transform.position = mousePos; Vector2 mousePos = Input.mousePosition; transform.position = Vector2.MoveTowards(gameObject.transform.position, mousePos, moveSpeed * Time.deltaTime); // Vector2 mousePos = new Vector2(xPos, 0); // transform.right = Vector2.MoveTowards(gameObject.transform.position, ) }
/** * Make enemy move freely */ private void wander() { //if the changeTime was reached, calculate a new movement vector if (Time.time - latestDirectionChangeTime > directionChangeTime) { latestDirectionChangeTime = Time.time; calcuateNewMovementVector(); } //move enemy: transform.position = Vector2.MoveTowards(transform.position, movementDirection, 1.3f * Time.deltaTime); }
/** * simple following command that follows target based off vector position */ void Chase() { // this will be used to chase the player transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime); anim.SetFloat("Xinput", (target.transform.position - transform.position).normalized.x); anim.SetFloat("Yinput", (target.transform.position - transform.position).normalized.y); // play mummy sound if not on cooldown if (canGroan) { StartCoroutine(Groan()); } }
void MoveToWaypoint() { var targetPosition = waypoints[waypointIndex].position; var movementThisFrame = waveConfig.GetMoveSpeed() * Time.deltaTime; transform.position = Vector2.MoveTowards(transform.position, targetPosition, movementThisFrame); float distance = Vector2.Distance(targetPosition, transform.position); if (transform.position == targetPosition) { waypointIndex++; } }
void Update() { if (Vector2.Distance(transform.position, target.position) >= distance) { transform.position = Vector2.MoveTowards(transform.position, target.position, (speed.value + 2) * Time.deltaTime); } if (Input.GetButtonDown("Fire1")) { if (laser.value) { GameObject bullet = ObjectPooler.Instance.GetPooledObject("Laser Bullet"); if (bullet != null) { bullet.transform.position = transform.position; bullet.SetActive(true); } } else { GameObject bullet = ObjectPooler.Instance.GetPooledObject("Player Bullet"); if (bullet != null) { bullet.transform.position = transform.position; bullet.SetActive(true); } } if (doubleM.value) { GameObject bullet = ObjectPooler.Instance.GetPooledObject("Double Bullet"); if (bullet != null) { bullet.transform.position = transform.position; bullet.SetActive(true); } } if (missile.value) { GameObject bullet = ObjectPooler.Instance.GetPooledObject("Missile Bullet"); if (bullet != null) { bullet.transform.position = transform.position; bullet.SetActive(true); } } } }
private IEnumerator FlyToward() { while (Target != null) { CorrectRotation(); transform.position = Vector2.MoveTowards(transform.position, Target.transform.position, speed * Time.deltaTime); yield return(null); } if (!hasImpacted) { Destroy(gameObject); } }
// Update is called once per frame void FixedUpdate() { if (photonView.IsMine) { foreach (Touch touch in Input.touches) { if (touch.phase == TouchPhase.Began)// So a touch is only registered once { if (containsHookPoint(touch)) { Vector2 Start = rb.position; Debug.Log("gor"); RaycastHit2D hit = Physics2D.Linecast(Start, currentTarget, 8); // Hits all colliders exept those in layer 2(hookpoint and player) onTheMove = (hit.collider == null); // if there are no colliders in the line, onTheMove is true // checks if the player does not have enough stamina for the grapple if (!(gameObject.GetComponent <playerManager>().currentStamina > staminaCost)) { onTheMove = false; } //If the player hasenough stamina for the grapple, do the grapple //and subtract the stamina cost from the players stamina else { gameObject.GetComponent <playerManager>().currentStamina -= staminaCost; Debug.Log(gameObject.GetComponent <playerManager>().currentStamina); } } } } if (onTheMove)//move towards current target as long as onTheMove is true { Debug.Log("On the move"); rb.transform.position = Vector2.MoveTowards(transform.position, currentTarget, grapplingSpeed); Debug.Log("moving"); if (!onTheMove) // if the player stops moving towards target, check if the player double clicked the target { if (doubleClicked) { doubleClick(); //if he doubleclicked the target } else { stick(); // if he didnt doubleclick, then you stick to the grapplepoint } } } } }
public void Update() { transform.position = Vector2.MoveTowards(transform.position, moveSpots[randomSpot].position, speed * Time.deltaTime); if (Vector2.Distance(transform.position, moveSpots[randomSpot].position) < 0.2f) { if (waitTime < -0) { randomSpot = Random.Range(0, moveSpots.Length); waitTime = startWaitTime; } else { waitTime -= Time.deltaTime; } } }
/** * run at players position */ private void Chase() { // this will be used to chase the player transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime); Vector2 direction = target.position - transform.position; anim.SetFloat("Xinput", direction.normalized.x); anim.SetFloat("Yinput", direction.normalized.y); // play laughter sound if not on cooldown if (canLaugh) { StartCoroutine(Laugh()); } }
// OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { if (_movePlayer) { PlayerController pc = FindObjectOfType <PlayerController>(); Transform player = pc.gameObject.transform; if (Math.Abs(player.position.x - _sceneLoader.transform.position.x) <= 0.1) { _movePlayer = false; pc.gameObject.GetComponent <Rigidbody2D>().velocity = Vector2.zero; return; } player.position = Vector2.MoveTowards(player.position, _sceneLoader.transform.position, Speed * Time.deltaTime); } }
private void Move() { if (transform.position.x < target.x && !reachedTarget) { _spriteRenderer.flipX = true; } else { if (Math.Abs(transform.position.x - target.x) > 0.05f && !reachedTarget) { _spriteRenderer.flipX = false; } } Vector2 position = Vector2.MoveTowards(transform.position, target, Time.deltaTime * speed); _rb.MovePosition(position); }
void MoveHorizontallyTowards(ref float xDestination) { float roomWidth = roomBackgroundModel.GetComponent <RectTransform>().rect.width; float playerWidth = playerModel.GetComponent <RectTransform>().rect.width; if (xDestination > roomWidth / 2 - playerWidth / 2) { xDestination = roomWidth / 2 - playerWidth / 2; } else if (xDestination < -roomWidth / 2 + playerWidth / 2) { xDestination = -roomWidth / 2 + playerWidth / 2; } transform.position = Vector2.MoveTowards(transform.position, new Vector2(xDestination, transform.position.y), speed * Time.deltaTime); }
// Update is called once per frame void FixedUpdate() { if ((Vector2)transform.position != targetposition) { speed = Random.Range(0.1f, 1.5f); transform.position = Vector2.MoveTowards(transform.position, targetposition, speed * Time.deltaTime); if ((Vector2)transform.position == originalPos) { transform.position = Vector2.MoveTowards(transform.position, transform.position, speed * Time.deltaTime); } } else { targetposition = rotationCenter.position; } }
bool IsOverLedge() { if (!_controller.State.IsGrounded) { return(false); } // cast ray a little forward than center to see if overhang is > 50% Vector3 forward = _character.IsFacingRight ? transform.right : -transform.right; Vector2 origin = Vector2.MoveTowards(transform.position, transform.position + forward, ledgeCheckRayOffset); float rayLength = ((CorgiControllerOverride)_controller).BoundsHeight / 2 + 0.05f + rayOffset; RaycastHit2D rc = MMDebug.RayCast(origin, -transform.up, rayLength, _controller.PlatformMask, Color.magenta, true); if (rc.collider == null) { return(true); } return(false); }
void hunt() { if (Vector2.Distance(transform.position, last_player_pos) >= 0) { transform.position = Vector2.MoveTowards(transform.position, last_player_pos, movespeed * Time.deltaTime * TimeManager.Instance.getTimeMultiplier()); } //rotate to look at the player transform.right = last_player_pos - (Vector2)transform.position; // Gone to last seen pos, and cannot find if ((Vector2)transform.position == last_player_pos) { // No clue where the player has gone had_LOS = false; // Will help other nearby enemies alerted = true; } }
// Update is called once per frame void FixedUpdate() { //Calculamos el nuevo punto donde hay que ir en base a la variable de destino Vector2 newPos = Vector2.MoveTowards(this.transform.position, destination, speed); //usamos el rigidbody para transportar a pacman hasta dicha posicion GetComponent <Rigidbody2D>().MovePosition(newPos); float distanceToDestination = Vector2.Distance((Vector2)this.transform.position, destination); if (distanceToDestination < 1f) { if (Input.GetKey(KeyCode.UpArrow) && CanMoveTo(Vector2.up)) { destination = (Vector2)this.transform.position + Vector2.up; } if (Input.GetKey(KeyCode.RightArrow) && CanMoveTo(Vector2.right)) { destination = (Vector2)this.transform.position + Vector2.right; } if (Input.GetKey(KeyCode.DownArrow) && CanMoveTo(Vector2.down)) { destination = (Vector2)this.transform.position + Vector2.down; } if (Input.GetKey(KeyCode.LeftArrow) && CanMoveTo(Vector2.left)) { destination = (Vector2)this.transform.position + Vector2.left; } } Vector2 dir = destination - (Vector2)this.transform.position; GetComponent <Animator>().SetFloat("X", dir.x); GetComponent <Animator>().SetFloat("Y", dir.y); }
IEnumerator Moving(Vector2 target, float speed, bool smooth) { targetPosition = target; //now we want to get the padding for our character Vector2 padding = anchorPadding; //now get the limitations for 0 to 100% movement. The farthest a character can move to the right before 100% should be the 1 value float maxX = 1f - padding.x; float maxY = 1f - padding.y; //now get the actal position target for the minimum anchors(left/bottom bounds) fo the character. because maxX and maxY is just a percentage. Vector2 minAnchorTarget = new Vector2(maxX * targetPosition.x, maxY * targetPosition.y); //move until we reach end of frame speed *= Time.deltaTime; while (root.anchorMin != minAnchorTarget) { root.anchorMin = (!smooth) ? Vector2.MoveTowards(root.anchorMin, minAnchorTarget, speed) : Vector2.Lerp(root.anchorMin, minAnchorTarget, speed); root.anchorMax = root.anchorMin + padding; yield return(new WaitForEndOfFrame()); } StopMoving(); }
void HandleMovement() { if (isAngry) { Vector2 newPosition = Vector2.MoveTowards(rb2d.position, player.GetComponent <Transform>().position, Time.fixedDeltaTime * moveSpeed); rb2d.MovePosition(newPosition); } else { if (path == null) { return; } if (rb2d.position != currentPathTarget) { Vector2 newPosition = Vector2.MoveTowards(rb2d.position, currentPathTarget, Time.fixedDeltaTime * moveSpeed); rb2d.MovePosition(newPosition); } else { if (currentPathTargetIndex + 1 > path.positions.Count - 1 && isPatrolling) { currentPathTargetIndex = 0; } else if (currentPathTargetIndex + 1 > path.positions.Count - 1 && !isPatrolling) { moveSpeed = 0; } else { currentPathTargetIndex += 1; } currentPathTarget = path.positions[currentPathTargetIndex]; } } }
private void FixedUpdate() { //move towards current target as long as onTheMove is true if (onTheMove) { transform.position = Vector2.MoveTowards(transform.position, currentTarget, grapplingSpeed); //Does not start rotating until close to destination if (Vector2.Distance(transform.position, currentTarget) < 3.2) { landingRotation(); } if (!onTheMove) // if the player stops moving towards target, check if the player double clicked the target { if (doubleClicked) { doubleClick(); //if he doubleclicked the target } else { stick(); // if he didnt doubleclick, then you stick to the grapplepoint } } } }
// Update is called once per frame void Update() { animator.SetFloat("Movement", Vector2.Distance(player.transform.position, transform.position)); if (currentState == "Patrol") { Vector2 nextposition = Vector2.MoveTowards(transform.position, nextwaypoint.position, Time.deltaTime * speed); transform.position = nextposition; if (transform.position == waypoint1.position || transform.position == waypoint2.position) { if (nextwaypoint == waypoint1) { nextwaypoint = waypoint2; } else { nextwaypoint = waypoint1; } } //emile code currentTarget = nextwaypoint.position - transform.position; transform.up = currentTarget; if (targetAquired()) { currentState = "Move"; } } else if (currentState == "Move") { var Target = player.transform.position; var timeToMousePosition = Target - transform.position; transform.up = timeToMousePosition; if (Vector2.Distance(player.transform.position, transform.position) > 10f) { Vector2 moveToplayer = Vector2.MoveTowards(transform.position, player.position, Time.deltaTime * speed); transform.position = moveToplayer; animator.SetBool("Enemy.Shoot", false); } if (!targetAquired()) { currentState = "Patrol"; } } bool targetAquired() { GameObject targetGO = GameObject.FindGameObjectWithTag("Player"); bool inRange = false; bool inVision = false; if (Vector2.Distance(targetGO.transform.position, transform.position) < range) { inRange = true; } var linecast = Physics2D.Linecast(transform.position, targetGO.transform.position, mask); if (linecast.transform == null) { inVision = true; } return(inRange && inVision); } }