Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Unity to realize the game of digging and ascending

2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to use Unity to achieve the relevant knowledge of the game, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe everyone will read this article on how to use Unity to achieve the game will have some gains, let's take a look at it.

implementation method

1. scene construction

Let's do it, let's actually do it.

Create a new scene, assemble a man and hammer with simple 3D objects, hang different materials and distinguish them with colors.

And then we're going to hang the Rigibodies separately, because we want to make the 2D plane work, so we're going to lock the Z axis movement and the X and Y axis rotation separately on the Rigibodies, and we're going to remove the gravity from the hammer, because we're going to use code to control the hammer position later.

2. control hammer

Create a new script to mount on the parent node and get the body and hammer separately.

The idea is to add an empty child node to the hammer head as an anchor point for the hammer head, and rotate the hammer around the Z axis of this anchor point so that the hammer handle points in the direction of the body.

public GameObject body;public GameObject hammer;Rigidbody body_rig;Rigidbody hammer_rig;Transform hammer_anchor;void Start(){ body_rig = body.GetComponent(); hammer_rig = hammer.GetComponent(); hammer_anchor = hammer.transform.GetChild(2);}//Physics-related operations are generally best done in FixedUpdate, synchronized with the system's physical calculations private void FixedUpdate() { HammerControl(); }void HammerControl(){ //Get mouse coordinates on screen and convert to world coordinates Vector2 MousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); //Move the hammer by modifying the speed of the rigid body on the hammer, and use the coordinates of the hammer as the starting point of the vector //Note that the anchor point of the hammer is equal to the Z value of the coordinates of the hammer itself. In order to make the rotation axis parallel to the Z axis of the world coordinate, the Z value of the mouse coordinate is also directly used. Z value of the hammer coordinate hammer_rig.velocity = (new Vector3(MousePosition.x, MousePosition.y, hammer.transform.position.z) - hammer_anchor.position) * 10; //Get directions from body to mouse Vector3 direction = (new Vector3(MousePosition.x, MousePosition.y, hammer_anchor.position.z) - new Vector3(body.transform.position.x, body.transform.position.y, hammer_anchor.position.z)).normalized; //Turn the hammer toward the body along the hammer anchor hammer.transform.RotateAround(hammer_anchor.position, Vector3.Cross(hammer_anchor.up, direction), Vector3.Angle(hammer_anchor.up, direction)); }

The effect is as shown in the picture:

But now the problem comes, because it is only a simple simulation of the effect of the function, so the male does not have a hand, but ultimately we want the hammer to have a maximum distance limit from the male, so it seems as if there is an invisible hand to operate the hammer, how to calculate this range?

As can be seen from the figure, the idea of this problem is to follow the coordinates of the mouse when the hammerhead anchor is inside the maximum range of motion, and to follow the intersection of the hammerhead anchor and the vector of the body coordinates and the maximum range of motion when it is outside the maximum range of motion. This idea is used in code to get the vector of directions from the current body to the mouse position when the mouse moves outside the maximum distance range, and then make the length of this vector equal to the length of the maximum distance.

So let's modify the code based on that.

public float MaxDistance; float RelativeDistance;//Select a function to get transformed mouse coordinates other than the maximum distance Vector2 GetConfinedPosition(Vector2 mouseposition) { Vector2 Confined_MousePosition; //Remove the Z value of the body coordinate to avoid the influence of calculating the distance Vector2 body_position = new Vector2(body.transform.position.x, body.transform.position.y); //Calculate the relative distance between the current mouse position and the body position RelativeDistance = Vector2.Distance(mouseposition, body_position); if (RelativeDistance > MaxDistance) { //When the relative distance is greater than the maximum distance set by yourself, get the target coordinates after conversion //The idea here is to take a slight turn, starting with the vector with the maximum distance and the direction from the body to the mouse. //Adding the current coordinates of the body to this vector is equivalent to setting the starting point of the vector to the coordinates of the body. //The final vector and coordinate point can be directly converted to each other, and the converted target coordinate is equal to this vector. Confined_MousePosition = (mouseposition - body_position).normalized * MaxDistance + body_position; } else { //If the relative distance is less than the maximum distance, then the current mouse coordinates are the target coordinates. Confined_MousePosition = mouseposition; } return Confined_MousePosition; }

Then modify the MousePosition assignment in HammerControl():

//Get the mouse coordinates on the screen and convert them to world coordinates. If the relative distance is greater than the maximum distance, get the converted coordinates Vector2 MousePosition = GetConfinedPosition(Camera.main.ScreenToWorldPoint(Input.mousePosition));

Finally adjust MaxDistance to a suitable value, the effect is as follows:

3. Control the reverse movement of the body

The hammer moves in the opposite direction to the theoretical movement of the body, and is tangent to a circle with the center of gravity of the body as the origin and the maximum distance of movement of the hammer as the radius. So let's follow this line of thought.

First of all, there was a prerequisite. The hammer could not be forced out of thin air. It had to be pestle on the ground or hung on an obstacle.

Let's start by adding a collision box to the hammer and a new script to the hammer that uses a few simple lines of code to detect if the hammer hits something.

public class CollisionDetection : MonoBehaviour{ public bool IsCollision; private void OnCollisionEnter(Collision collision) { IsCollision = true; } private void OnCollisionExit(Collision collision) { IsCollision = false; }}

Note here that the hammer is the parent of the hammer, so even if the collision box is not on the hammer, the hammer as the parent object can still detect the collision information on the child object. Conversely, since there is no rigid body component on the hammer, the script hanging on the hammer will not detect the collision information.

The body is then given an opposite velocity value when a collision is detected.

void HammerControl(){ //Add the following code if(hammer.GetComponent().IsCollision) after assigning a value to hammer_rig.velocity) { BodyControl(hammer_rig.velocity); }}void BodyControl(Vector3 velociy) { body_rig.velocity = -velociy; }

The final effect is as follows:

The content of this article on "How to use Unity to realize the game of digging for promotion" is introduced here. Thank you for reading! I believe everyone has a certain understanding of the knowledge of "how to use Unity to realize the game of digging for promotion." If you still want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 219

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report