How to make my list of enemies to shot c# - c#

I've been working on some game project for college work. My problem is that I have list of enemies that is moving left-right and when touching left or right edge goes down a bit, and what i want them to do is to shot bullets from random enemy while game lasts,but I have managed for bullet to show and shot from random enemy but that is it :'D, it just goes that one time and it needs to go while game lasts. So here is my method for shooting to me it looks all right and cant figure out why is just one shot.
I'm beginner so sorry if my code looks stupid.
private int MoveEnemyBullet()
{
int randomnum = r.Next(30);
shooter = enemies[randomnum];
bullet.Goto_Sprite(shooter);
bulletEnemy.Add(bullet);
bullet.SetVisible(true);
bullet.Active = true;
while (START)
{
if(bulletEnemy.Count!=0)
bullet.Y += bullet.speed;
Wait(0.01);
if (bullet.TouchingEdge())
{
bullet.SetVisible(false);
}
}
Game.StartScript(MoveEnemyBullet);
return 0;
}

Your code is hard to read since there is only a little of it. What is bullet, bulletEnemy or START? I would ask why you don't have a list of bullets also, but i think that you want to only have one bullet at a time on the screen.
There might be a problem with TouchingEdge() method. Something similar happened to me, the bullet was not destroyed when it whent out of the bounds of the screen, only when it hit its target, so the bullet was still "alive" but outside the screen, still moving. If this happened to you, the bullet was never set to invisible.
Also, it does not seem to me that you change the value of START, which would mean that your while loop never ends. If you change it to false in the SetVisible(false) method, then this is not the problem.
I believe Goto_Sprite(shooter) changes the position of the bullet to the new shooter, but you should check if that one is correct too.

Related

Networked AR Unity can't make a bullet a child of the ground plane stage

I am trying to make an AR game and am currently using Unity's high-level networking classes. I have set my player prefab to be spawned in one of the two network spawn locations, which are both childs of the Ground Plane Stage. When user has tapped to make their ground plane stage and has tapped to be the host, their player character appears. Unfortunately, if they press the fire button, the bullets appear above the stage and unscaled, meaning they aren't parented to the stage. This confuses me because I've checked many times and the bullet emitter is a child of the player, and in my code it references said emitter. Thus I'm rather confused why the bullets don't seem parented.
I've attempted to attach a script to make the bullet emitter a child of the player when it spawns. I've also tried making it a child of the stage when it spawns. I've tried making the player character not dependent on the Network manager spawning it when the player joins, but then that leads to other networking problems when it comes to controlling the character, but it can shoot then.
The only one that was relatively successful was making the bullet a child of the stage when it spawned, but it would only stay in one place. Attempting to make the bullet a child of the player did nothing
//This is the class I'm trying to use to make the object a child of
something
public class AddToBeetle : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
GameObject player =
GameObject.FindGameObjectWithTag("Player");
transform.SetParent(beetle.transform, false);
}
}
It rarely prints any error messages. I hope that I can eventually get the bullet to spawn in front of the player model when the button is pressed.
Oh gosh I was dumb, the one thing I didn't apply the script to was the player's bullet emitter. It was super late and that's probably why I missed trying it on that. Anyway I hope this helps someone else if they ever have to deal with AR and Unity.

How to allow movement while in animation (Unity)

I have a game I am making where I want that the player has no animations until it hits a certain trigger. Then I want a short animation to be played, then it will stop the animation again.
To get this effect I have the above animator state machine attached to my player, and when the player collides with the trigger it plays the Halfway animation, then it goes back to the empty state. All of this seems to work fine at first but while my player is in this empty state loop thing, I cannot move it. I am using "rb.velocity = new Vector3(stuff);" to move my player, and when I disable the animator component I can move fine. I was wondering if
There is any way to enable movement while I am in this empty state,
There is some way to get rid of the entry transition entirely so I don't have to have the empty state at all, or
Pretty much anything else that would make this work :P
If you need more information let me know, I will try and help you help me as much as I can. Thanks!

Somehow physically colliding with Collision Triggers

With a whole bunch of copy-pasting from all over the web, I managed to hack together a piece of C# code which spawns a random object from an array when the player enters a large, spherical collision trigger. When the player leaves the trigger, the object is deleted. It's working almost as intended, only problem is that six out of ten times when the player passes through the collider, for a split second it feels like a physical collider. Meaning that although the player is trying to move straight forward, he or she will move left or right along the trigger collider for about half a second, before the object spawns and the player can move freely again.
I was under the impression that checking the "Is Trigger" box would remove all physical properties of that specific collider? Am I missing something?
I'm using the standard FPS Character Controller in Unity 5 if that might have something to do with it.
All help is greatly appreciated.
Make sure the spherical collision trigger has a rigid body component, and that the "Is Kinematic" property of the rigid body is checked.
Realized my solution didn't get posted as I thought it had. Yoyo's solution was correct. The object containing the code was also the trigger. Switched so that the player was the trigger and now all is well.

XNA How to make an object invisible

I'm making a 2D Shooter game in XNA. I had been working on the shooting speed of the player (Every how often can the player shoot another bullet) and made it so that the player can only shoot again once the previous bullet has traveled a certain distance like so:
if (this.bulletList[0].BULLETS[(this.bulletList[0].BULLETS.Count) - 1].X >= Pos.X + attackSpeed)
canShoot = true;
Where bulletList is the available Projectiles the player can shoot, BULLETS is the List of Bullets already shot, and attackSpeed is the rate in which the bullets should be shot, or simply put: the distance the bullet has to travel until another bullet can be shot.
Now I've been working on Collisions. My method was to dispose of a bullet after it hits a target like so:
for (int i = 0; i < player.BULLETLIST[0].BULLETS.Count; i++)
{
if (CollisionManager.PlayerBulletOnBot(player.BULLETLIST[0].BULLETS[i], bot))
player.BULLETLIST[0].BULLETS.RemoveAt(i);
}
Problem is, if the bullet has been removed upon hitting a target, I can no longer ask if that bullet has passed the given distance for another bullet to be shot.
To solve that, I'd like the bullet to turn invisible upon hit, and afterwards it'll be disposed of in another function that's already been made.
Simply have a Visible flag for a Bullet instance:
class Bullet {
public bool Visible { get; set; }
}
When it hits.. make it invisible:
// ... hit
bulletInstace.Visible = false;
Then check before drawing it:
if (bulletInstance.Visible)
drawBullet(bullet);
If it isn't visible, your drawing code should just skip over it.
When you draw an object using spriteBatch.Draw(...) you have to select a color for the sprite itself.
We all know that the color is a mixture of "red, green and blue" values (or rgb). What not many people know is that, at least in XNA, there is a fourth value called alpha.
The alpha value indicates the transparency of your object, meaning that if you use it like the code below, your object will be half invisible (or half visible).
spriteBatch.Draw(..., Color.White * 0,5f,...);
You can play with that :)
Check a little more here, on the old XNA forums.
You need rate of fire (rof) property that you increase to some maxRof value when you hold down button. and when rof is equal to maxrof then you add bullet to a list of bullets (and reset rof to 0).
Do not make it invisible, remove it from list. Every instance of bullet should have "Active" property set to true on fire. (when you fire a bullet, add it to list) And when collsition happened set this property to false. pseudo code as example:
UPDATE
for each bullet in bullets
-- update bullet position
-- check collision if happened if yes then set Active to false
end for
bullets.removeall(function(c) NOT(c.active));
DRAW
for each bullet in bullets.findall(function(c) c.active)
-- draw your bullets
end for

Ball gets stuck when colliding with Rectangle

A new problem i have at the moment is sometimes when the ball is bouncing around the screen, it may all of a sudden get stuck colliding with a rectangle, so it may hit a spot between the rectangle and the side of the screen. This sometimes results in either: a) the ball looks like it's shaking or something, which i am assuming as it's still colliding with the rectangle, it's velocity (as i have defined it) is constantly inversing... but it's still stuck... (that's what i cant solve... lol :( ), or b) when the ball collides with the rectangle, it starts to move along the edge of the rectangle and goes back and forth but it continues to do this... (so it's basically stuck moving just along the edge).
I have uploaded a video to youtube showing this in action (btw... the ball gets stuck and goes up and down the right side of the screen and then starts going along the top edge of the white rectangle and then eventually bounces off into oblivion... well basically frees itself, which is kind of ok but then if you wait 5 more seconds, it repeats this process)
http://www.youtube.com/watch?v=3qfpgtoWbIU&feature=youtu.be
the code i am using for collision detection with the rectangle is:
if (theBall.GetRectangle.Bottom >= cornerSquare.GetRectangle.Top && theBall.GetRectangle.Bottom <= cornerSquare.GetRectangle.Bottom)
{
theBall.pVelocity.Y = -theBall.pVelocity.Y;
}
thats for detection if the ball hit's the top of the rectangle.
detection with the right side of the screen is:
if (pPosition.X + pTexture.Width >= screenWidth)
{
pVelocity.X = -pVelocity.X;
}
I hope someone has a simple and effective solution to this as i just want to 'be done' with all this collision detection nonsense lol... as it's taking up most of my time whilst i could be alot more productive doing other parts of the game.
Thanks for reading, taking the time to read, taking the time to help.... ETC.... :D
Try to check for collision first using a calculated test-rectangle.
For example the ball moves for Vector2(1, 1) per Update. Find out where the Rectangle for collision WOULD be at the next step BEFORE moving.
int nextPosX = currRect.X + (int)movementVector.X;
int nextPosY = currRect.Y + (int)movementVector.Y;
// find out where the rectangle would be after this update:
Rectangle nextStepsCollisionRect = new Rectangle(nextPosX, nextPosY, width, height);
// check if the test rectangle would collide:
if( collisionTest(nextStepsCollisionRect, sideWall) )
{
// do not move the intended way!
// invert X and recalc with pre-calculated rectangle again...
} else {
// no collision. now move the caculated way:
ball.CollisionRectangle = nextStepsCollisionRect;
}
This is a quite simple way. Use "ray tracing" if the ball is a very fast bullet (just to be mentioned).
If your ball is stuck along the edge you can simply bring the ball position to its previuos position before the collision (the one it had in the previous frame), in this way you change its velocity and ensure that the ball is not colliding anymore, this should do the trick.
You can achieve this by subtracting ball speed to ball position.

Categories