Previous biography:
Today, Jia Wei brother told us a third case, it is regrettable that the first second I did not remember to use the blog Park to write, blog Park since the freshman summer after the application has no use, but really will feel sorry. There is time later, I will still put the algorithm a little bit of the breach ~ this is my computer as a professional student must do.
Theme: unity4.3 game development Project The third case platform (game platform)
First of all, Jia Wei brother said really good, whether it is the code or the specific operation, speak very clearly.
The planning of a game I do not write in detail, I think I have not yet to be able to write the extent of planning, the following I specifically detailed the function of the module implementation of the function.
Second, module function:
The realization and function analysis of 1,LEVEL1
In the process of doing this, I realized that the location of the object in each scene of the specific settings, data can be set according to their own needs. What you need to set is the game background, platform, diamond, character.
Here's the data. The first step you need to make is to add a master camera to the scene, the camera is global, with a global sound, and a parallel light source, and second, remember to set the camera to the orthographic format. The book says to set the camera position to z=-5, close to see-0.3, the farthest see 20, we set the farthest to 10 is OK. (Various import resources I will not say, the foundation, remember in the beginning, save the scene. Keep the project in good. )
Position of background (0,0,9)
Location of the platform (0,0,5)
Location of the camera (0,0,-1)
In order to ensure that the objects that occur are in the same interface to trigger, all objects must be guaranteed to be z=0 on the same interface.
About the world coordinates and the local coordinates of the object, I decided to take a good look, I feel this is my weakness.
NN// ask someone else. Relative to a parent object in the world coordinate system, the position of the sub-object in the world coordinate system is equal to the coordinates of the child object and the coordinates of the parent object .
Looking at the whole picture, we need to add a collider to the diamond, the platform, the diamond needs to be triggered, so that when the character to collect the diamond, the effect of destruction, the characters to add the character controller
After adjusting the position of each object, we begin to write the code for each character.
According to the order in the book, I first put gem, the diamond code. GEM has the following functions to achieve: to achieve a certain distance within the platform to move up and down, followed by the destruction of the character collision
When we pour in the resources, we first find a suitable location for the diamond, that is, defining the initial position of the diamond, which is placed in the start function, no doubt STARTPOSITION=TRANSFORM.POSITION.Y because the diamond is moving up and down, independent of the x axis.
Initializes the definition of the diamond's speed, start position, and whether a downward bool variable
private float speed=3.0f;
Private Flaot startposition;
private bool Isdown=true;
And then write the Diamond's motion pattern in the upstate.
Statement in a certain
if (Isdown)
Transform. Translate (Speed*time.deltatime*vector3.down);
Else
Transform. Tranlate (Vector3.up*speed*time.deltatime);
Judging the direction of the diamond's movement
if (startposition-transform.positon>0.4f)
Isdown=false;
if (transform.position-startposition>0.4f)
Isdown=true;
After labeling the character, the character will destroy it after it touches the diamond.
void Ontriggerenter (Collider other)
{
if (other.tag== "player")
{
Audio.Source.PlayClipAtPoint (soundname,new Vector3 (0,0,-10);//http://www.unitymanual.com/home.php?do=blog& id=141&mod=space&uid=12062 a detailed look at this bar, play the clip at the specified location. The audio source is automatically eliminated when playback is complete.
DeTroy (Gameobject);
}
}
Here's a look at the movement of the characters.
First of all, talk about the change of the character frame
First to define
public int framenumber=10;//The book that just started to drag the picture of the character run frame into the screen, the last time I saw a learning brother said how to get a map code to add to go after the move, because the picture is the image of the artist to do a series of animation, Not just a poster ~ ~
public bool Direction=true; judge the first run direction of the character
public int lastframeno=0;
private bool onetime=true; This is the definition of a motion process.
private int index=0;
private float framerate=0;
private float mytime=0; Define an initial value my time is 0.
private int myindex=0;//This is the change of frame in the course of action
In
Next, write the judgment about motion.
if (direction)
{
Render.material.maintexturescale=new Vector2 (framerate,1);//is the tiling coordinates of the map Uvs
Render.material.maintextureoffset=new Vector2 (index*framerate,0);//render.material.maintextureoffset is the offset of the map
}
Else
Render.material.maintexturescale=new Vecor2 (-framerate,1);
Render.material.maintextureoffset=new Vector2 (index*framerate,0);
Let's take a look at the rest of the logic
void Update ()
{
Famerate=1.0f/framenumber;//1.0f/framenumber 1/small number = Proportion of the long station large map of each small graph
if (oneTime)//one-time switch
{
The accumulation of mytime+=time.deltatime;//time
myindex= (int) (mytime* (FRAMENUMBER-1))//change of frame during motion
Index=myindex%framenumber;
}
if (lastframeno!=0)
{
if (index==lastframeno-1)//frame is an array, starting from 0, the first few frames ==framenumber-1
Onetime=false;
}
}
Next, study the character game state ~// give oneself a sentence, difficult to insist back than easy to give up a lot more!!
The characters are divided into the following States, enumerated by enumeration type implementation
public enum GameState
{
Idle
Runleft,
Runright,
Jumpright,
Jumpleft,
Idleleftjump,
Idlerightjump
Celebrate,
Die
}
The Playerstatecontroller code is implemented as follows
In the initial definition, the setting of the motion state be careful not to miss out, I try my own dictation
Public Texture Idle;
public Texture run;
Public Texture Jump;
Public Texture celebrate;
Publictexture die;
public x Wintexture;
Public texture2d dietexture;
Public GameState gamestate;
Voice
Public AudioClip Soundname;
Public AudioClip Diesound;
In setting several switches
private bool Movedirection=true;
private bool Exit=false;
private bool Youwin=false;
private bool Youdie=false;
Private Animationcontroller myanimation;
Get Animationcontroller components in start to make it easy to change the status of the person and benefit Yong
void Start ()
{
Myanimation=getcomponent<animationcontroller> ();
}
Continue to implement the character's movements in update ()
The first realization is the realization of the character nine basic action logic
if (Input.getaxis ("horizontal"))
{
Movedirection=false;
if (Input.getbutton ("Jump"))
Gamestate=gamestate.jumpright;
Else
Gamestate=gamestate.runright;
}
else if (Input.getaxis ("horizontal") <0)
{
Movedirection=true;
if (Input.getbutton ("" jump))
Gamestate=gamestate.jumpleft;
Else
Gamestate=gamestate.runleft;
}
Else
if (Input.gtbutton ("Jump"))
{
if (movedirection)
Gamestate=gamestate.idleleftjump;
Else
Gamestate=gamestate.idlerightjump;
}
else Gamestate=gamestate.idle;
}
if (Youwin)
Gamestate=gamestate.celebarate;
if (Youdie)
Gamestate=gamestate.die;
The code is understandable, but don't miss the action of the character.
Next, write with a switch statement, take an example
Switch (gamestate)
{
Case Gamestate.idle:
Transform.render.material.SetTexture ("_maintex", idle);
Myanimation.framenumber=1;
Myanimation.direction=false;
Break
}
At the time of passing the first pass
if (Input.getbutton ("Jump") &&youwin)
{
if (application.loadedleveiname== "Level1")
Application.loadlevel ("Level2");
else if (application.loadedlevelname== "Level2")
Application.loadlevel ("Level3");
Else
Application.loadlevel ("Level1");
}
if (Input.getbutton ("Jump") &&youdie)
{
Transform.position=new Vector3 (0,-2,0);
Transform.collider.enabled=true;
Youdie=false;
}
Ienumberator Ontriggerenter (Collider Other)
{
if (other.tag== "Exit" &&gameobject.findwithtag ("Gem") ==null)
{
Youwin=true;
Audiosource.playclipatpoint (soundname,new Vector3 (0,0,-10));
}
if (other.tag== "enemy")
{
Youdie=true;
Audiosource.playclipatpoint (Diesound,newvector3 (0,0,-10)); position determines the size of the sound
yied return new Waitforseconds (0.5f); screen buffering for 5 seconds
Transform.collider.enabled=false;
}
}
GUI interface settings
if (Youwin)
{
Gui. Drawtexture (New Rect (screen.width/2-1/9,screen.height/2-90,358,180), wintexture,scalemode.scaletofit,true,0);
}
Playcontroller aspects
public class Playercontroller:monobehaviour
{
public float speed = 2.0f;
Private Charactercontroller Controller;
Private Vector3 velocity = Vector3.zero;
private float gravity = 20.0f;
private float jumpspeed = 4.5f;
Private GameState gamestate;
Private Playercontroller Myplayerstatecontroller;
Use this for initialization
void Start ()
{
Controller = getcomponent<charactercontroller> ();
Myplayerstatecontroller = getcomponent<playercontroller> ();
}
Update is called once per frame
void Update ()
{
GameState = myplayerstatecontroller.gamestate;
if (controller.isgrounded)
{
Switch (gamestate)
{
Case Gamestate.runright:
Velocity = speed * (new Vector3 (1, 0, 0));
Break
Case Gamestate.runleft:
Velocity = speed * (New Vector3 (-1, 0, 0));
Break
Case Gamestate.jumpright:
Velocity = speed * New Vector3 ( -1, jumpspeed, 0);
Break
Case GameState. Jumpleft:
Velocity = speed * New Vector3 (-1, 0, 0);
Break
Case GameState. Idleleftjump:
Velocity = speed * New Vector3 (0, jumpspeed, 0);
Break
Case GameState. Idlerightjump:
Velocity = speed * New Vector3 (0, jumpspeed, 0);
Break
Case GameState. Celebrate:
velocity = Vector3.zero;
Break
Default
velocity = Vector3.zero;
Break
}
}
Else
{
VELOCITY.Y-= gravity * time.deltatime;
}
Controller. Move (velocity * time.deltatime);
}
}
Unity Learning Note 1012 (platform)