问题
I know very little Unity and would like to create a platform 2D game very simple, just using the basic shapes (squares, rectangles, circles, etc.). I searched the internet and I found a more complicated tutorial of what I need.
For example, I saw this but its goal is more ambitious than mine (I have also seen other tutorials but I can't put in additional links).
I would love to create something very easy as point to understand the program basics and then try to put something more complicated made by me. Does anyone have any advice for me?
This is what I imagine as the first level, is really simple (and ugly):
I do not know where to start, I don't think it makes sense to start with something complicated then do not understand anything and copy only. Preferable from something simple and complicate the work as I become more practical, am I right?
I add that I know Java and a bit of JavaScript.
I followed the tutorial of Fredrik and this is what I got. Reading the code, I see this:
if(col.tag == "Coin") {
coins++;
Destroy(col.gameObject); // remove the coin
}
So when the player collides to the coin, the coin should not be destroyed? But in my case is not like that..
回答1:
This is not exactly the right forum for this question, but I'm in the mood for answering so I will. I hope it can help you, even if it gets flagged, hehe.
So for your simple 2D platformer you need:
A player
- with physics (Rigidbody component)
- and collisions (Box collider component)
- with left/right movement and jump (code)
- that can detect triggers such as powerups/spikes/level ending (code)
Level ground objects
- with colliders (so player can walk on it) (Box collider component)
Pickups
- with trigger colliders (so player can go through them & detect them) (Box collider component)
Create a new 2D project through the Unity launched. Check the 2D button.
First, lets create our GameObjects. GameObjects are every Object in the game and are created by rightclicking in the hierarchy (where your main camera is, for default) and click "Create Empty". But since this is a 2D game, we're gonna want every object to be a Sprite; an image. So instead of clicking "Create Empty", click "2D Object -> Sprite". This will be our Player. Right now the player doesn't look like anything, since there is no image to represent it. Change this by dragging an image of your choice to somewhere in your unity assets folder and then drag that imported image to the sprite field on the sprite. Like this: http://i.imgur.com/6cKQCss.gifv
Add Components (Like this: http://i.imgur.com/UkkxbgK.png) Box Collider, Rigidbody2d and a new C# script called something like... "Player". Go ahead and tag our player gameobject with the pre-existing "Player" tag and, in the Rigidbody components constraint-fold down, check "Freeze Rotation: Z" See image: http://i.imgur.com/be6G2pB.png
Your player should now look something like this http://i.imgur.com/be6G2pB.png (+ the Player script)
- Here comes the Player script. There might be some bugs (for example, the player can jump forever, even if not on the ground), try to fix them if there are, or improve if it doesnt work good enough. It requires the player to have a Rigidbody2D- & Collider2D-component (a collider component can be a box collider, circel collider etc etc. Any shape), which we added in the last step. The Rigidbody2d gives the player physics (falling with gravity, affected by forced) and the collider gives it the ability to collide with things. Without this the player would just fall through the ground and can't detect if it touches other triggers/colliders.
This is a C#-script which I recommend you use. It's very similar to java and easy to pick up. Most of it is unity-specific anyway:
using UnityEngine;
public class Test : MonoBehaviour {
float speed = 3f;
Rigidbody2D rb;
int coins = 0;
Vector3 startingPosition; // If we hit a spike we will teleport player to starting position.
void Start()
{
rb = GetComponent<Rigidbody2D>(); // Get the rigidbody component added to the object and store it in rb
startingPosition = transform.position;
}
void Update()
{
var input = Input.GetAxis("Horizontal"); // This will give us left and right movement (from -1 to 1).
var movement = input * speed;
rb.velocity = new Vector3(movement, rb.velocity.y, 0);
if (Input.GetKeyDown(KeyCode.Space))
{
rb.AddForce(new Vector3(0, 100, 0)); // Adds 100 force straight up, might need tweaking on that number
}
}
void OnTriggerEnter2D(Collider2D col) // col is the trigger object we collided with
{
if (col.tag == "Coin")
{
coins++;
Destroy(col.gameObject); // remove the coin
}
else if (col.tag == "Water")
{
// Death? Reload Scene? Teleport to start:
transform.position = startingPosition;
}
else if (col.tag == "Spike")
{
// Death? Reload Scene? Teleport to start:
transform.position = startingPosition;
}
else if (col.tag == "End")
{
// Load next level? Heres how you get this level's scene number, add 1 to it and load that scene:
// SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
}
}
Next, lets create our level objects.
Right click in the hierachy and click "Create Empty". This will act like a folder to all our level objects, to keep it tidy. Right click on this newly created empty game object and click "2D Object -> Sprite" and name this "Floor" (or whatever). If you want to see it, give it a sprite like we did with the player! (gif again: http://i.imgur.com/6cKQCss.gifv) Give this block a "Box Collider 2D" component. The player can now stand on these objects. You can now go ahead and run the game, you should be able to have a (somewhat) functioning player and objects for player to stand on. Change its values in its Transform component to change its size properties. You can copy and paste this object if you need more (which you will). There's also a thing called "Prefab". It's like saving a GameObject so you can use it many times and has many more benefits that you will realize later. To save the GameObject as a Prefab, simply Drag the Object from the Game Hierarchy to somewhere in your Projects Assets folder (like we did with sprites, kind of).
For Coins, do the same thing but give it a new Tag (how to create new tag: http://i.imgur.com/dYt9b0T.gifv) called "Coin" and in the Box Colldier 2D component, check the "Is Trigger" button. This will make it trigger the OnTriggerEnter2D when we walk in it and player wont be able to stand on it; we will go straight through it (since its just a trigger). And we will use the Tag name to identify what kind of an object it is in our OnTriggerEnter2D method in the player.
Repeat the coin step for most of the other objects you want. We identify them and handle the action we want with
void OnTriggerEnter2D(Collider2D col)
{
if (col.tag == "Coin")
{
coins++;
Destroy(col.gameObject);
}
else if (col.tag == "Water")
{
transform.position = startingPosition;
}
else if (col.tag == "Spike")
{
// Death? Reload Scene? Teleport to start:
transform.position = startingPosition;
}
else if (col.tag == "End")
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
/*
*
* Keep adding objects to collide with here!
*
*/
}
This is a basic example of how you what you asked. More in depth of things to know... Further important information would be...
Every object has as Transform which contains the Object's Position, Rotation and Scale. Modify these (in script: transform.position, transform.rotation, transform.localScale) to modify... well, the said properties.
To get a component in the code you need to use GetComponent(); where Component is the Component you want, like we did in our Player-script with rb = GetComponent<Rigidbody>();
.
Now do this example, expand it, google "Unity how to ..." if you need help with anything or ask here if you can't get it to work.
Happy Unitying! Oh and ask away if you've got questions on my answer.
来源:https://stackoverflow.com/questions/42207122/create-a-basic-2d-platformer-game-in-unity