please help me with my game my game have a problem [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 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.

Related

What would the best method be to make the player increase in size when they touch a "food" item? [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 4 months ago.
Improve this question
I'm very new to Unity and I'm developing a game were the player gets bigger every time they eat a food pellet.
I've already implemented the food mechanic but the player doesn't become bigger yet. What would be the best way of going about this? I know I might have to increase the scale of the Collison sphere and game object (duh) and I will have to increase the hight of the camera since it is a top down perspective that's very close to the player character. I know this I just don't know how.
I've tried directly setting the scale inside the if statement that controls the eating of the food, no luck.
my if statement if that helps:
private void OnTriggerEnter(Collider collision)
{
if (collision.tag == "Food Particle")
{
growth += 1;
Debug.Log(growth);
collision.gameObject.SetActive(false);
}
}
Help would be appreciated!
If you mean the GameObject should get bigger then you could change the scale. Best would be to make an animation which makes the mesh get bigger. But this isn't a tutorial forum. So limited to 'to get something on screen fast':
Please consider to take those dull tutorials. They only seem not to talk about your goal/game. They actually do.
private float scale_min = 1.0f;
private float scale_max = 2.0f;
private float energy_max = 1.0f;
private float energy_current = 0.0f;
// grow the game object
private void UpdateScale()
{
Vector3 scale = transform.localScale;
scale.y = scale_min + (scale_max - scale_min) * energy_current;
transform.localScale = scale;
}
// a method which consumes the food
// return true when consumed, otherwise false
private bool Feeding(float growth)
{
if(energy_current >= energy_max)
return false;
energy_current += growth;
if (energy_current > energy_max)
energy_current = energy_max;
return true;
}
private void OnTriggerEnter(Collider collision)
{
if (collision.tag == "Food Particle")
{
// if the food is consumed, remove the food game object
if(Feeding(0.1f))
{
collision.gameObject.SetActive(false);
}
}
}
Another script at the 'food' GameObject with a value of energy, or growth, would make the mechanism more generic.
public class Food : MonoBehaviour
{
public float energy = 0.1f;
}
This will need an adjustment for your OnTriggerEnter.
private void OnTriggerEnter(Collider collision)
{
// Get the Food component from the food game object
Food food = collision.GetComponent<Food>();
// Check if the game object actually has one
if(food)
{
// if the food is consumed, remove the food game object
if(Feeding(food.energy))
{
collision.gameObject.SetActive(false);
}
}
}
Rather then setting collision.gameObject inactive, destroying the object seems to be more appropiate.
Destroy(collision.gameObject);
you could simply create a parent game object and put everything inside and basically increase the scale of the parent object which will scale every object under it!

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;
}

How do I make my chest lid transform to open position when all 3 keys are collected, then have a portal activate from hierarchy? [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
Hi I am doing a project for a class and am a bit stumped. The player is to collect all the keys in the game to open the chest. What I want to happen is that when all 3 keys are collected, the player can go up to the chest and when it is collided with, the chest lid transforms into an open position. After this happens, I would like a deactivated portal from the hierarchy to activate into my scene. Please help, this is my key collector script.
int keys = 0;
public Text keyText;
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("key"))
{
Destroy(other.gameObject);
keys++;
keyText.text = "Keys: " + keys;
}
}
}
Well, from the code structure it seems you might be new to Unity. (I'm sorry if I'm mistaken!)
But I can suggest you a solution that can be done pretty easily.
Create a GameObject in your hierarchy that will serve as a "GameHandler" it has a purpose to handle some logic you need globally in your scene.
Create a GameHandler script and attach it to this GameObject. In this script, create a reference for the portal and the chest as well.
[SerializeField] private GameObject portal;
[SerializeField] private Transform chest;
After this, add a reference for GameHandler in the Key Collector script.
[SerializeField] private GameHandler handler;
(In the future, you should have a PlayerController script that manages every component like Key Collecting if you don't have already.)
Attach every GameObject to the scripts (add the references).
Now, create a function in GameHandler that manages the stuff you need.
private class GameHandler
{
[SerializeField] private GameObject portal;
[SerializeField] private Transform chest;
public void KeysCollected()
{
portal.SetActive(true);
chest.position = new Vector3(something, something, something);
chest.roation = Quaternion.Euler(something, something, something);
}
}
I don't really know what you want to do with the chest, but that part you can figure out yourself (opening by animation, modifying position and rotation of the chest lid, etc...). In this case I did the last one.
The last thing you need to do is calling the KeysCollected method uppon collecting all the keys.
GameHandler handler;
int keys = 0;
int maxKeys = 3;
public Text keyText;
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("key"))
{
Destroy(other.gameObject);
keys++;
keyText.text = "Keys: " + keys;
if(keys == maxKeys)
handler.KeysCollected();
}
}
That should do the trick, but this solution will get old as you proceed and gain knowledge. But for now, it should be okay.

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!

Categories