Ejemplo n.º 1
0
    // Update is called once per frame
    protected override void Update()
    {
        base.Update();

        timer -= Time.deltaTime;
        Vector3 pos = transform.position;

        //Make new wander point after getting close enough to previous point or running out of time
        if (Vector2.Distance(target.position, pos) < maxDistance || timer <= 0)
        {
            //Raycast forward so agent may eventually move from wall (wander target won't go past wall)
            RaycastHit2D raycast = Physics2D.Raycast(pos, transform.up, circleDistance);
            PlayerDebug.DrawRay(pos, transform.up * circleDistance, Color.blue, .5f);


            //Rigidbody will be null if nothing hit
            float dist = (raycast.rigidbody == null) ? circleDistance : raycast.distance;
            circlePosition = transform.position + transform.up * dist;


            bool position_within_bounds = false;
            while (!position_within_bounds)
            {
                target.position = (Vector3)(Random.insideUnitCircle * circleRadius + circlePosition);
                if (Physics2D.Raycast(pos, target.position - transform.position, Vector3.Distance(target.position, transform.position)).rigidbody == null)
                {
                    position_within_bounds = true;
                    print("found a position");
                }
            }

            timer = maxTime;
        }
    }
Ejemplo n.º 2
0
 // Update is called once per frame
 protected virtual void Update()
 {
     PlayerDebug.DrawRay(transform.position + Vector3.back, DirectionFacing * .75f, new Color(1f, 0, 0, 1f));
     if (animator != null)
     {
         animator.SetFloat("MoveSpeed", rb.velocity.magnitude);
         animator.SetBool("Stopping", direction.magnitude == 0);
     }
 }
Ejemplo n.º 3
0
    void FixedUpdate()
    {
        rb.AddForce(transform.up * Mathf.Min(maxAcceleration, acceleration * maxAcceleration));
        PlayerDebug.DrawRay(transform.position, transform.up, Color.white);
        //This clamping is not actually accurate as the addForce will increase the speed for a frame after
        //If you disable force when speed is over max speed that creates hysteresis
        //A correct force reduction is more complicated
        rb.velocity = Vector2.ClampMagnitude(rb.velocity, maxSpeed);

        TurnToTarget();
    }
Ejemplo n.º 4
0
    void DrawBoundary()
    {
        if (!selected)
        {
            return;
        }
        Color color = Color.red;

        PlayerDebug.DrawRay(transform.position + new Vector3(deadzone, -5f, 1f), Vector3.up * 10f, color);
        PlayerDebug.DrawRay(transform.position + new Vector3(-deadzone, -5f, 1f), Vector3.up * 10f, color);
        selected = false;
    }
Ejemplo n.º 5
0
    // Update is called once per frame
    void Update()
    {
        if (targets.Count == 0)
        {
            DrawBoundary();
            return;
        }

        //Get average positions of all transforms
        Vector3 averagePosition = new Vector3();

        for (int i = 0, targetsCount = targets.Count; i < targetsCount; i++)
        {
            averagePosition += targets [i].position;
        }
        averagePosition *= 1f / targets.Count;

        //Get max and min x to set camera projection size if necessary
        //
        //

        if (selected)
        {
            Vector3 zPosition = averagePosition;
            zPosition.z = transform.position.z + .5f;
            PlayerDebug.DrawRay(zPosition + Vector3.down * .5f, Vector3.up, Color.blue);
            PlayerDebug.DrawRay(zPosition + Vector3.left * .5f, Vector3.right, Color.blue);
        }

        //Push camera if average position moves outside of deadzone
        float x        = averagePosition.x - transform.position.x;
        float distance = Mathf.Abs(x);

        if (distance > deadzone)
        {
            float   direction = Mathf.Sign(x);
            Vector3 pos       = transform.position;
            pos.x = averagePosition.x - deadzone * direction;

            //transform.position = pos;
            transform.position = Vector3.Lerp(transform.position, pos, Time.deltaTime * moveSpeed);
        }

        DrawBoundary();
    }
Ejemplo n.º 6
0
	void Update(){
		PlayerDebug.DrawRay (transform.position, transform.up * indicatorLineLength, Color.white);
		PlayerDebug.DrawCircle(transform.position, GetComponent<CircleCollider2D>().radius,agentColor);
	}