#Value addition

1 messages · Page 1 of 1 (latest)

verbal wharf
#

?

dense kayak
#

so

verbal wharf
#

Alright

dense kayak
#

thsi si the code

verbal wharf
#

So

#

You don’t have to do all that

dense kayak
#

and it fire every 8 seconds

#

instead of 1

verbal wharf
#

If you want to add 1 every 1 second right?

#

Do you want to add 1 every second?

dense kayak
#

wait

#

i want

#

so i can atak

#

to be 1

#

after atking to be 0

tardy oyster
#

you want to attack every second

dense kayak
#

and the waitnig another

#

1 second

#

to can atack agian

verbal wharf
#

Oh so you want a timer. You don’t want to add a value every second

#

A cooldown timer?

dense kayak
#
using System.Collections.Generic;
using UnityEngine;

public class enemy : MonoBehaviour
{
    public Transform player;
    Rigidbody2D rb;
    Vector2 movement;
    public float moveSpeed = 5f;
    [SerializeField] float dmg = 10f;
    [SerializeField] float attackSpeed = 1f;
    float canAttack =1f;

    void Start()
    {
        rb = this.GetComponent<Rigidbody2D>();
    }

    
    void Update()
    {
        Vector3 direction = player.position - transform.position;
        float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg - 90f;
        rb.rotation = angle;
        direction.Normalize();
        movement = direction;
        
    }


    private void FixedUpdate()
    {
        MoveCharcater(movement);
    }


    void MoveCharcater(Vector2 direction)
    {
        rb.MovePosition((Vector2)transform.position + (direction * moveSpeed * Time.deltaTime));
        
    }

    void OnCollisionStay2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "Player")  
        {
            if (attackSpeed <= canAttack)
            {
                collision.gameObject.GetComponent<PlayerHealth>().UpdateHealth(-dmg);
                canAttack = 0f;
            }else
            {
                canAttack += Time.deltaTime;
            }
        }
    }


}
#

this is wath the tutorial said to doo

dense kayak
verbal wharf
dense kayak
#

nope

#

if the hit me

verbal wharf
#

If he hits you then it takes damage?

dense kayak
#

thsi is the code

dense kayak
#

they wait 1 scond

tardy oyster
#
    void OnCollisionStay2D(Collision2D collision)
    {
        float TimeBTWAttack = 1;
        float Time = 1;
        if (collision.gameObject.CompareTag("Player"))  
        {
            if (Time >= TimeBTWAttack)
            {
                collision.gameObject.GetComponent<PlayerHealth>().UpdateHealth(-dmg);
                Time = 0;
            }else
            {
                Time+= Time.deltaTime;
            }
        }
    }
``` try this maybe
dense kayak
#

and then hit me agian

verbal wharf
#

This is a simple vid

dense kayak
#

ok

#

i use gras

#

to see if it works

night fractal
#

ok, what's going on?

tardy oyster
#

he wants to shoot every second

dense kayak
#

trouble with a timer

#

dosent work

#

still takes like 8 seconds

#

to hit

#

yess

#

finaly

#

i need to take the values

#

and put them on colision enter

#

stay*

#

:DDDDD

night fractal
#

why are you using OnCollisonStay?

dense kayak
#

so i can chek every secind

#

and after that add a timer

#

ty yall for help

night fractal
#

should just use Update and only run the timer when the player has entered the collision . . .

#

you have two checks for the player. you check the tag and the PlayerHealth component. you only need to check for one of them . . .

dense kayak
#

i like how u say

#

but is a litle bit confusing

verbal wharf
#

I thought this person was trying to do it by collision

#

Why something is being collided