Quantcast
Channel: Questions in topic: "enemy"
Viewing all articles
Browse latest Browse all 1488

Enemy Chase and Jump : 2D Coroutines

$
0
0
Hi, I am struggling with a simple functionality to let the enemy jump once when it is within range of the player. My enemy (in this case a dog chasing a mouse based on the Jetpack Joyride Tutorial) either jumps all the time, or doesn't jump smoothly. It however needs to increase its move speed when it is within a certain range around the x-as. If the mouse is above a certain distance on the y-as the dog should just pass the mouse and return and flip after it reached the maxdistance. It does the latter correctly. I converted if-then-else into Coroutines and tried many time but without success ![alt text][1] using UnityEngine; using System.Collections; public class FollowMouse : MonoBehaviour { public Transform target; // capability to drag Character to this field in the inspector public float moveSpeed = 4f; // controlling Speed of my Character public float rotationSpeed; public float maxDistance = 2; // distance parameter private float distancex ; private float distancey ; public float accelarateSpeed = 4f; private bool grounded = true; public Transform groundCheckTransform; public LayerMask groundCheckLayerMask; private bool playerIsLeftFromEnemy = false; private float logspeed ; private float timer; public float waitAfterJump = 1.0f; private bool mouseIsDead = false; private float timerange; private bool jump = false; private bool withinrange; private bool attack_triggered; private float numberofjumps; Animator animatorDog; Animator anim; //animator private Transform myTransform; void Start () { } IEnumerator JumpLogic () { float minWaitTime = 2f; float maxWaitTime = 5f; timerange = Random.Range (minWaitTime, maxWaitTime); while (true) { yield return new WaitForSeconds (timerange); Jump (); } } void Jump () { myTransform.position += myTransform.up * (accelarateSpeed + 1.3f) * Time.deltaTime; } void Awake () { myTransform = transform; animatorDog = GetComponent (); } // Use this for initialization void UpdateGroundedStatus () { //1 grounded = Physics2D.OverlapCircle (groundCheckTransform.position, 0.1f, groundCheckLayerMask); animatorDog.SetBool ("grounded", grounded); } // Update is called once per frame void FixedUpdate () { if (mouseIsDead == false) { if (target.GetComponent ().dead == true) { animatorDog.SetBool ("MouseDead", false); mouseIsDead = true; Vector2 newVelocity = GetComponent ().velocity; newVelocity.x = 0; GetComponent ().velocity = newVelocity; } // Debug.DrawLine (target.position, myTransform.position, Color.red); UpdateGroundedStatus (); //Look at target distancex = myTransform.position.x - target.position.x; distancey = myTransform.position.y - target.position.y; if (distancex < 6f || distancex > -4f) { withinrange = true; } if (distancex > 6f || distancex < -4f) { withinrange = false; } if (distancex > 0 && withinrange == false) { if (playerIsLeftFromEnemy == false) { transform.Rotate (Vector3.up * -180); playerIsLeftFromEnemy = true; myTransform.position += myTransform.right * (accelarateSpeed + 1.0f) * Time.deltaTime; } else { myTransform.position += myTransform.right * (accelarateSpeed + 1.0f) * Time.deltaTime; } } if (distancex < 0 && withinrange == false) { if (playerIsLeftFromEnemy == true) { transform.Rotate (Vector3.up * -180); playerIsLeftFromEnemy = false; logspeed = accelarateSpeed; } else { myTransform.position += myTransform.right * (accelarateSpeed + 1.3f) * Time.deltaTime; } } if (distancex < 0 && withinrange == true && attack_triggered == false) { logspeed = accelarateSpeed + 1.3f; attack_triggered = true; myTransform.position += myTransform.right * (accelarateSpeed + 0.9f) * Time.deltaTime; StartCoroutine (JumpLogic ()); } if (distancex > 0 && withinrange == true && attack_triggered == false) { logspeed = accelarateSpeed + 0.9f; myTransform.position += myTransform.right * (accelarateSpeed + 0.9f) * Time.deltaTime; } } } } [1]: /storage/temp/47459-2015-06-01-14-12-52-unity-personal-64bit-rocketmou.png

Viewing all articles
Browse latest Browse all 1488

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>