How to add delay to shooting on keypress - c#

I'm making a space shooter and I'm wondering how to make a delay with the shooting instead of spamming the space bar thanks :)
public void Shoot()
{
Bullets newBullet = new Bullets(Content.Load<Texture2D>("PlayerBullet"));
newBullet.velocity = new Vector2((float)Math.Cos(rotation), (float)Math.Sin(rotation)) * 5f;
newBullet.position = playerPosition + playerVelocity + newBullet.velocity * 5;
newBullet.isVisible = true;
if (bullets.Count() >= 0)
bullets.Add(newBullet);
}

I think you want to use XNA's GameTime property. Whenever you shoot a bullet you should record the time it happened as the current GameTime.ElapsedGameTime value.
This is a TimeSpan so you could compare it like the following:
// Create a readonly variable which specifies how often the user can shoot. Could be moved to a game settings area
private static readonly TimeSpan ShootInterval = TimeSpan.FromSeconds(1);
// Keep track of when the user last shot. Or null if they have never shot in the current session.
private TimeSpan? lastBulletShot;
protected override void Update(GameTime gameTime)
{
if (???) // input logic for detecting the spacebar goes here
{
// if we got here it means the user is trying to shoot
if (lastBulletShot == null || gameTime.ElapsedGameTime - (TimeSpan)lastBulletShot >= ShootInterval)
{
// Allow the user to shoot because he either has not shot before or it's been 1 second since the last shot.
Shoot();
}
}
}

You can have a counter that counts the number of frames since the last shot.
int shootCounter = 0;
...
if(shootCounter > 15)
{
Shoot();
shootcounter = 0;
}
else
{
shootcounter++;
}
If you want to go more advanced, you can use the gameTime that the default update method normally gives you to keep track of how much times has passed since you've last shot.

Related

Adjust the length of animation clip to match cooldown

I'm planning on have several buttons to spawn monsters, all with a different cooldown. Is there a way to match the animation clip to be the same length as the cooldown of the button?
Here is an example: https://gyazo.com/0a2ae868e5458c701e1a258aac6dc59a
The animation is 1 second but the cooldown is 3 seconds.
Here is my code:
private void ButtonCooldown()
{
if (GetComponent<Button>().interactable == false)
{
buttonTimer += Time.deltaTime;
if (buttonTimer >= cooldown)
{
GetComponent<Button>().interactable = true;
buttonTimer = 0;
}
}
}
public void DisableButton()
{
GetComponent<Button>().interactable = false;
myAnimatior.SetTrigger("ButtonCooldownAnimation");
}
You could adjust the according Animator's speed to adjust it's overall playback speed.
E.g. something like
// Adjust in the inspector
[SerializeField] private float cooldownTime = 3;
// Already reference this via the Inspector if possible
[SerializeField] private Button button;
private void Awake ()
{
// Do this only once!
if(!button) button = GetComponemt<Button>();
}
public void DisableButton()
{
button.interactable = false;
// typo in Animator btw ;)
myAnimatior.SetTrigger("ButtonCooldownAnimation");
// Make the Animator play slower so the animation now takes 3 seconds
myAnimatior.speed = 1/cooldownTime;
// Instead of Update simply use Invoke here
// Execute the method called WhenCooldownDone after cooldownTime seconds
Invoke(nameof(WhenCooldownDone), cooldownTime);
}
private void WhenCooldownDone ()
{
button.interactable = true;
myAnimator.speed = 1;
}
As in the comments I would use Invoke instead of constantly checking the states in Update. In particular never use GetComponentrepeatedly in Update. It is very expensive. Always try to rather store the reference and reuse it.

Counting a full rotation with a circular drive

I've been raking my brain for the last day on how to calculate a full rotation with how to count a full rotation of an object along the X axis thats using the base circular drive from SteamVR.
I thought a simple 3d cube, with the mesh turned off in the the path of the rotation with collision code on it would be a barebone way of doing it, but it doesn't even seem to be registering the detection when the object hits the placed cubes, and i know its not because of me being stupid, as its recycled code from a working part of the project.
Below i have a small piece of code that basically detects when the object has reached the end of the rotation, and then increments the Count by one.
My main problem is that sometimes it manages to clock more than once, and if you can find the right spot, you can just keep it there and it'll keep on adding the count up by. Im wondering how i can stop it and increment only by one, until another full rotation has been made?
EDIT: to be more clear in case there is any confusion, Once the angle is clocked in between 359 and 360, i want it to increment once, whereas currently if you get the angle to sit anywhere in between 359-360 it will carry on adding one to the rotation count, despite no full rotation having been made, so im trying to figure out how to make my code only increment once, and once it does increment once it resets the position to zero, so therefore no more Increments can happen. It's a crank mechanism in VR, along the X axis.
Any help is appreciated, Thanks!
float Test;
float RotationCount = 0;
// Start is called before the first frame update
void Start()
{
// Test = transform.localRotation.eulerAngles.x;
}
// Update is called once per frame
void Update()
{
if (Test > 359 && Test < 360)
{
Debug.Log("Clocked");
count();
}
else
{
// Debug.Log("Nope");
}
if (Test == 0)
{
Debug.Log("Yes");
}
Test = transform.localRotation.eulerAngles.x;
}
void count()
{
RotationCount++;
}
sometimes it manages to clock more than once, and if you can find the right spot, you can just keep it there and it'll keep on adding the count up by.
well in
if (Test > 359 && Test < 360)
{
Debug.Log("Clocked");
count();
}
what happens if your angle is e.g. 359.5?
It is very difficult to just take a current rottaion and know whether it was turned more or less than a certain angle.
I'ld rather store the last rotation, compare it to the current one and add the difference to a variable. Than if the variable exceeds 360 a full rotation was done. Since I also don't like to calculate anything with Quaternion and eulerAngles, way simplier is to use the methods provided by Vector3.
For the local rotation around X (== transform.right) I would use the angle between the current and the last transfrm.up vector.
Something like
public class RotationCheck : MonoBehaviour
{
public int RotationCount;
public float rotatedAroundX;
public Vector3 lastUp;
public UnityEvent On3TimesRotated;
private void Awake()
{
rotatedAroundX = 0;
// initialize
lastUp = transform.up;
}
private void Update()
{
var rotationDifference = Vector3.SignedAngle(transform.up, lastUp, transform.right);
rotatedAroundX += rotationDifference;
if (rotatedAroundX >= 360.0f)
{
Debug.Log("One positive rotation done", this);
RotationCount++;
rotatedAroundX -= 360.0f;
}
else if (rotatedAroundX <= -360.0f)
{
Debug.Log("One negative rotation done", this);
RotationCount--;
rotatedAroundX += 360.0f;
}
// update last rotation
lastUp = transform.up;
// check for fire the event
if (RotationCount >= 3)
{
On3TimesRotated?.Invoke();
RotationCount = 0;
}
}
}
You can use a UnityEvent to get the same thing the Button uses for onClick so you can reference callbacks there via the inspector.
BUT if you don't care about the single rotations but actually only wnat the final RotationCount >= 3 I would actually use
private void Update()
{
var rotationDifference = Vector3.SignedAngle(transform.up, lastUp, transform.right);
rotatedAroundX += rotationDifference;
RotationCount = Mathf.RoundToInt(rotatedAroundX / 360.0f);
// update last rotation
lastUp = transform.up;
// check for fire the event
if (RotationCount >= 3)
{
On3TimesRotated?.Invoke();
RotationCount = 0;
rotatedAroundX = 0;
}
}
which directly reduces the value by one if rotated under the 360 mark instead of waiting for a full negative rotation
*as you can see instead of reaching 3 the RotationCount is reset to 0. This is where the On3TimesRotated event is/would get fired.

(Unity3D) in Block Breaker game I'm trying to load levels when all bricks destroyed

Good Morning Developers!
so here is what i'm trying to do: i created a block breaker game, and i wrote some code so that when all bricks in the scene are destroyed the next level is loaded.
It works fine, but there is a bug! when i lose before destroying all the bricks, and then i press "play again", the static variable who is responsible of counting bricks on scene does not reset to 0! it keeps the number of brick before i lost and add to it the number of bricks in the new scene!, so instead of returning 24 for ex (which is the correct number of bricks in scene) it returns 35 (11 + 24)
how can i fix that please?
here is the code i'm using: first the brick script :
public int maxHits;
public int timesHit;
public Sprite[] hitSprites;
public static int breakableCount = 0;
private bool isBreakable;
private LevelManager levelManager;
// Use this for initialization
void Start () {
isBreakable = (this.tag == "Breakable");
if(isBreakable){
breakableCount++;
}
print (breakableCount);
timesHit = 0;
levelManager = GameObject.FindObjectOfType<LevelManager> ();
}
void OnCollisionEnter2D(Collision2D collision) {
if (isBreakable) {
HandleHits ();
}
}
void HandleHits(){
//TODO remove the print!!
print ("collison");
timesHit++;
if (timesHit >= maxHits) {
breakableCount--;
print (breakableCount);
levelManager.BrickDestroyed ();
Destroy (gameObject);
} else {
LoadSprite ();
}
}
// Update is called once per frame
void Update () {
}
//TODO Remove this when player can WIN
void NextLevel(){
levelManager.LoadNextLevel ();
}
void LoadSprite(){
int spriteIndex = timesHit - 1;
this.GetComponent<SpriteRenderer> ().sprite = hitSprites [spriteIndex];
}
and here is the LevelManager script I'm using to manage levels :
public void LoadLevel (string name) {
Debug.Log ("level change requested for : " + name);
Application.LoadLevel (name);
}
public void ExitRequest() {
Debug.Log ("Exit game requested");
Application.Quit ();
}
public void LoadNextLevel () {
Application.LoadLevel (Application.loadedLevel + 1);
}
public void BrickDestroyed () {
if(Brick.breakableCount <= 0) {
LoadNextLevel ();
}
}
hope i explained correctly, and sorry if i made some English errors i'm not native speaker lol, Thank you have a nice day ^^
-Edited due to misunderstanding-
I didn't realize that was your BRICK script. The reset should be inside our LevelManager.
Your first line in your function to load a new level in LevelManager should be:
breakableCount = 0;
This will make it so that when the level is initialized that the counter is reset.
Also, you could reset the same way as soon as you've decided that a person has beat the current level.
Also, I recognize this from Ben Tristram's Unity Dev Course. You should try using the tools built into his class for questions, there is a lot of support there for these specific exercises!
Stack Overflow is great though, and it's a great source for when that stuff falls through. Another place to check is https://gamedev.stackexchange.com/
private static int breakableCount = 0;
public static int BreakableCount
{
get{ return breakableCount; }
set{
breakableCount = value;
if(breakableCount <= 0){ EndOfLevel() }
}
}
Turning your variable into property (or you can use a method if you prefer), you can now add some logic when it gets modified.
EndOfLevel is just a method you call to load the next level, save some data and reset some static values before leaving.
I Want to update this post because i find a solution and i have another question in the same subject!
First i'll tell you how i fixed it:
as #JorgeSantos suggested i created a ResetGame fonction in my loadlevel script :
void ResetGame(){
Brick.breakableCount = 0;
print ("breakableCount set to 0 ");
}
then i called that fontion in my LoadLevel fonction :
public void LoadLevel (string name) {
ResetGame ();
Debug.Log ("level change requested for : " + name);
Application.LoadLevel (name);
now the variable is resetting just fine
the only problem (it's not really a problem because the game runs fine, it's just that i want to know why it's happening) is that for ex let's say that i run the game, destroy 4 bricks, and then i lose, (keep in mind that there are 24 bricks in the scene) so i left 20 bricks non destroyed!
when i press play again, in the console, i notice that when i destroy a brick the breakableCount variable is not taking new values, then when i destroy 4 brick, (which means i'm in the same number of bricks left as i were before losing), then the breakableCount variable takes the value 20 (which is the right value) and continue decreasing when i destroy bricks normally!, You can see now the the game continue to work fine, but i dont understand why that variable is not reset to the right number of brick after i click on play again, and only takes effect when i reach the same number of destroyed brick as in my fist try?!
hope i made my point clear looking forward for your answers, and thank you all ^^

Unity3D: Adding charged force in relation to position

I am trying to make a game which "kind of" simulates the shooting of the "worms" game. The player can choose the position (circular) of an object and then, the force that is applied to the object should move in the direction its pointing towards. I tried using the AddForce(transform.right) code, but it would just go to the right. (2D BoxCollider and RigidBody2D)
Then comes the hard part, making the player choose the force by charging the power. When the player holds down the "f" key, I want the power to go up to a certain point. Once it reaches that point, I want it to go down again and then up again, so the player can choose the power he wants. I have no idea how to go about this.
It's been awhile since I've did Unity coding, so there may be some minor errors with my syntax but it should give you an idea of how to accomplish this. Your best bet for the loop is to use a coroutine to not block the main thread.
in Update() check for 'on key down' for F and start this coroutine:
IEnumerator Cycle()
{
float max = 10.0f;
float min = 1.0f;
float interval = 0.5f;
do
{
for(int i=min;i<max;i++)
{
PowerValue = i;
yield return new waitforseconds(interval);
if(!input.getkey(f))
break;
}
for(int i=max;i>min;i--)
{
PowerValue = i;
yield return new waitforseconds(interval);
if(!input.getkey(f))
break;
}
} while(input.getkey(f));
}
And back in update() use that powerValue with getKeyUp(f)
And here is PowerValue setup as a parameter that prevents code from setting the max and min outside of a 1 to 10 range (configurable)
private float powerValue = 1.0f;
public float PowerValue
{
get { return powerValue; }
set {
if(value>10f)
powerValue=10f;
else if (value<1f)
powerValue=1f;
else
powerValue=value;
}
}

XNA - Play Animation at place when collide and delete when reach last frame

In my pong game I'm making I have two paddles and a ball. I'm trying to make so when the ball collides with the paddle, an effect/animation is shown. I have a spritehseet and a working animation. I use this to detect when the animation plays (please notice that I havent fixed the position of the effect yet, so it plays on 400, 300 just to see if it works)
public bool BallHitEffect()
{
if (gPaddle.gpRect.Intersects(ball.ballRect))
{
return true;
}
else { return false; }
And in my Draw:
protected override void Draw(GameTime gameTime)
{
spriteBatch.Begin();
if (BallHitEffect())
{
animatedSprite.Draw(spriteBatch, new Vector2(400, 250));
}
}
Now it appears when it collide, but only for a short millisecond, because when the ball leaves the paddle, it disappears. I'm aware that it's coded to only appear when it is colliding with the paddle, but in what way could I possibly make it only disappear only when it's animation ends?
You can easily used elapsed time and some makeshift timers to do this. I assume your spritesheet is a series of frames, and that you already have rendering set up.
You will need some variables to start out:
double frameTimePlayed; //Amount of time (out of animationTime) that the animation has been playing for
bool IsPlaying; //Self-Explanitory
int frameCount; //Frames in your sprite, I'm sure you already handle this in your animation logic, but I just threw it in here
int animationTime = 1000; //For 1 second of animation.
In your update method you need to
Check if the ball is intersecting, and set IsPlaying to true
If the animation IsPlaying, increment the frameTimePlayed by the elapsed time
If the frameTimePlayed is equal to or greater than the animationTime, stop the animation by setting IsPlaying to false
In your draw method you need to
Draw the animation, if IsPlaying
Here is some example code:
protected override void Update(GameTime gameTime)
{
//If it is not already playing and there is collision, start playing
if (!IsPlaying && BallHitEffect)
IsPlaying = true;
//Increment the frameTimePlayed by the time (in milliseconds) since the last frame
if (IsPlaying)
frameTimePlayed += gameTime.ElapsedGameTime.TotalMilliseconds;
//If playing and we have not exceeded the time limit
if (IsPlaying && frameTimePlayed < animationTime)
{
// TODO: Add update logic here, such as animation.Update()
// And increment your frames (Using division to figure out how many frames per second)
}
//If exceeded time, reset variables and stop playing
else if (IsPlaying && frameTimePlayed >= animationTime)
{
frameTimePlayed = 0;
IsPlaying = false;
// TODO: Possibly custom animation.Stop(), depending on your animation class
}
}
And for drawing, pretty easy, just check is IsPlaying and draw if that is the case.
I made sure to comment the code good, so hopefully this will help you understand and work better.
You could also use a double and use TotalSeconds instead of milliseconds if needed, and calculate elapsed time with float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;

Categories

Resources