Unity3D study notes (two, rolling the ball to eat gold coins)

Source code: keyboard arrow keys to operate the ball rolling to eat gold coins Unity3D source code

Next: Unity3D study notes (3. Small ball parkour)

1. Create a color shader

 2. Plane creation

Three, the wall

In the same way, create a new Cube, adjust the properties, and set up a fence

4. Create a new ball and assign relevant attributes to the ball 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Ball : MonoBehaviour
{
    Rigidbody r;
    // Start is called before the first frame update
    void Start()
    {
        r = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {
        float x = Input.GetAxis("Horizontal");//让系统知道玩家按下了方向键的左右
        float z = Input.GetAxis("Vertical");//让系统知道玩家按下了方向键的前后
        Vector3 f = new Vector3(x,0,z);//把玩家的输入操作打包在一个力f里面
        r.AddForce(f*5);//给小球施加力f
    }
}

5. Create coins

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Coin : MonoBehaviour
{
    private void OnTriggerEnter(Collider other)
    {
        Destroy(gameObject);//触碰的时候消失
    }
}

Note: Gold coins need to check the attribute Is Trigger

 Six, run

Seven: Summary

Add RigidBody attribute to the object, which means giving the rigid body attribute to the object;

Function: The game object added with the rigid body component can move under the control of the object system. The rigid body can accept external force and torque to ensure that the game object moves like in the real world. Any game object can only be affected by gravity if the rigid body component is added. The force added to the game object through the script and the interaction calculation with other game objects through the NVIDIA object engine require the rigid body component to be added to the game object. Without the RigidBody component, game objects can penetrate each other without collisions.

in C# script

declare object RigidBody ballRigidBody;

Rigid body object materialization: ballRigidBody = new GetComponent<RigidBody>();

Rigid body object exerts force: ballRigidBody.addForce(****);

Capsule Collider

Is trigger trigger: enabled; 

OnTriggerEnter (trigger event) OnCollisionEnter (collision event)
Both sides of the collision must be colliders (Conllision) Both sides of the collision must be colliders (Conllision)
One of the collision parties must be a rigid body (Rigidbody) The active party of the collision must be a rigid body (Rigidbody)    
One of the colliders on both sides of the collision must check the IsTrigger option Colliders cannot check IsTrigger    
The IsKinematic option of the rigid body can be checked or not checked Rigid bodies cannot check IsKinematic    

Vector3 is a class that represents direction, both size and direction 

Vector3 v=new Vector3();

Vector3.up; represents the unit vector in the positive direction of the y-axis in the world coordinate system. (0, 1, 0)

Vector3.down; represents the unit vector in the negative direction of the y-axis in the world coordinate system. (0, -1, 0)

Vector3.right; represents the unit vector in the positive direction of the x-axis in the world coordinate system. (1,0,0)

Vector3.left; represents the unit vector in the negative direction of the x-axis in the world coordinate system. (-1, 0, 0)

Vector3.forward; represents the unit vector in the positive direction of the z-axis in the world coordinate system. (0, 0, 1)

Vector3.back; represents the unit vector in the negative direction of the z-axis in the world coordinate system. (0, 0, -1)

Vector3.zero; represents the origin in the world coordinate system. (0,0,0)

You can also Vector3 v=new Vector3(x,y,z);<<<< initialize when creating an object

The Input.GetAxis(string axisname) method returns a number of type float. The range is between -1 and 1. If the mouse movement is obtained, it is no longer between -1 and 1. It will change with your mouse speed.

1. Touch screen:

                    1. MouseX Triggered when the mouse is pressed and slides along the X-axis of the screen

                    2. MouseY Triggered when the mouse is pressed and slides along the Y axis of the screen

                    3. Mouse ScrollWheel Triggered when the mouse scroll wheel scrolls

2. Keyboard operation class:
                   1. Vertical corresponds to the up and down arrows on the keyboard, and is triggered when the up or down arrow is pressed
                   2. Horizontal corresponds to the left and right arrows on the keyboard, and is triggered when the left or right arrow is pressed

Methods for receiving keyboard information:

1、 public static bool GetKey(KeyCode key)  

          Parameters: key - a key on the keyboard.

          Return value: bool——returns true when the key specified by name is pressed by the user 

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    void Update()
    {
        if (Input.GetKey(KeyCode.UpArrow))
        {
            print("up arrow key is held down");
        }

        if (Input.GetKey(KeyCode.DownArrow))
        {
            print("down arrow key is held down");
        }
    }
}

It also has an overloaded method: public static bool GetKey(string name), whose parameter is a string

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    void Update()
    {
        if (Input.GetKey("up"))
        {
            print("up arrow key is held down");
        }

        if (Input.GetKey("down"))
        {
            print("down arrow key is held down");
        }
    }
}

2  、public static bool GetKeyDown(KeyCode key)

    Returns true for frames during which the user presses the key with the specified name.

    You need to call this function from the Update function because the state is reset every frame. It doesn't return true until the user releases the key and presses it again.

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            print("space key was pressed");
        }
    }
}

Overload method: public static bool GetKeyDown(string name)

3 、 public static bool GetKeyUp(KeyCode key)

    Returns true the frame the user releases the key with the given name.

    You need to call this function from the Update function because the state is reset every frame. It won't return true until the user presses the key and releases it again

using UnityEngine;

public class Example : MonoBehaviour
{
    void Update()
    {
        if (Input.GetKeyUp(KeyCode.Space))
        {
            print("space key was released");
        }
    }
}

 Overload method: public static bool GetKeyUp(string name)

4. An example of an object moving in the direction of the key

void Update()
    {
        if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
            dir = Vector2.right;
        else if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
            dir = Vector2.left;
        else if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S))
            dir = Vector2.down;
        else if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W))
            dir = Vector2.up;
    }

Guess you like

Origin blog.csdn.net/chenyang_wei/article/details/126343487