I am making a "boss fight" in unity2d. I wanted my player to get Iframes on hit. And it works but it bugs out so quickly. Any idea why it gets bugged? [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 days ago.
Improve this question
So basically i did a character with 3 health. It seemed like this character needed iframes because he was getting stuck to death very quickly. So i made it so when he gets hit he is untouchable for 5 seconds and it worked but was bugging out too frequently (in about every 8-9 hits).
What i mean by bugging out is... My character did not get hit at all, infinitely. until my character died or i restarted the game.
Here is my code
If you need any of the other codes just say it.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Iframes : MonoBehaviour
{
Renderer rend;
Color c;
GameObject character;
// Start is called before the first frame update
void Start()
{
rend = GetComponent<Renderer> ();
c = rend.material.color;
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "Player")
{
StartCoroutine("GetInvulnerable");
}
}
IEnumerator GetInvulnerable()
{
Physics2D.IgnoreLayerCollision(8, 9, true);
yield return new WaitForSeconds(3f);
Physics2D.IgnoreLayerCollision(8, 9, false);
}
}

Related

How can i make a player stop moving when it touchs a collider? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
So i was wondering if there is a way to make a player move infinitely with just one click and just stop when colliding with another collider.
You can create a global property:
bool movePlayer = false;
In FixedUpdate method you can detect a mouse click and change movePlayer to true, and move the player if it values true:
void FixedUpdate() {
if (Input.GetMouseButtonDown(0))
movePlayer = true;
if (movePlayer) {
//move the player here, maybe with Translate method
}
}
With OnTriggerEnter method you can detect the collision and change movePlayer to false:
void OnTriggerEnter(Collider other) {
//you can check for some particular object or avoid this if
if (other.gameObject.name == "SomeObject")
movePlayer = false;
}

please help me with my game my game have a problem [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am making a objectpoolingManager which is supposed to put bullets under an empty object called objectpoolingManager and in my code i wrote that it must enable the bullet when i click the mouse button then after 2 second it will diables the bullet now my problem is that it is always disabled.
public GameObject bulletPrefab;
public int bulletAmount = 20;
private List<GameObject> bullets;
// Start is called before the first frame update
void Awake()
{
instance = this;
bullets = new List<GameObject>(bulletAmount);
for (int i = 0; i < bulletAmount;i++)
{
GameObject prefabInstance = Instantiate (bulletPrefab);
prefabInstance.transform.SetParent (transform);
prefabInstance.SetActive(false);
}
bullets.Add(bulletPrefab);
}
public GameObject GetBullet ()
{
foreach (GameObject bullet in bullets)
{
if(!bullet.activeInHierarchy)
{
bullet.SetActive (true);
return bullet;
}
}
GameObject prefabInstance = Instantiate (bulletPrefab);
prefabInstance.transform.SetParent (transform);
bullets.Add(bulletPrefab);
return prefabInstance;
}
}
Your getter/setter of objects in your pool manager should not instantiate or destroy gamobjects, but enable and disable them. That is the purpose of pooling.
It makes more sense to EnQueue/deQueue them and activate/deactivate like this.
private Queue<GameObject> objPool;
private Queue<GameObject> activeObj;
//get from pool
public GameObject GetObjFromPool(Vector3 newPosition, Quaternion newRotation)
{
GameObject newObject = objPool.Dequeue();
newObject.SetActive(true);
newObject.transform.SetPositionAndRotation(newPosition, newRotation);
//keep actives to be retrieved
activeObj.Enqueue(newObject);
return newObject;
}
//return to pool
public void ReturnObjToPool(GameObject go)
{
go.SetActive(false);
objPool.Enqueue(go);
}
Find this question in case its helpfull.
You can find plenty of pooling examples if you goolgle that up. Here is one.

Unity pong game ball physics slowing issue [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
In my game for pong, the ball is supposed to bounce and never become slower. However, the ball is steadily slowing down over time. I will put an image of the ball object and the scripts.
Here is the ball properties on the left
Here is the ball script
using UnityEngine;
using System.Collections;
public class Ball : MonoBehaviour
{
public float ballVelocity = 3000;
Rigidbody rb;
bool isPlay;
int randInt;
void Awake()
{
rb = GetComponent<Rigidbody>();
randInt = Random.Range(1,3);
}
void Update()
{
if (Input.GetMouseButton(0) && isPlay == false)
{
transform.parent = null;
isPlay = true;
rb.isKinematic = false;
if (randInt == 1)
{
rb.AddForce(new Vector3(ballVelocity, ballVelocity, 0));
}
if (randInt == 2)
{
rb.AddForce(new Vector3(-ballVelocity, -ballVelocity, 0));
}
}
}
}
and here is the bounce physics image
and since I have no idea why it won't work, here is my physics project settings
I have been stuck and am new to unity so any help would be awesome! If you need any more info, comment!
Go to you assets folder and create a PhysicMaterial and set both Frictions to (Static and Dynamic) to 0 and Bounciness to 0.

Instantiate only once? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
How do Instantiate only once? This script continuously create clones.
GameObject[] cubeParticules;
void OnTriggerEnter()
{
foreach(GameObject part in cubeParticules)
{
Instantiate(part, temp1, Quaternion.identity);
}
}
No scripts attached to cubeParticules
The foreach term (if it wasn't obvious enough) performs the contained code for each object that is true to the condition; in this case: for each game-object contained in the cubeParticles array.
After your edit:
Also, your colliders may be touching each other at multiple entry points.
Try using a bool flag that is set once the objects are initiated, and then reset in OnTriggerExit().
Here's some sample code:
void OnTriggerEnter()
{
...
if (!instantiated)
{
foreach(GameObject part in cubeParticules)
{
Instantiate(part, temp1, Quaternion.identity);
}
...
instantiated = true;
}
...
}
void OnTriggerExit()
{
...
instantiated = false;
...
}
I hope that helps!

How to hide a gui canvas after some time? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I am displaying a canvas in Unity 3D that shows the instructions of the game at the beginning of the level, which I want to be hidden after 5-6 seconds. ( I don't want to do it with keyDown)
I was wondering what is the best way/practise to do it?
Is it using Animation? Or using some asynchronous methods like coroutine?
IEnumerator hideUI (GameObject guiParentCanvas, float secondsToWait,bool show = false)
{
yield return new WaitForSeconds (secondsToWait);
guiParentCanvas.SetActive (show);
}
To call it, simply start the coroutine and pass in the name of the parent gameobject that contains the UI then pass in how many seconds you want it to wait for before hiding UI canvas. The third parameter is optional and is there if you want to show that UI again. Pass true to it to display that hidden GUI again.
Usage:
StartCoroutine (hideGUI (gameObject, 2.0f)); //Wait 2 seconds then hide UI
StartCoroutine (hideGUI (gameObject, 2.0f, true)); //Wait 2 seconds then show UI
With Unity, they have a WaitForSeconds function that will wait for x seconds, then do something. You can use this so when the level starts up, you wait 5-6 seconds then hide the GUI.
yield return new WaitForSeconds (5);
// GUI hide code
May use a delayed Invoke, like:
public class Hide : MonoBehaviour
{
public float after = 5.0f;
void Start()
{ Invoke("Disable", after); }
void Disable()
{ gameObject.SetActive (false); }
}
Or you can use Start() returning IENumerator, like:
public class Hide : MonoBehaviour
{
public float after = 5.0f;
IEnumerator Start()
{
yield return new WaitForSeconds(after);
gameObject.SetActive (false);
}
}

Categories