Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use iTween to achieve a mouse click a place in the middle of the plane there is a ball thrown up and then falling

Shulou Source: shulou.com Published: 2022-06-03 02:37:18 10月01日 Update

This article mainly introduces how to use iTween to achieve a mouse click a place plane in the middle of a throw up and then fall of the ball, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

First, we create a plane, and we use Cube to set X and Z to 10, and then we have a terrain, and then we create a ball that needs to be generated, and then we create an empty object, put our ball under the empty object, and make it into prefabs, and then we build an icon, and here we use Cube instead of this icon, and change the color to red. We also need to create an empty object that is 0.5f away from our terrain Y, mainly to instantiate a ball in this place, and then we write a C # script on this empty object.

The code is as follows:

Using UnityEngine

Using System.Collections

Public class SphereSpawn: MonoBehaviour

{

Public GameObject spherePrefab

Public Transform redSphere

Void Update ()

{

Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition); / / get the click point on the screen

RaycastHit hitinfo

Bool isCollider = Physics.Raycast (ray, out hitinfo); / / get the point of the collision

If (isCollider & & hitinfo.collider.tag = = "Ground") {

RedSphere.position = hitinfo.point

}

If (Input.GetMouseButtonDown (0)) {

/ / instantiate the ball

SpawnSphere ()

}

}

Void SpawnSphere ()

{

GameObject go= GameObject.Instantiate (spherePrefab,transform.position,Quaternion.identity) as GameObject

Go.GetComponent () targetPosition=redSphere.position

}

}

/ / the main functions above are to determine the mouse click time (detected by X-rays) and to instantiate the ball.

And then we write a script on top of our default object, on the parent object.

Using UnityEngine

Using System.Collections

Public class MoveSphere: MonoBehaviour

{

Public Vector3 targetPosition

/ / the location you need to move to

Private GameObject sphere

Void Start ()

{

/ / Let the ball go up and down

Sphere=transform.Find ("Sphere"). GameObject

ITween.MoveTo (this.gameObject, iTween.Hash ("time", 2, "position", targetPosition, "easetype", iTween.EaseType.easeOutCubic))

ITween.FadeTo (this.gameObject, iTween.Hash ("time", 2, "delay", 2, "alpha", 0, "oncomplete", "destorySprere"))

ITween.MoveBy (sphere, iTween.Hash ("time", 1, "y", 5, "easetype", iTween.EaseType.easeOutCubic))

ITween.MoveBy (sphere, iTween.Hash ("delay", 1, "time", 1, "y",-5, "easetype", iTween.EaseType.easeInCubic))

}

Void destorySprere ()

{

Destroy (this.gameObject)

}

}

/ / the above code mainly uses iTween to instantiate how the ball moves and destroys objects.

U3D interface:

Thank you for carefully reading this article, I hope the editor shared the "how to use iTween to achieve a mouse click on a place plane in the middle of a ball thrown up and then fall" this article is helpful to everyone, but also hope that you support a lot, pay attention to the industry information channel, more related knowledge waiting for you to learn!

Tags: Object example article place plane mouse code icon terrain script value location interest function at the same time ray ball screen time is in Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi MySQL OPPO Reno NVidia Huawei