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 Unity2D to make a boomerang game

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "how to use Unity2D to do a boomerang game". In daily operation, I believe many people have doubts about how to use Unity2D to do a boomerang game. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to use Unity2D to do a boomerang game". Next, please follow the editor to study!

This paper gives you an example to share the specific code of the boomerang implementation of the Unity2D game for your reference, the specific contents are as follows

1. Boomerang in 2D flat games

To achieve a throw boomerang, will slow down, and then directly take back the hand, encounter the effect of returning immediately

First create a boomerang entity and add the following script to it

The public float Speedrotate;public float AttackDamage; private Vector2 speedVelocity; private float Speedtemprotate;// intermediate variable records the maximum rotation speed bool first = true;//. This Bool value represents whether it is the first stage: the flight phase void Start () {speedVelocity = gameObject.GetComponent (). Velocity; Speedtemprotate = Speedrotate;} private void FixedUpdate () {transform.Rotate (0,0, Speedrotate) / / rotate if according to rotation speed (Speedrotate > 0 & & first) / / rotation speed and flight decrease with time until it is 0 and stop moving {gameObject.GetComponent (). Velocity-= speedVelocity * 0.02f; Speedrotate-= (Speedtemprotate * 0.02f);} else if (Speedrotate=AttackSpeed&&Input.GetAxis ("Fire2") = = 1&&isCanweapon) {TimeweaponBack = 0; isCanweapon = false Vector3 worldPos = Camera.main.ScreenToWorldPoint (Input.mousePosition); / / get the point of the current screen Vector3 dir = worldPos-transform.position;// to get the direction between the player and the screen dir.z = 0Trachap 2D games do not need z-axis GameObject go = Instantiate (weapon, transform.position+dir.normalized*0.2f, transform.rotation); go.GetComponent (). Velocity = dir.normalized*4 / / give boomerang speed}} private void OnTriggerEnter2D (Collider2D collision) {/ / pick up the returned boomerang if (collision.tag== "weapon" & & TimeweaponBack > 0.03f) {isCanweapon = true; TimeweaponShoot = 0; Destroy (collision.gameObject);}}

The effect is demonstrated as follows:

2. Boomerang only on the left and right (transporting bilibili up Qin innocence)

Achieve a boomerang to throw out and fly back, the flight will not be interrupted by strange, and the y-axis follows the player.

The code is as follows

Void Start () {rb2D=GetComponent (); rb2D.velocity=transform.right*speed;// records the initial speed playerTransform=GameObject.FindGameObjectWithTag ("Player"). GetComponment () for the boomerang initial speed startSpeed=rb2D.velocity;//; / / gets the player position void Update () {transform.Rotate (0memo) rotateSpeed); / / self-rotating float y=Mathf.Lerp (transform.position.y,playerTransform.position.y,0.1) / / transform.position=new Vector3 the boomerang y axis with the player transform.position=new Vector3 (transform.position.x,y,0.0f); rb2D.velocity-=startSpeed*Time.deltaTime;// slows the boomerang down slowly, and after reaching 0, the speed direction is the opposite direction to fly back to if (Mathf.Abs (transform.position.x-playerTransform.position.x))

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: 0

*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

Development

Wechat

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

12
Report