Take notes on the first game Roll the ball in unity, unityroll
1. The camera follows the motion. The logic is to keep the distance between the camera and the main character unchanged (Undate () function ).
Offset = trandform. position-player.position.
Undate ()
{
Transform. position = player. position + offset;
}
2. Control the movement of objects (Input reading usage) (Other methods can be supplemented)
Method 1:
Obtain the rigid body of an object. The rigid body is appended with the AddForce () method. The Input is a human vector, Input. GetAxis (). The buttons on the upper and lower sides of the keyboard are read through Horizontal and Vertical.
Float h = Input. GetAxis ("Horizontal ");
Float v = Input. GetAxis ("Vertical ");
Rd. AddForce (new Vector3 (h, 0, v) * force );
3. Collision Detection OnCollisionEnter () refers to an object that is in conflict with it. It mainly obtains the collision tool collider on it. You can use tags to obtain the collision object.
Void OnCollisionEnter (Collision collision)
{
// String name = collision. collider. name; // obtain the collider (collision tool) of the long-hitting object)
// Print (name );
If (collision. collider. tag = "pickup ")
{
Destroy (collision. collider. gameObject );
}
}
4. Trigger the collision, OnTriggerEnter (), which is passed in as a collision generator.
/Trigger Detection
Void OnTriggerEnter (Collider collider)
{
If (collider. tag = "pickup ")
{
Score ++;
Text. text = score. ToString (); // Score. ToString () is used to assign the score value to Text. (UGUI)
If (score = 10)
{
WinText. SetActive (true); // display winText when the conditions are met
}
Destroy (collider. gameObject );
}
}
}