Auto attacking on collision - unity3d

For a sidescrolling game I need to attack the enemies which collides with my player range, here is how I do :
void OnCollisionStay2D(Collision2D coll) {
Enemy target = coll.gameObject.GetComponent <Enemy> ();
if (target != null) {
moving = false;
anim.SetBool ("attack", true);
target.Damage (damageValue);
}
}
The problem is that i deal damageValue damages at each frame, so I wanted to add a cooldown, like this:
void OnCollisionStay2D(Collision2D coll) {
Enemy target = coll.gameObject.GetComponent <Enemy> ();
if (target != null && time.time > nextFire) {
moving = false;
anim.SetBool ("attack", true);
target.Damage (damageValue);
nextFire = Time.time + 1;//I set the cooldown to 1 second
}
}
The problem is that the player is attacking one time, and never again, I tried to Debug.Log(Time.time) at the end of OnCollisionStay method, and the debug spam is stopping just before the time where Time.time > nextFire;
I tried to Debug.Log at the start of the method, and it seems like OnCollisionStay is not called anymore after this debug freeze, which occurs around 1s - time.deltaTime after the first attack.
So I don't know how to do, basically here it is a 1D side-scrolling, you can just go from right to left, this should be really simple but I don't get it.

You may consider to create a fire rate for the weapon so it is changeable to one bullet at a time or serial fire. You will have control over it. Also prefer raycasting instead of collision. There is a 2D platformer tutorials around but I very liked the one from Brackeys.
YouTube Playlist

Related

How to find out who shot a projectile

So I am trying to make a multiplayer game with abilities sort of like Overwatch/Paladins. All in all, one ability should be a sort of projectile that moves across the ground and allows that player to teleport to its position at any time while it is alive. I can't find the solution to teleporting only the player that shot it since thus far in my tests, when one player activated their ability, all players would teleport. How can I solve this?
My code:
void Update()
{
if (Input.GetKeyDown(KeyCode.E))
GetComponent<playerController>().heldAbility = "gateCrash";
if (GetComponent<playerController>().heldAbility == "gateCrash")
holding = true;
else
holding = false;
if (holding && Input.GetMouseButtonDown(0))
PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs", "GateCrashModel"), spawnPos, transform.rotation, 0);
}
This code is attached to the projectile:
public float speed = 10;
PhotonView pv;
private void Awake()
{
pv = transform.GetComponent<PhotonView>();
}
private void FixedUpdate()
{
transform.Translate(transform.forward * speed * Time.fixedDeltaTime);
}
I guess that I should make have something as instatntiation parameter but idk what.
A simple approach would be to add a shotOwner property to each of your projectiles. Every time a projectile is fired, update shotOwner to point to the player object that fired the shot. (This will also let you implement "Player_X killed Player_Y" functionality, among other things.)

Unity: Player jumps fine, but falls extremly slow

I am currently building a little 2D platformer with a character that can jump. This is how the jumping looks (FixedUpdate):
if (jump)
{
if (isGrounded)
{
isGrounded = false;
rb.AddForce(Vector2.up * (jumpHeight * counterForJumpHeight) * Time.deltaTime, ForceMode2D.Impulse);
jump = false;
anim.SetBool("bool_anim_isJumping", true);
}
if (timer != null)
timer.Stop();
counterForJumpHeight = jumpMulitMin;
jumpAlreadCharging = false;
}
Looks perfect for every jump up and then falling back down.
HOWEVER: when the player JUST falls (like off a cliff oder something) without a jump it looks like he has the mass of a leaf. Sailing to the ground extremley slowly. Not accelerating at all. Just falling as in slow motion. Of course I can up the gravity, but that also affects the falling AFTER my jump and makes him look like a stone. As if the falling is sped up or something. But that doesnt make sense. Him falling AFTER a jump and him just falling off of something SHOULD look the same, right? But it doesnt.
These are my values for the RB:
YOu can either up your player's rigidbody Mass, or increase gravity in edit/projectsettings/ physics
I'm not 100% sure but this may have to do with the collision detection. Try changing the setting to continuous dynamic instead of discrete.
It can be a problem with the animator, if the animation contains changes to Rigidbody, it does weird stuff if you have set the Make Default in animation.
it was all my fault, there was no way of guessing it from your point of view.
Everything the player could jump from was tagged with "can_jump".
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag == "can_jump") // detect collision with ground game object
{
isGrounded = true;
deacceleratePerFrame = 1.5f;
anim.SetBool("bool_anim_isJumping", false);
}
}
This includes all edges. Removing this means I cannot jump atm anymore, however this caused the issue. I sure find another way :-) thank you all

Unity 2D same state for different enemies issue

I'm making 2D platformer and im getting weird bug - when i hit one enemy, every one else of that type getting the same state. I mean i wanted them to enter "angry" state when my player hit one of them with sword.
Problem looks like this:
->Hit Enemy1 with sword,
->Enemy1 is getting damage and going to "angry" state,
->Enemy2/3/4/5 didn't get damage, but went in "angry" state.
If getting damage is individual for every one of them so why am i suffer that kind of situation. I think the cause is in my Damage Script. If anyone have an idea i would be glad to hear it. Thanks in advance.
public void DealDmg()
{
blob = FindObjectOfType<EnemyScript>();
if (attackPos.gameObject.activeSelf == true)
{
Collider2D[] enemiesToDamage = Physics2D.OverlapCircleAll(attackPos.position, attackRange, whatIsEnemy);
for (int i = 0; i < enemiesToDamage.Length; i++)
{
EnemyScript enemyScript = enemiesToDamage[i].GetComponent<EnemyScript>();
enemyScript.GetComponent<EnemyScript>().TakeDmg(damage);
if (gameObject.GetComponent<PlayerControls>().facingRight == true)
{
enemyScript.GetComponent<Rigidbody2D>().AddForce(new Vector2(30f, 20f), ForceMode2D.Impulse);
StartCoroutine("ResetValues");
blob.CalmLogic();
}
else if (gameObject.GetComponent<PlayerControls>().facingRight == false)
{
enemyScript.GetComponent<Rigidbody2D>().AddForce(new Vector2(-30f, 20f), ForceMode2D.Impulse);
StartCoroutine("ResetValues");
blob.CalmLogic();
}
}
attackPos.gameObject.SetActive(false);
}
}
public IEnumerator ResetValues()
{
gotHit = true;
blob.gotAngry = true;
yield return new WaitForSeconds(resetValuesDelay);
gotHit = false;
yield return new WaitForSeconds(resetValuesDelay);
blob.gotAngry = false;
}
Track enemy state inside the enemy
Right now it looks like your code is running on the player and the player is keeping track of enemy state. This is wrong. Your player script should never know anything about (much less modify!) the properties of your enemy. All the player should do is:
when the player successfully hits an enemy, the player tells the enemy "you've been hit!"
The enemy script then takes that information (such as how much damage, what damage type it was, etc) and deal with itself: reducing the damage ("am I wearing armor?"), enhancing the damage ("am I weak to fire?") and so on and so forth...like getting angry at the player and the resulting cooldown before its calm again.
Your player script does not need to know (and does not care!) about this information and any attempt to try and handle it here is folly. As soon as you change how many enemies there are, or create a new enemy type that does something ELSE you end up with a giant pile of useless junk you can't manage.

OnTrigger Events Work With Delay

I've got a bullet script with a particle system and a decal.
I think that it has something to do with these events not being able to fire in time or with fps in update. Not sure yet.
So, it's being late.
The ellow points are where the particles start to play. They should be right on these wooden walls. There should be three particles working and three bullet holes, kinda bullet penetrating one wall and getting destroyed on the second one.
THE QUESTION IS HOW TO MAKE IT WORK NORMAL, SO THAT THE TRIGGERS WORK WHEN NEEDED AS WELL AS THE PARTICLES AND THE DECALS? Maybe there's a way to excellerate the code to work on time? Or maybe there's another problem with that?
The screenshot:
The Code:
public class BulletScript : MonoBehaviour {
public bool isThrough = true;
public float BulletSpeed = 100;
public float CurrentDamage;
public float EnterLuft = -0.005f;
public float ExitLuft = 0.05f;
public GameObject woodParticle;
private ContactPoint CollisionPoint;
public GameObject BulletMarkPref;
Rigidbody bullet;
private void Start()
{
bullet = this.GetComponent<Rigidbody>();
}
void FixedUpdate () {
bullet.velocity = Vector3.forward * BulletSpeed;
//this.transform.Translate(Vector3.forward * BulletSpeed * Time.deltaTime);
}
private void OnTriggerEnter(Collider other)
{
Transform hitPoint = this.transform;
LevelObject.LvlObjectType objectType = other.gameObject.GetComponent<LevelObject>().objType;
if(objectType == LevelObject.LvlObjectType.obstacle)
{
if (isThrough)
{
Instantiate(woodParticle, hitPoint.localPosition, Quaternion.LookRotation(-hitPoint.forward)).GetComponent<ParticleSystem>().Play();
LeaveBulletMark(this.transform, true);
}
else
{
Instantiate(woodParticle, hitPoint.localPosition, Quaternion.LookRotation(-hitPoint.forward)).GetComponent<ParticleSystem>().Play();
LeaveBulletMark(hitPoint, true);
Destroy(this.gameObject);
}
}
else if(objectType == LevelObject.LvlObjectType.obstacle)
{
Destroy(this.gameObject);
}
else if(objectType == LevelObject.LvlObjectType.wall)
{
LeaveBulletMark(hitPoint, true);
Destroy(this.gameObject);
}
}
private void OnTriggerExit(Collider other)
{
Transform hitPoint = this.transform;
Instantiate(woodParticle, hitPoint.localPosition, hitPoint.rotation).GetComponent<ParticleSystem>().Play();
LeaveBulletMark(hitPoint, false);
}
void LeaveBulletMark(Transform hitPoint, bool ifEnter)
{
GameObject TemporaryBulletMarkHandler;
TemporaryBulletMarkHandler = Instantiate(BulletMarkPref, hitPoint.localPosition, Quaternion.LookRotation(ifEnter ? hitPoint.forward : CollisionPoint.normal)) as GameObject;
isThrough = false;
TemporaryBulletMarkHandler.transform.Translate(hitPoint.forward * (ifEnter ? 0.005f : -0.005f));
}
}
I don't think your problem is something simple with the code. There is an inherent issue with calculating fast moving objects like bullets with true physics calculations especially if they are small. Often between physics updates, they pass through wall colliders completely without registering.
You have to think of it like this to see the problem. The bullet isn't tracked continuously along its trajectory. It has a starting location, a formula for its movement and it calculates a new location at each physics update. You could fire a bullet straight at a wall, and in one update the bullet would be several meters in front of the wall, and in the next, it would be several meters behind the wall without ever triggering a collision. This is why so many game use ray tracing to calculate bullet trajectories. The bullet movement isn't perfectly accurate, especially for long shots, but obstructions to the bullet path are registered.
By default unity's Physics Engine runs at 50 frames per second. A modern bullet travels between 1200 and 1700 m/s. That gives you a distance between 24 and 34 meters traveled between frames. Even a small object falling at terminal velocity (54 m/s) might pass through a collider unnoticed. If you made a 1-meter thick box collider, you would likely register a falling object but not a bullet.
I think you could do some clever combination of ray tracing and bullet physics to get the best of both worlds. Maybe you could ray trace from the bullet at each fixed update or there may be some better technique already invented for this exact situation that I don't know about.

How to change a sprite to another and then back after 1 second

I'm developing a simple game in Unity2D, in which several monsters eat things that are dragged to them. If the right object is dragged to the monster, the score goes up by one and the monster should make a happy face, otherwise, score goes down and makes a sad face. This is the code I'm using for that (minus the transitions to happy/sad):
if (transform.name.Equals ("yellow")){
if (collinfo.name.Equals ("plastic(Clone)")) {
Debug.Log ("+1");
audio.Play ();
GameSetup.playerScore += 1;
gs.GetComponent<GameSetup>().removeit(aux);
}
else {
Debug.Log ("-1");
audio.Play ();
if (GameSetup.playerScore == 0)
{}
else
{
GameSetup.playerScore -= 1;
}
gs.GetComponent<GameSetup>().removeit(aux);
}
The audio played is just a 'munching' sound.
I want the monster to change sprites to happyFace (via GameObject.GetComponent ().sprite = happyFace), wait one second and then switch back to it's normal sprite, but don't know how to implement that waiting period.
Any and all help would be much appreciated.
This can be implemented several ways, however, I would use a method that returns an IEnumerator like so…
This assumes you have a variable in your script that has a reference to the SpriteRenderer that is attached to the GameObject with this script.
SpriteRenderer sr = GetComponent <SpriteRenderer> ();
I also assume you have an original sprite and the possible sprites to change to as variables too.
public Sprite originalSprite;
public Sprite happyFaceSprite;
public Sprite sadFaceSprite;
public IEnumerator ChangeFace (Sprite changeToSprite)
{
sr.sprite = changeToSprite;
yield return new WaitForSeconds (1.0f);
sr.sprite = originalFaceSprite;
}
You would then call this function with the applicable sprite as the variable.
if (happy)
StartCoroutine (ChangeFace (happyFaceSprite);
else
StartCoroutine (ChangeFace (sadFaceSprite);
Because the ChangeFace method returns an IEnumerator, we must call that function with the StartCoroutine function. The method will run until it reaches the yield return new WaitForSeconds (1.0f) function, then wait for 1.0f seconds and then resume where it stopped last time.
Understand?
Note
I haven't tested this but I don't see why it wouldn't work.
Put a floating point variable in your monster controller, call it happyTimer or something. It should start at zero.
Then in your Update function you should check happyTimer is above zero. If it is, then subtract Time.deltaTime and check again. If happyTimer is zero or below for the second check, then call your function that resets the sprite.
When you set the sprite to "happy face", also set happyTimer = 1. This will begin the countdown starting with the next Update call.
The relevant part of Update would look like this:
if(happyTimer > 0) {
happyTimer -= Time.deltaTime;
if(happyTimer <= 0) {
resetSprite();
}
}