I'm making a side-scrolling Mega Man based game in C#. I am NOT using the XNA frame work to do so. I'm looking at creating multiple "bullets" from one location using one single image within my game class. The only thing I can think of at this point is something similar to this:
if (shooting == true)
{
BulletLocation.X += 3.0F;
Bullet = Properties.Resources.Bullet;
Charecter = Properties.Resources.shooting;
}
Shooting is set true on the keyDown event, and set to false on the keyUp event. I'm positive i would need an array of the sorts, but I'm not sure exactly how I should go about it. Thanks for your help!
EDIT:
What portion of that code would actually allow you to generate multiple "bullets" from one sprite single sprite? When the user presses the space bar, I would like to create a bullet that moves forward until it reaches the end of the screen. I can do that part easily. I cannot, however, do it with multiple bullets. I can only have one bullet alive at a time. I'm not sure how I would go about creating multiple bullets on the forum from one single image.
If I understand it correctly, you want to show multiple bullets, right?
I would make a variable for # of bullets and bulletposition.
So lets say:
const DISTBETWEENBULLETS = 3.0;
int distToOpponent = 9.0:
int curBulletDist = 0;
do{
curBulletDist += DISTBETWEENBULLETS;
//Draw bullet
}while(distToOpponent <= curBulletDist);
I hope this helps, and if it doesn't answer your question, or you mean something else, feel free to ask.
Related
I am working on a game in unity, a type of sliding number puzzle where you have to line up each piece in ascending order. My code of checking collisions is only working when I start the game, so if I move a piece, it won't tell the near pieces that I moved the near piece. I hope you understood my problem, every help is appreciated. Thank you.
This is how my code looks with the up check. I
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.name == "Piece" || collision.gameObject.name == "Up_Collider")
{
piece_Script.Collide_Up = true;
Debug.LogError("Up");
}
else
{
piece_Script.Collide_Up = false;
}
}
I Thought that the else statement will solve this problem but it didn't.
Make sure all the things I write below are correct in your project:
1- You are using "2D" colliders on both objects.
2- At least 1 of the objects has a "Rigidbody2D".
3- IsTrigger Checkbox is active in your collider.
4- You are using the correct Tag.
5- Check layer collision matrix in - Edit > Project Setting > Physics2D.
And it is better to use tag instead of name
Based on your code in your question and your video, I see at least a couple issues.
First, there's an issue with how you're moving pieces. Your movement code for a Piece is:
Piece.transform.position = Vector3.Lerp(a, b, t);
This is not correct if you want to trigger collider events. These require interactions with rigidbodies, which means you should instead be changing the position of the piece's rigidbody with Rigidbody2D.MovePosition(). This will move your rigidbody within the physics system, which means taking into account collisions/triggers.
So (assuming you get the rigidbody for each Piece when you initialize them), your code might look like:
// Note also that I used Vector2 here - it's best you keep it consistent
pieceRigidbody.MovePosition(Vector2.Lerp(a, b, t));
Second, OnTriggerEnter2D() is not fired when two colliders stop touching. So I don't see your "else" condition being particularly useful. To achieve that with your current code, you could theoretically introduce OnTriggerExit2D() to take care of the "else" condition. However, you run into complications because while moving, a Piece may Enter and Exit the same directional colliders of two other Pieces before coming to rest. You'd have to take into account the order that occurs to get an accurate final state.
While workable, I have an alternative approach to suggest: Abandon using Trigger Collider events, and only check for a valid move at the time a Piece is clicked. Eliminate the collider event handlers, and just execute a Physics2D.OverlapBox() in each position around a Piece when it is clicked. If the position is occupied by another piece or an edge, that's not a valid move. If there is a position without something blocking it, then move the piece there.
Hello there :) I'm making a 2d platform game with Unity and got stuck with the crouch animation... I have quite a few frames for crouching, so, when player presses appropriate button character sits down and when the button is released I want this animation to be played backward, so, character stands up. Currently in the animator I created two states with the crouching animation assigned to them. Crouch state speed is 1 and so called un-crouch one is -1 which sort of works fine. My inner perfectionist's question: is there more elegant solution for this kind of cases that allows not to "duplicate" states?
Thank you in advance!
Something you can try is to add in your script a parameter to multiply the speed of the animation. You can call this parameters animDirection or something like this.
I am not sure if you want to speed up the animation as well, but just in case you can do something like this:
float animSpeed = 1;
float animDirection = 1;
Now you can manipulate in your script this variables, to make the animation move faster and also forward (animDirection = 1) or backward (animDirection = -1)
gameObject.animation["crouch"].speed = animSpeed * animDirection;
So with this you don't need to have two different states.
So i am creating a movement script for my character, and i want him to move 1 square every a button is pressed, the problem i am having is that, if i hold down the button, he will constantly move which i don't want, for example if it was held down i still only want him to move 1 square. I have already tried using
if (Input.GetKeyDown(KeyCode.W) && WButton == true)
{
transform.Translate(0, 0, 1);
WButton = false;
}
if (Input.GetKeyUp(KeyCode.W))
{
WButton = true;
}
what this does is, when the W button is pressed down it disables it, and when you lift your finger up it then enables it. This does what i want but it doesn't work very well at all, like definitely not good enough to be in a game, so is there any other ways that i am able to achieve this.
Thanks for all the help,
it is well appreciated
By your given example, I assume you are using Unity3D, right? (if so, please consider adding the unity3d tag to your question, in order for people to have a better context of your problem and find your question in the appropriate sections of StackOverflow).
Your code example seems right. The problem that might be happening is that you can only read input values from the Update() method, as stated in the Input class documentation.
If you are calling Input.GetKeyDown() and/or Input.GetKeyUp() from any place other than inside an Update() method, then these methods will return inconsistent/erroneous values.
Input.GetKeyDown called only first time that you press the button; So you don't need to use bool WButton.
Refer to Unity3D documentation - Input.GetKeyDown, "You need to call this function from the Update function, since the state gets reset each frame. It will not return true until the user has released the key and pressed it again."
Use this code:
void Update()
{
if (Input.GetKeyDown(KeyCode.W))
transform.Translate(0, 0, 1);
}
I hope it helps you.
I am wanting to make a birds-eye view pixel-art game.
I currently have two sprite sheets set up, and split and whatnot
groundSheet and characterSheet these are split up into
ground_0_0_0 (A concrete floor)
ground_1_0_0 (grass)
character_0_0_0 (man idle animation frame 1)
character_0_0_1 (man idle animation frame 2)
character_0_1_0 (man run animation frame 1)
character_0_1_1 (man run animation frame 2)
character_1_0_0 (woman idle animation frame 1)
character_1_0_1 (woman idle animation frame 2)
character_1_1_0 (woman run animation frame 1)
character_1_1_1 (woman run animation frame 2)
The numbers after are a note as to:
first number - the main set of sprite animations (eg man)
second number - the animation set in use (eg run or idle)
third number - the frame of said animation.
(the ground has this as i plan to have animated grounds late on)
Now, I wish to make a script for the character (and ground alike) that has an editable value that is view able in the unity editor, for example how things like the sprite renderer has sprite, colour etc. Which dictates what first number to use (see above) what second number and the delay for the animation of the third number. This will then tell the sprite renderer what pictures to load and how quickly to load them. I also wish for the script to scan for the file starting with for example character_0_0_ and then count how many files after it, so it knows what to do when animating. I would like this script to be checking for a change in one of the variables viewable in the unity editor to change and as soon as it does it checks everything it needs for an animtion.
Something else could be done where there is only 1 box to edit in unity, which you put character_0_0_ or ground_1_0_ or something similar, and it checks everything that way (it also makes the script universal, and usable on the ground, character and walls (which I am adding later)).
This may seem confusing, but it make sense for me and many of you will probably mention a much easier way to do animations and such, but please only say these if it does what I want above.
For scripts and such my file layout:
/Assets
/scripts
ground.cs
character.cs
/sprites
characterSheet.png
character_0_0_0
character_0_0_1
character_0_1_0
character_0_1_1
character_1_0_0
character_1_0_1
character_1_1_0
character_1_1_1
groundSheet.png
ground_0_0_0
ground_1_0_0
(For some reason Stack overflow said the above was code, so i had to make it as that)
ground.cs and character.cs are the scripts in which I want to made as explained above.
In my object view thingy I have
Main Camera
ground
character
I am practically a newb to C# and JS I know bascially the grammar of C# (like where to use {} and put ; at the end of the lines). If you help me with this, i request that you explain the script, like use the // thing to simply explain what each command does, I know a few but not all of them. And I know someone is going to say it is really well documented in tutorial X and such, but most tutorials are not in unity 5 and while helping with the matter do not touch on it exactly.
Thank you for your help, if there is anything about this question/request that you do not understand (It is pretty complex) I will explain.
Maybe I am completely wrong, but it seems to me that you are trying to recreate an Animation system.
Is there a specific reason for which you wouldn't use Unity's animation system?
You can do everything that you describe here with it (change sprite, choose animation speed). And you would have almost no code to write. Just drag and drop you sprites in the editor for a start
EDIT - answer to first comment:
What you should do is create all the animations you need. Then in the animator, you choose which condition will trigger a transition to another animation (for instance, boolean jump is true will transition to animation jump). Then in your code you can call GetComponent().SetBool("Jump", true).
To have different character, you can create different gameObjects (one per character). They all have a different animator with animations specific to the character.
The other solutiojn if you really want one one gameObject and one animator is that you also add string condition to you animation (example, if character=="character1" and jump==true, transition to jump animation).
If I were you I would really focus on testing and learning all you can do with Unity animator. I can't think of a case were you would need to recreate the animation system yourself
Your question was long winded and hard to understand but ill try to help,
firstly if you want editable values in the unity editor I would Suggest using a serialized structure of information like this
[System.Serializable] // this makes it viewable in the inspector
public struct Sprite_Manager;
{
public Sprite[] Render_Sprites; // array of sprites to store sprites
public SpriteRenderer S_Renderer;
public float Anim_Delay;
}
public class Character : MonoBehavior {
Sprite_Manager SMG = new Sprite_Manager(); // instantiate a new instance of the struct
void Set_Sprite()
{
for(int i = 0; i < SMG.Render_Sprites.Length; i++)
{
SMG.S_Renderer.sprite = SMG.Render_Sprites[i];
}
}
void Update
{
Invoke("Set_Sprite", SMG.Anim_Delay);
}
}
Not sure if this is exactly what your looking for but this is one way you could setup a structure of sprite based information and use Invoke to setup some sort of delay when passing new sprites to the renderer.
I am pretty new at C# and I wanted to make a simple 2D RPG (Role-Playing-Game) character which can move around with its walking animation by simply using 'W' 'A' 'S' 'D' keywords. To do that, I used Picture Box to hold character image and 2 Timers Tool, one for managing the 'walking' animation by changing the picture every 100 ms, and the another timer is for moving that Picture Box location every 1 ms.
In the 'Form_KeyDown' event, I set those 2 timers Enabled = True whenever user presses one of the moving keywords and I set those 2 timers Enabled = False in the 'Form_KeyUp' event to indicate that the character is no longer moving.
Here is the first timer code that control the animation by changing the picture on each tick:
private void timerchangepic_Tick(object sender, EventArgs e)
{
//movementPhase will determine the picture to be displayed, added by 1
//every tick means character image change every tick
movementPhase++;
if (movementPhase > 4) movementPhase = 1;
//determining which image is currently displayed
if (charDirection == Direction.Front)
{
if (movementPhase == 1)
pbcharacter.BackgroundImage = Image.FromFile("Icon\\front.png");
else if (movementPhase == 2)
pbcharacter.BackgroundImage = Image.FromFile("Icon\\front2.png");
else if (movementPhase == 3)
pbcharacter.BackgroundImage = Image.FromFile("Icon\\front3.png");
else if (movementPhase == 4)
pbcharacter.BackgroundImage = Image.FromFile("Icon\\front4.png");
}
//and goes the same for another 3 directions (left, right, and back)
}
Here is the second timer code that move the location of the character on each tick:
private void timermovement_Tick(object sender, EventArgs e)
{
if (charDirection == Direction.Front)
{
pbcharacter.Location = new Point(pbcharacter.Location.X, pbcharacter.Location.Y + 5);
}
//and goes the same for another 3 directions (left, right, and back)
}
My problem is: the character can't move well when I hold one of the moving keystroke. In a first second it works fine, but after a few second (2-3 seconds) pressing and holding 'S' stroke made the character stopped, moved a little, stopped again, moved a little and over and over. Besides, the animation only worked for 1 lap, the picture changed from 'front' to 'front2' until 'front4' well, but not from 'front4' back to 'front'. In conclusion, the character's animation only ran for 1 shift, then it became a static image which moved a little, stopped, moved again, and stopped again whenever i hold 'S' button.
What is wrong with my codes? Are there any better approaches to implement a moving 2D character task with its animation?
I suggest you to use somethimg more specific to build your application: XNA, MonoGame or Unity3D. But if you are using winforms I have several suggestion for you:
1) Cache images instead load them from file every time.
2) Cause Timer Events interval is not very accurate calculate ElapsedTime from last event. And make change +5 to something dependent of ElapsedTime.
3) Instead using several timers organize game loop to handle your events.
4) Use Double Buffer on your form.
Building a game using windows forms can be incredible hard, and also extremely inefficient. If you were to use XNA, which isn't that difficult to learn, you could create a much better and much stronger game.
If your using WinForms i will presume your a beginner so I wouldn't bother with unity or mono as they are much more complicated. If you still reject this I would advise you to:
Cache images
more accurately calculate ElapsedTime and use dependencies
use a while loop to repeat for each tick, this point should fix your problem
Hope to have been of help.
I think if you must make your game using windows form then you need to
at least handle your update with a gameloop instead of form timers and
if your moving things around your going to need some kind of clock to
help your game run at the same speed on any cpu