This is the 16th day of my participation in the August More Text Challenge. For details, see:August is more challenging
Recommended reading
- CSDN home page
- GitHub open source address
- Unity3D plug-in sharing
- Jane’s address book
- My personal blog
- QQ group: 1040082875
Hello, everyone, I am a Buddhist engineer ☆ quiet little magic dragon ☆, not regular update Unity development skills, think useful remember a key three connect oh.
One, foreword
Unity3D is a multi-platform integrated game development tool developed by Unity Technologies that allows players to easily create interactive content such as 3D video games, architectural visualization, real-time 3D animation and other types of content. It is a fully integrated professional game engine. Unity website design has written a series of relatively simple tutorials, today let’s take a look at the content of the tutorial.
Second, the introduction of
Create a simple scrolling ball game and teach you a lot about working with Unity away from it. In your first attempt at Unity development, create a simple scrollball game that teaches you many of the principles of using game objects, components, prefabrication, physics and scripting.
The official tutorial is the video version, the address is unity3d.com/learn/tutor…
GitHub: github.com/764424567/G… * Note: You can clone or download the source code directly from the GitHub repository
Effect:
Third, the body
1. Set up the game
New scene
Hierarchy panel Create->3D Object->Plane
Then in the upper right corner of the Transform component in the Inspector panel, left-click and click Reset to Reset the position
Create a Cube using the same process as creating a plane above, and then return to zero
Adjust position and size
Then follow the above process to create 3 cubes and enclose the plane
Set the protagonist to a new Sphere, set his position to zero, and adjust the height to 0.5.
Create a collectible object
Put them evenly on the ground
Ctrl+S save scene game object name
And then we’re going to group the objects together and create an empty object
Classification of the
2. Move the player object to create a new script playerController.cs to write the script
using UnityEngine;
// Include the namespace required to use Unity UI
using UnityEngine.UI;
using System.Collections;
public class PlayerController : MonoBehaviour {
// Create a private reference to the RigidBody component on the gameobject
private Rigidbody rb;
// At the beginning of the match...
void Start ()
{
// Assign the Rigidbody component to our private rb variable
rb = GetComponent<Rigidbody>();
}
void FixedUpdate ()
{
// Set some local floating variables equal to the horizontal and vertical input values
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
// Create a Vector3 variable and specify X and Z as the characteristics of the horizontal and vertical floating variables above
Vector3 movement = new Vector3 (moveHorizontal, 0.0 f, moveVertical);
// Use the "move" vector 3 above to add a physical force to our player's Rigidbody and multiply it by the "speed" -- the public player speed that appears in the Inspectorrb.AddForce (movement * speed); }}Copy the code
3. Move the camera. Create the script cameracontroller.cs
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
// Store a public reference to the Player Game object so that we can reference its Transform
public GameObject player;
// Store the Vector3 offset between the player and the camera (always place the distance between the camera and the player)
private Vector3 offset;
// At the beginning of the match...
void Start ()
{
// Create an offset by subtracting the camera's position from the player's position
offset = transform.position - player.transform.position;
}
// After the standard 'Update()' loop, before each frame is rendered...
void LateUpdate ()
{
// Set the location of the camera (the game object attached to the script)
// To the player's position, plus the offsettransform.position = player.transform.position + offset; }}Copy the code
4. Display scores and Text in the Inerarchy panel Create->UI->Text Select the newly created Text and then Ctrl+D to copy two texts
Modify the text after adjusting its relative position
Set the tag of the collectible object to Pick Up
If you don’t have the Pick Up option, you can Add Tag
6. Score the playerController.cs script
using UnityEngine;
// Contains the namespaces needed to use Unity UI
using UnityEngine.UI;
using System.Collections;
public class PlayerController : MonoBehaviour {
// Create public variables for the player speed and text UI game objects
public float speed;
public Text countText;
public Text winText;
// Create a private reference to the RigidBody component on the player and a count of the objects picked up so far
private Rigidbody rb;
/ / score
private int count;
// At the beginning of the match...
void Start ()
{
// Assign the Rigidbody component to our private rb variable
rb = GetComponent<Rigidbody>();
// Set the count to zero
count = 0;
// Run SetCountText to update the UI(see below)
SetCountText ();
// Set the Text property of the Win Text UI to an empty string, making "You Win" (Game over Message) empty
winText.text = "";
}
void FixedUpdate ()
{
// Set some local floating variables equal to the horizontal and vertical input values
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
// Create a Vector3 variable and specify X and Z as the characteristics of the horizontal and vertical floating variables above
Vector3 movement = new Vector3 (moveHorizontal, 0.0 f, moveVertical);
// Use the "move" vector 3 above to add a physical force to our player's Rigidbody and multiply it by the "speed" -- the public player speed that appears in the Inspector
rb.AddForce (movement * speed);
}
// When the gameobject intersects a collider, select 'is trigger',
// Store the reference to the collider in a variable named "other".
void OnTriggerEnter(Collider other)
{
If the game objects we intersect are given the tag "Pick Up"
if (other.gameObject.CompareTag ("Pick Up"))
{
// Make the other gameobject (pick) inactive, making it disappear
other.gameObject.SetActive (false);
// Add 1 to the score variable count
count = count + 1;
// Add a SetCountText() function (see below)SetCountText (); }}// Create a separate function that updates the 'countText' UI and checks if the amount needed to win has been reached
void SetCountText()
{
// Update the text field of the 'countText' variable
countText.text = "Your score is:" + count.ToString ();
// Check whether our "count" equals or exceeds 12, because there are 12 Cube scoring objects
if (count >= 12)
{
// Set the text value of "winText"
winText.text = "You Win!"; }}}Copy the code
Check Is Trigger for scoring objects
Attach the script playerController.cs to the Player object
Drag the UI object into the panel where the arrow points
7. Create a new rotator. cs script and write the script
using UnityEngine;
using System.Collections;
public class Rotator : MonoBehaviour {
// Before rendering each frame.
void Update ()
{
// Rotate the game object attached to this script by 15 units on the X-axis,
// The Y-axis is 30 and the z-axis is 45, times the trig function and that is equal to per second
// instead of each frame.
transform.Rotate (new Vector3 (15.30.45) * Time.deltaTime); }}Copy the code
Attached to the scoring object
8. Bug fixes
Rigidbody: RigidBody: RigidBody: RigidBody: RigidBody: RigidBody: RigidBody
add
OK, end of tutorial