I'm coding a simple farming simulator in Unity, as it is my first game. I want to make it so that the turnip spawns after 5 seconds, but it doesn't spawn at all. I will make the amount of time that it takes the turnip to spawn a public variable that I can change in the inspector. The C# Script is attached to the player, which is a capsule, hence the SphereCast. Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PickUpItems : MonoBehaviour
{
//Variables here
public float reach;
public GameObject mainCamera, turnip;
public Text txt;
RaycastHit hit;
RaycastHit onFarmLand;
public LayerMask layerMask, farmLand;
// Voids here
private IEnumerator Plant()
{
if (Physics.SphereCast(transform.position, 0.48f, Vector3.down, out onFarmLand, 1.5f, farmLand))
{
txt.text = "Click Left Ctrl to plant the turnip";
if (Input.GetKeyDown(KeyCode.LeftControl))
{
yield return new WaitForSeconds(5);
Instantiate(turnip, new Vector3(transform.position.x, 0, transform.position.z), Quaternion.identity);
}
}
}
void Harvest()
{
if (Physics.Raycast(transform.position, mainCamera.transform.forward, out hit, reach, layerMask))
{
txt.text = "Click Left Ctrl To Grab The Object";
if (Input.GetAxis("Fire1") == 1)
{
Destroy(hit.transform.gameObject);
}
}
else
{
txt.text = "";
}
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
StartCoroutine(Plant());
Harvest();
}
}
You shouldn't put the OnKeyDown check in the corutine, what you instead should do is checking in the Update method if the key is down, then spherecast and if it hit something start the corutine, it should look something like this:
void Update()
{
if(Input.GetKeyDown(KeyCode.LeftControl))
{
if( Here you spherecast )
{
StartCorutine(Plant());
}
}
}
and then in your corutine you can just wait and then Instantiate:
private IEnumerator Plant()
{
yield return new WaitForSeconds(5);
Instantiate( ... );
}
This is a common coroutine missunderstanding of how their work.
Your coroutine method is not a while loop, you have to build the loop INSIDE your coroutine method.
So instead of checking if the key is pressed, to avoid an infinite loop, we will check the opposite: if the key is not pressed, keep waiting, if it's pressed, do the instantiation.
private IEnumerator Plant()
{
//while the key is not pressed, or the SphereCast is returning false keep looping
while(!Input.GetKeyDown(KeyCode.LeftControl) || !Physics.SphereCast(transform.position, 0.48f, Vector3.down, out onFarmLand, 1.5f, farmLand))
{
yield return null;
}
//if key is pressed and SphereCast returns true, wait 5 seconds and do the spawn
yield return new WaitForSeconds(5);
Instantiate(turnip, new Vector3(transform.position.x, 0, transform.position.z), Quaternion.identity);
}
Checking Raycasts inside a Coroutine is not the most optimal solution, but I hope you can understand how coroutines works.
Related
Okay, this is probably a dumb question but I'm new to this. I have an enemy AI that walks toward the player only when the enemy is visible to the player and the space key is pressed. I want to make a second if statement that makes the enemy run if the player presses the space bar a second time while the enemy is walking or if the enemy is within 2 meters of the of the players current position.
{
public NavMeshAgent enemy;
public Transform player;
public float speedWalk = 6f;
public float speedRun = 60f;
public float groundDrag;
public float playerHeight;
bool isWalking;
Renderer m_Renderer;
void Move(float speed)
{
enemy.speed = speed;
}
private void Start()
{
m_Renderer = GetComponent<Renderer>();
isWalking = false;
enemy.speed = speedWalk;
}
private void OnBecomeInvisible()
{
enabled = false;
}
//DelayEnemyChase
IEnumerator delayChase()
{
yield return new WaitForSeconds(2);
Move(speedWalk);
enemy.SetDestination(player.position);
}
//Visible by camera
void OnBecameVisible()
{
enabled = true;
//starts walking towards player position
if ((Input.GetKey(KeyCode.Space)) && (m_Renderer.isVisible) && (isWalking == false))
{
StartCoroutine(delayChase());
isWalking = true;
}
//starts walking towards player position
else if ((isWalking == true) && (Input.GetKey(KeyCode.Space)) && (m_Renderer.isVisible))
{
Move(speedRun);
enemy.SetDestination(player.position);
isWalking = false;
}
}
private void Update()
{
//sees if enemy is visible + space bar is pressed
OnBecameVisible();
}
}
This is confusing the heck out of me, this is what I have and it's not workign at all. Any help is appreciated!!!!
The main issue is that GetKey is fired every frame as long as the button stays pressed!
You rather want to use GetKeyDown in order to track only the first key press.
Then you currently also start and run multiple concurrent Coroutines!
I would rather use a kind of state routine and do e.g.
private void OnBecomeInvisible()
{
StopAllCoroutines();
enemy.enabled = false;
}
private void OnBecameVisible()
{
enemy.enabled = true;
Move(0f);
enemy.SetDestination(enemy.transform.position);
StartCoroutine (StatesRoutine());
}
private IEnumerator StatesRoutine ()
{
// wait until the space is pressed the first time
// here it depends on what exactly you want to do
// you can either already track if the key is still pressed already
yield return new WaitUntil (() => Input.GetKey(KeyCode.Space));
// or rather wait until the key goes down the first time after having become visible
//yield return new WaitUntil (() => Input.GetKeyDown(KeyCode.Space));
yield return new WaitForSeconds(2);
Move(speedWalk);
enemy.SetDestination(player.position);
// Then for the second press we definitely wait until it gets down again instead of
// only checking if the button is still pressed
// except again your use case actually wants that behavior
yield return new WaitUntil (() => Input.GetKeyDown(KeyCode.Space));
Move(speedRun);
enemy.SetDestination(player.position);
}
Some things still depend on your exact needs though, in particular what shall happen if the enemy becomes invisible. For now I assume you wanted to reset the behavior and start the process of handling space clicks from scratch.
I'm making a 2D game and i have spawning potion items in a making potion scene first of all i want to make the item pop up after spown and go like in this video that i recorded from my game:
Spawned Item do not move like the original
what do i do to make the spawned (Cloned) Item move the same as the original potion item?
Secondly, i want to spawn random and more than one item (potion) each time the scene starts how do i do that knowing that i'm using this script that spawns only one object:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HerbSpawner : MonoBehaviour
{
//Here, we declare variables.
public GameObject objToSpawn;
public Transform groupTransform;
//public means the var is exposed in the inspector, which is super helpful.
// Start is called before the first frame update
Vector2 spawnPos;
void Start()
{
spawnPos = Camera.main.ViewportToWorldPoint(new Vector2(0.5f, 0.5f));
}
// Update is called once per frame
void Update()
{
//let's also spawn on button press:
if (Input.GetMouseButtonDown(0))
{
RaycastHit2D hit = Physics2D.GetRayIntersection(Camera.main.ScreenPointToRay(Input.mousePosition));
if (hit.collider && hit.collider.CompareTag("Bush"))
{
SpawnIt();
}
}
void SpawnIt()
{
Vector2 spawnPos = Camera.main.ViewportToWorldPoint(new Vector2(0.5f, 0.7f));
Instantiate(objToSpawn, spawnPos, Quaternion.identity, groupTransform);
}
}
}
please let me know if there is anyway to do it spawn multiple objects randomly and make the movement for the items to popup like in the video. This is teh script i used for that:
using System.Collections;
using UnityEngine;
using TMPro;
public class DragNDropItem : MonoBehaviour
{
public string itemName;
private bool dragging;
private Vector2 firstPos;
private Color spriteRenderer;
[SerializeField] private float speed = 3;
[SerializeField] private GameObject boiler;
public TMP_Text itemNameText;
private BoilerScript boilerScript;
public AudioSource drop;
void Awake()
{
// Initial position of the item
firstPos = transform.position;
boiler = GameObject.FindGameObjectWithTag("Boiler");
boilerScript = boiler.GetComponent<BoilerScript>();
spriteRenderer = gameObject.GetComponent<SpriteRenderer>().color;
}
void OnMouseDown()
{
dragging = true;
}
void OnMouseUp()
{
dragging = false;
if (Vector2.Distance(transform.position, boiler.transform.position) < 0.7f) // We control the distance between the item and the cauldron without using the collider.
{
itemNameText.text = itemName;
boilerScript.Potion(); // Checks the recipe's completion status each time an item is placed.
spriteRenderer.a = 0f;
gameObject.GetComponent<SpriteRenderer>().color = spriteRenderer;
StartCoroutine(Alpha());
}
else drop.Play(); // If the item is left
}
void Update()
{
if (dragging) // As soon as the item is clicked with the mouse, the item follows the mouse.
{
Vector2 mousePosition = MousePos();
transform.position = Vector2.Lerp(transform.position, mousePosition, speed * Time.deltaTime);
} else if (!dragging && (Vector2)transform.position != firstPos) // As soon as we stop clicking, it takes it back to its original place. While the item is in its original location, we are constantly preventing the code from running.
{
transform.position = Vector2.Lerp(transform.position, firstPos, speed * Time.deltaTime);
}
}
Vector2 MousePos() // position of mouse
{
return Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
private IEnumerator Alpha() // After a second the item becomes visible.
{
spriteRenderer.a = 1f;
yield return new WaitForSeconds(0.6f);
gameObject.GetComponent<SpriteRenderer>().color = spriteRenderer;
}
}
Your colliders aren't set to trigger. My guess is, when a clone is spawned, it collides with the existing potion object.
Alright, so here's what's happening: When I hit play and left click to shoot, unity editor freezes and I have to do the old Ctrl + Alt + Del, now, I am almost certain this script is the source of the issue, because when a bullet is shot, this script is immediately added to it, so here's the script(It's called BulletLife.cs, just letting you know)
using System.Timers;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
public class BulletLife : MonoBehaviour
{
public GameObject bullet;
public double bulletLifeSpan = 3;
bool bulletLifeEnded;
public LayerMask targetMask;
bool hasHitTarget;
// Start is called before the first frame update
void Start()
{
var bulletAge = new System.Timers.Timer(bulletLifeSpan * 1000);
bulletAge.Elapsed += OnTimedEvent;
bulletAge.AutoReset = false;
while(hasHitTarget == false && bulletLifeEnded == false) {
hasHitTarget = Physics.CheckSphere(bullet.transform.position, bullet.transform.localScale.y, targetMask);
}
Destroy(bullet);
Debug.Log("Finish");
}
private void OnTimedEvent(System.Object Source, ElapsedEventArgs e) {
bulletLifeEnded = true;
}
}
Also, here's the Shoot.cs script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Shoot : MonoBehaviour
{
public Transform gun;
public GameObject bullet;
public LayerMask targetMask;
public float bulletSpeed = 1000f;
bool hasHitTarget = false;
// Update is called once per frame
void Update()
{
if(Input.GetButtonDown("LeftClick")) {
GameObject bulletInstance;
bulletInstance = Instantiate(bullet, gun.position, new Quaternion(gun.rotation.w, gun.rotation.x, gun.forward.y, gun.rotation.z));
bulletInstance.AddComponent<Rigidbody>();
bulletInstance.GetComponent<Rigidbody>().useGravity = false;
bulletInstance.GetComponent<Rigidbody>().AddForce(gun.up * bulletSpeed);
bulletInstance.AddComponent<BulletLife>();
bulletInstance.GetComponent<BulletLife>().bullet = bulletInstance;
}
}
}
NOTE: I am using Unity 2019.4.15f1
Well everytime you instantiate a bullet in Start you do
while(hasHitTarget == false && bulletLifeEnded == false)
{
hasHitTarget = Physics.CheckSphere(bullet.transform.position, bullet.transform.localScale.y, targetMask);
}
this loop will never finish since none of the conditions is changed inside the loop. There either is a hit or not .. but then the parameters for the raycast are never changed, the position isn't updated since you are still in the same frame => endless loop => freeze the main thread completely.
What you rather wanted to do is move that thing to Update which is called once a frame like e.g.
//public GameObject bullet; // not needed
public double bulletLifeSpan = 3;
//bool bulletLifeEnded; // not needed
public LayerMask targetMask;
//bool hasHitTarget; // not needed
void Start()
{
var bulletAge = new System.Timers.Timer(bulletLifeSpan * 1000);
bulletAge.Elapsed += OnTimedEvent;
bulletAge.AutoReset = false;
}
private void Update()
{
if(Physics.CheckSphere(transform.position, transform.localScale.y, targetMask))
{
Destroy(gameObject);
Debug.Log("Finish");
}
}
private void OnTimedEvent(System.Object Source, ElapsedEventArgs e)
{
Destroy(gameObject);
Debug.Log("Finish");
}
Or make it a single Coroutine
// If Start returns IEnumerator it is automatically started as Coroutine
// So no need to start an extra routine
private IEnumerator Start()
{
// Keeps track of how long your bullet exists already
var bulletAge = 0f;
while(bulletAge < bulletLifeSpan && !Physics.CheckSphere(transform.position, transform.localScale.y, targetMask))
{
// Increase by the time passed since last frame
bulletAge += Time.deltaTime;
// "Pause" this routine, render this frame
// and continue from here in the next frame
yield return null;
}
Destroy(gameObject);
Debug.Log("Finish");
}
Btw note that in Shoot you can shorten this a lot
void Update()
{
if(Input.GetButtonDown("LeftClick"))
{
// Note that your quaternion made no sense -> simply pass in the gun.rotation
var bulletInstance = Instantiate(bullet, gun.position, gun.rotation);
var rb = bulletInstance.AddComponent<Rigidbody>();
rb.useGravity = false;
rb.AddForce(gun.up * bulletSpeed);
var life = bulletInstance.AddComponent<BulletLife>();
// Assigning the gameObject reference is completely unnecessary
// within BulletLife simply use "gameObject" as show before
}
}
You could shorten this even more by making sure these components already exist on your prefab object and are configured correctly. Then you wouldn't need any of these line but just Instantiate it.
And finally you shouldn't use thisCheckSphere at all but rather let Unity handle its Collision detection itself and use OnCollisionEnter and configure your Collision Layers according to your needs!
The issue with your solution is: If your bullet moves fast it might simply pass a target without your CheckSphere noting it namely if its velocity is higher then localScale.y * 2.
Your Start method is blocking, thus freezing your game.
You'll have to use Update or a Coroutine to make your hit tests.
public class BulletLife : MonoBehaviour
{
public GameObject bullet;
public double bulletLifeSpan = 3;
bool bulletLifeEnded;
public LayerMask targetMask;
bool hasHitTarget;
// Start is called before the first frame update
void Start()
{
StartCoroutine(CheckHit(0, bulletLifeSpan));
}
private IEnumerator CheckHit(float interval, float lifetime){
bool checkEveryFrame = interval <= 0;
WaitForSeconds wait = checkEveryFrame ? null : new WaitForSeconds(interval);
while(lifetime > 0){
yield return wait;
lifetime = lifetime - (checkEveryFrame ? Time.deltaTime : interval);
hasHitTarget = Physics.CheckSphere(bullet.transform.position, bullet.transform.localScale.y, targetMask);
}
bulletLifeEnded = true;
Destroy(bullet);
Debug.Log("Finish");
}
}
Basically, what I'm trying to do is when pressing Z - it executes function to spin, and X - it executes function to stop spinning. Before, I had UI buttons which worked perfectly fine, now I try doing it by button but nothing happens.
Also, if you can suggest on how to make it start spinning and stop spinning by only pressing "Space" button, that'd be great.
Heres my code so far:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class spin : MonoBehaviour
{
public float speed = 500f;
public Button starter;
public Button stopper;
bool isSpinning = false;
IEnumerator spinnerCoroutine;
void Start()
{
//The spin function
spinnerCoroutine = spinCOR();
//Button btn = starter.GetComponent<Button>();
//Button butn = stopper.GetComponent<Button>();
//butn.onClick.AddListener(FidgetSpinnerStop);
//btn.onClick.AddListener(FidgetSpinnerStart);
if (Input.GetKey(KeyCode.Z)) {
FidgetSpinnerStart();
}
if (Input.GetKey(KeyCode.X)) {
FidgetSpinnerStop();
}
}
IEnumerator spinCOR()
{
//Spin forever untill FidgetSpinnerStop is called
while (true)
{
transform.Rotate(Vector3.up, speed * Time.deltaTime);
//Wait for the next frame
yield return null;
}
}
void FidgetSpinnerStart()
{
//Spin only if it is not spinning
if (!isSpinning)
{
isSpinning = true;
StartCoroutine(spinnerCoroutine);
}
}
void FidgetSpinnerStop()
{
//Stop Spinning only if it is already spinning
if (isSpinning)
{
StopCoroutine(spinnerCoroutine);
isSpinning = false;
}
}
}
Thanks.
There are just two problems in your code:
1.Checking the keypress in the Start() function.
The Start() will be called once while the Update() function will be called every frame.
You need to use the Update() function to constantly poll the input every frame.
2.Using Input.GetKey() function to check for keypress.
The Input.GetKey() function can return true multiple times over several frames. While you may not see any problems now, that's because the isSpinning variable is preventing possible problems but you will run into problems if you want to add more code directly inside the if (Input.GetKey(KeyCode.Z)) code because those code will execute multiple times in a frame.
You need to use the Input.GetKeyDown() function.
public class spin : MonoBehaviour
{
public float speed = 500f;
public Button starter;
public Button stopper;
bool isSpinning = false;
IEnumerator spinnerCoroutine;
void Start()
{
spinnerCoroutine = spinCOR();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Z)) {
FidgetSpinnerStart();
}
if (Input.GetKeyDown(KeyCode.X)) {
FidgetSpinnerStop();
}
}
IEnumerator spinCOR()
{
//Spin forever until FidgetSpinnerStop is called
while (true)
{
transform.Rotate(Vector3.up, speed * Time.deltaTime);
//Wait for the next frame
yield return null;
}
}
void FidgetSpinnerStart()
{
//Spin only if it is not spinning
if (!isSpinning)
{
isSpinning = true;
StartCoroutine(spinnerCoroutine);
}
}
void FidgetSpinnerStop()
{
//Stop Spinning only if it is already spinning
if (isSpinning)
{
StopCoroutine(spinnerCoroutine);
isSpinning = false;
}
}
}
Also, if you can suggest on how to make it start spinning and stop
spinning by only pressing "Space" button, that'd be great
You can do that with KeyCode.Space. Check if Space key is pressed then check the isSpinning variable before starting/stopping the coroutine.
Just replace the Update function above with the one below:
void Update()
{
//Start if Space-key is pressed AND is not Spinning
if (Input.GetKeyDown(KeyCode.Space) && !isSpinning)
{
FidgetSpinnerStart();
}
//Stop if Space-key is pressed AND is already Spinning
else if (Input.GetKeyDown(KeyCode.Space) && isSpinning)
{
FidgetSpinnerStop();
}
}
Your input logic is only executed once, when Start() is executed.
Put it in the Update() method to check for it every frame.
In this case remove the coroutine and put its logic (without the while-loop) into the Update() method aswell.
public class spin : MonoBehaviour
{
[SerializeField]
private float speed = 500f;
[SerializeField]
private Button starter;
[SerializeField]
private Button stopper;
[SerializeField]
bool isSpinning = false;
void Update()
{
if (Input.GetKeyDown(KeyCode.Z))
{
isSpinning = true ;
}
if (Input.GetKeyDown(KeyCode.X))
{
isSpinning = false ;
}
if( isSpinning )
{
transform.Rotate(Vector3.up, speed * Time.deltaTime)
}
}
}
Further reading
The part with touching the object and playing the animation is working fine now i want to add the walls script part.
In this case i change a cube height.
What i need to do is that only when the player touch an object it will raise/change the height of another object.
Son in the first case i find when the player is touching an object:
using UnityEngine;
using System.Collections;
using System.Reflection;
public class DetectPlayer : MonoBehaviour {
GameObject target;
int counter = 0;
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name == "ThirdPersonController") // "Platform"
{
Debug.Log("Touching Platform");
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.name == "ThirdPersonController") // "OnTop Detector"
{
counter = 0;
Debug.Log("On Top of Platform");
target = GameObject.Find("Elevator");
GameObject findGo = GameObject.Find("ThirdPersonController");
GameObject findGo1 = GameObject.Find("Elevator");
findGo.transform.parent = findGo1.transform;
GameObject go = GameObject.Find("CubeToRaise");
go.GetComponent<RaiseWalls>();
Debug.Log("The button clicked, raising the wall");
StartCoroutine(playAnim(target));
}
}
void OnTriggerExit(Collider other)
{
GameObject findGo = GameObject.Find("ThirdPersonController");
findGo.transform.parent = null;
}
IEnumerator playAnim(GameObject target)
{
Animation anim = target.GetComponent<Animation>();
foreach (AnimationState clip in anim)
{
// do initialisation or something on clip
clip.speed = 1;
}
while (true)
{
if (counter == 1)
break;
anim.Play("Up");
while (anim.IsPlaying("Up"))
{
yield return null;
}
anim.Play("Down");
while (anim.IsPlaying("Down"))
{
yield return null;
}
yield return null;
counter++;
}
}
void OnGUI()
{
GUI.Box(new Rect(300, 300, 200, 20),
"Times lift moved up and down " + counter);
}
}
At this part i'm calling the second script RaiseWalls:
using UnityEngine;
using System.Collections;
public class RaiseWalls : MonoBehaviour
{
public GameObject gameObjectToRaise;
public float speed;
// Use this for initialization
void Start()
{
speed = 2;
}
void Update()
{
gameObjectToRaise.transform.localScale += new Vector3(0, 50, 0);
}
}
GameObject go = GameObject.Find("CubeToRaise");
go.GetComponent<RaiseWalls>();
Debug.Log("The button clicked, raising the wall");
Now the DetectPlayer is attached on one game object.
The RaiseWalls script is attached on another game object.
On the RaiseWalls script i want to set the speed of the object height change. Now it's changing the height by 50 but many times. I want it to change it by 50 but in slow motion like a slowly building/raising wall. Like electronic fence that raise from bottom to top effect.
Second problem i want that first it will raise the wall or walls once it finished raising the walls move to the next part in the DetectPlayer script:
StartCoroutine(playAnim(target));
Steps:
When the player is touching the object in DetectPlayer script raise the wall/s in RaiseWalls script in specific speed.
When the walls raised only then make the StartCoroutine.
So, you want to raise the wall 50 in total, but with a controllable speed.
First make a counter in RaiseWalls:
float raiseAmount;
Also record the total to raise it, which makes it easier to change later:
float raiseTotal = 50;
Then, in your Update, raise it by a little, but record how much was raised
if(raiseAmount < raiseTotal ) // i.e. we haven't raised it fully
{
// work out how much to raise it
float raiseThisFrame = speed * Time.DeltaTime; // to account for frame rate
// now we cap it to make sure it doesn't go over 50
if(raiseAmount + raiseThisFrame > raiseTotal )
{
raiseThisFrame = raiseTotal - raiseAmount;
}
// add raiseThisFrame to raiseAmount
raiseAmount += raiseThisFrame;
gameObjectToRaise.transform.localScale += new Vector3(0, raiseThisFrame, 0);
}