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

HELP,How to change between two speeds smoothly?

$
0
0
I have to speeds One is when he detects the player 4.5f Second is when he just walking 2,5f **I want to create very smooth increase and decrease between these two** What I need to do? *Please help* Here is the Most Important part of it i think void Behaviours() { if (spotted == true) { arrow.SetActive (true); moveSpeed = 4.5f; } else { arrow.SetActive (false); moveSpeed = 2.5f; } if (flip == true && moveSpeed > 2.5f) { moveRight = !moveRight; } } And here is the full enemy script public float moveSpeed; public bool moveRight; public Transform sightStart, sightEnd; public Transform flipStart, flipEnd; public bool spotted = false; public bool flip = false; public GameObject arrow; public LayerMask detectionLayers; public Transform wallCheck; public float wallCheckRadius; public LayerMask whatIsWall; private bool hittingWall; public Transform edgeCheck; public float edgeCheckRadius; private bool notAtEdge; public float timer = 3f; //Чтобы изменить время остановки нужно изменить кроме этого значения еще значение ниже, того же timer Animator anim; private Rigidbody2D rb2d; void Start () { rb2d = gameObject.GetComponent(); anim = GetComponent(); } void Update () { anim.SetFloat ("Speed", Mathf.Abs(rb2d.velocity.x)); Raycasting (); Behaviours (); hittingWall = Physics2D.OverlapCircle (wallCheck.position, wallCheckRadius, whatIsWall); notAtEdge = Physics2D.OverlapCircle (edgeCheck.position, edgeCheckRadius, whatIsWall); } void FixedUpdate () { if (hittingWall || !notAtEdge) { if (timer > 0) { timer -= Time.deltaTime; return; } moveRight = !moveRight; timer = 3f; } if (moveRight) { transform.localScale = new Vector3 (0.75f, 0.75f, 1f); rb2d.velocity = new Vector2(moveSpeed, rb2d.velocity.y); } else { transform.localScale = new Vector3 (-0.75f, 0.75f, 1f); rb2d.velocity = new Vector2(-moveSpeed, rb2d.velocity.y); } Physics2D.IgnoreLayerCollision (16,16); //Даёт возможность не сталкиваться врагам } void Raycasting() { Debug.DrawLine (sightStart.position, sightEnd.position, Color.green); spotted = Physics2D.Linecast (sightStart.position, sightEnd.position, detectionLayers); Debug.DrawLine (flipStart.position, flipEnd.position, Color.red); flip = Physics2D.Linecast (flipStart.position, flipEnd.position, detectionLayers); } void Behaviours() { if (spotted == true) { arrow.SetActive (true); moveSpeed = 4.5f; } else { arrow.SetActive (false); moveSpeed = 2.5f; } if (flip == true && moveSpeed > 2.5f) { moveRight = !moveRight; } } }

Viewing all articles
Browse latest Browse all 1488

Trending Articles



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