#Value addition
1 messages · Page 1 of 1 (latest)
Alright
thsi si the code
you want to attack every second
Oh so you want a timer. You don’t want to add a value every second
A cooldown timer?
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
You want a cooldown timer?
like so
So if you hit them you have to wait a second before you can hit again?
If he hits you then it takes damage?
thsi is the code
no if they hit me
they wait 1 scond
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
and then hit me agian
Oh
---COUNTDOWN TIMER IN UNITY---
In this video I show you how to create a countdown timer. This video is very simple and great for beginners.
Time.deltaTime Docs - https://docs.unity3d.com/ScriptReference/Time-deltaTime.html
---Previous Video---
https://www.youtube.com/watch?v=qD7fDop-Ptw&t=3s
---My Social Media---
TWITTER: https://twitter.com/...
This is a simple vid
ok, what's going on?
he wants to shoot every second
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
why are you using OnCollisonStay?
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 . . .
Wait wait you don’t want it to happen on collision?
I thought this person was trying to do it by collision
Why something is being collided