Previous link:Blog.csdn.net/qq_36171287…

directory

The last link: https://blog.csdn.net/qq_36171287/article/details/84144912

Write the initial position of the Player box

Set up collision detection between enemy and Player

Add particle system

Four, set up the fence

Set goals

Reference example: https://download.csdn.net/download/qq_36171287/10806875



Write the initial position of the Player box

Open unity3d, then open playermove’s C# script.

Playermove code:

public class playermove : MonoBehaviour {
    
    public float moveSpeed; / / move speed
    private float maxSpeed=15;  // Maximum speed limit
    private Vector3 initialPosition;   // Define the initial position
	// Use this for initialization
	void Start () {
        initialPosition = transform.position;  // The position of the player at the beginning

    }
	
	// Update is called once per frame
	void Update () {
        // Get the keyboard value
        //var Automatically matches the target type
        var move = new Vector3(Input.GetAxisRaw("Horizontal"),0,Input.GetAxisRaw("Vertical")); // Construct a vector to get the X,Y, and Z variables
        var rigidbody = GetComponent<Rigidbody>();
        
        if (rigidbody.velocity.magnitude < maxSpeed)  // The value of the motion vector is less than the maximum speed limit
        {
            rigidbody.AddForce(move * moveSpeed);  // Add a force to the rigid body
        }

        if (transform.position.y<2 -)  // If the Y-axis position drops to -2
        {
            transform.position = initialPosition;  // The player position returns the original position}}}Copy the code

Set up collision detection between enemy and Player

Click on Enemy and select “Add Tag” in the Inspector.

Note: Collisions are valid only if the player is moving.

Add the tag Enemy. Then re-select the Enemy symbol and the new Tag will appear in the Tag.

The player code is as follows:

public class playermove : MonoBehaviour {
    
    public float moveSpeed; / / move speed
    private float maxSpeed=15;  // Maximum speed limit
    private Vector3 initialPosition;   // Define the initial position
	// Use this for initialization
	void Start () {
        initialPosition = transform.position;  // The position of the player at the beginning

    }
	
	// Update is called once per frame
	void Update () {
        // Get the keyboard value
        //var Automatically matches the target type
        var move = new Vector3(Input.GetAxisRaw("Horizontal"),0,Input.GetAxisRaw("Vertical")); // Construct a vector to get the X,Y, and Z variables
        var rigidbody = GetComponent<Rigidbody>();
        
        if (rigidbody.velocity.magnitude < maxSpeed)  // The value of the motion vector is less than the maximum speed limit
        {
            rigidbody.AddForce(move * moveSpeed);  // Add a force to the rigid body
        }

        if (transform.position.y<2 -)  // If the Y-axis position drops to -2
        {
            die();  // The player position returns the original position}}void OnCollisionStay(Collision other)
    {
        if (other.gameObject.tag== "Enemy")
        {
            die();  // The player position returns the original position}}void die()
    {
  
        transform.position = initialPosition;  // The player position returns the original position}}Copy the code

Add particle system

Click Open Editor to set the particle system, uncheck Looping, and set the Start Size range. Create a new folder Pretabs in project and move Particle System into the folder Pretabs.

You can then remove Particle System from Hierarchy and click Player to move Particle System in the folder Pretabs to the text box following Death Effect in Playermove(Script). And then run it.

Player code:

public class playermove : MonoBehaviour {
    
    public float moveSpeed; / / move speed
    private float maxSpeed=15;  // Maximum speed limit
    private Vector3 initialPosition;   // Define the initial position
    public GameObject deathEffect;   // Death effect
	// Use this for initialization
	void Start () {
        initialPosition = transform.position;  // The position of the player at the beginning

    }
	
	// Update is called once per frame
	void Update () {
        // Get the keyboard value
        //var Automatically matches the target type
        var move = new Vector3(Input.GetAxisRaw("Horizontal"),0,Input.GetAxisRaw("Vertical")); // Construct a vector to get the X,Y, and Z variables
        var rigidbody = GetComponent<Rigidbody>();
        
        if (rigidbody.velocity.magnitude < maxSpeed)  // The value of the motion vector is less than the maximum speed limit
        {
            rigidbody.AddForce(move * moveSpeed);  // Add a force to the rigid body
        }

        if (transform.position.y<2 -)  // If the Y-axis position drops to -2
        {
            die();  // The player position returns the original position}}void OnCollisionStay(Collision other)
    {
        if (other.gameObject.tag== "Enemy")
        {
            die();  // The player position returns the original position}}void die()
    {
        Instantiate(deathEffect,transform.position,Quaternion.identity);         // Death effect
        transform.position = initialPosition;  // The player position returns the original position}}Copy the code

Four, set up the fence

Create a new Cube and rename it Wall. Create a new empty box named Walls and drag all the wall elements above into Walls.

Set goals

Create a new block called Goal, change the color, and drag the block to the desired location.

Create a new script file and name it GameCenter.

Then edit the GameCenter code as follows:

public class GameCenter : MonoBehaviour {
    public static int currentLevel = 0;  // Set the number of levels to 0 by default

    public static void CompleteLevel()  // Complete the current level{ Application.LoadLevel(currentLevel); }}Copy the code

Next you need to consider collisions in your PlayerMove script. Select the Goal widget in Unity and select the Is Trigger in the Inspector.

Then add the Tag for the Goal component, name the Tag Goal, and change the Tag value to Goal

Then add a new method to the PlayerMove script.

void OnTriggerStay(Collider other)  // Collision judgment
    {
        if (other.gameObject.tag == "Goal")
        {
            GameCenter.currentLevel++;  // currentLevel of GameCenter increases
            GameCenter.CompleteLevel(); // Call the CompleteLevel() method of GameCenter}}Copy the code

To Add the scene, select File–Build Settings–Add Current

To create the second scenario, select Demo01 in Project and press CTRL+D. A copy of the demo01 scenario will be automatically made and named Demo02.

You can modify the position of scene components in DEMO02

Also add Demo02

The next run time is successful, you can beautify the scene!

Reference Examples:Download.csdn.net/download/qq…