示例#1
0
    void Update()
    {
        if (playerCoreScript.CanMove())
        {
            float inputH = Input.GetAxisRaw("Horizontal");
            float inputV = Input.GetAxisRaw("Vertical");

            float deltaH = inputH * walkSpeed;
            float deltaV = inputV * walkSpeed;

            Vector2 vel = new Vector2(deltaH, deltaV);
            rigidBody.velocity = vel;

            //Move this to the animation script
            if (inputH != 0 || inputV != 0)
            {
                aniscr.Walk(inputH);
            }
            else
            {
                aniscr.Iddle();
            }
        }
    }
    // Update is called once per frame
    void Update()
    {
        //Set attack hitbox in place
        //Get mouse position in world coordinates
        Vector3 target = Camera.main.ScreenToWorldPoint(Input.mousePosition);

        //Set z of this position to zero. We are in 2d after all.
        target.z = 0f;
        //find the unit vector that goes from player to target
        target = (target - transform.position).normalized;
        //Multiply unit vector by a scalar end set its starting point at the center of the player
        attackPos.position = transform.position + (target * hitboxDistance);

        //Attack
        if (coolDown > 0)
        {
            coolDown -= Time.deltaTime;
        }
        else if (playerCoreScript.CanMove() && Input.GetButtonDown("Fire1"))
        {
            Collider2D[] enemiesHit = Physics2D.OverlapCircleAll(attackPos.position, hitboxRadius, enemyLayerMask);
            AttackInfo   aInfo      = new AttackInfo(attackPower, Vector3.zero, element);
            foreach (Collider2D e in enemiesHit)
            {
                //forceVector is different for each enemy hit
                aInfo.forceVector = (e.gameObject.transform.position - transform.position).normalized * attackForce;
                e.GetComponent <EnemyCoreScript>().TakeHit(aInfo);
            }
            //Instantiate slash effect

            GameObject slash = Instantiate(slashEffectPrefab, attackPos.position, Quaternion.FromToRotation(Vector3.right, target));
            slash.transform.localScale *= hitboxRadius;

            coolDown = attackCoolDown;
        }
    }