Click Here to open Unity's official stencil documentation.
Drag & Drop 2D | World-Space 2D Objects | C# | Unity Game Engine
DragDrop2D.cs
using UnityEngine; public class DragDrop2D : MonoBehaviour { Vector3 offset; Collider2D collider2d; public string destinationTag = "DropArea"; void Awake() { collider2d = GetComponent<Collider2D>(); } void OnMouseDown() { offset = transform.position - MouseWorldPosition(); } void OnMouseDrag() { transform.position = MouseWorldPosition() + offset; } void OnMouseUp() { collider2d.enabled = false; var rayOrigin = Camera.main.transform.position; var rayDirection = MouseWorldPosition() - Camera.main.transform.position; RaycastHit2D hitInfo; if (hitInfo = Physics2D.Raycast(rayOrigin, rayDirection)) { if (hitInfo.transform.tag == destinationTag) { transform.position = hitInfo.transform.position + new Vector3(0, 0, -0.01f); } } collider2d.enabled = true; } Vector3 MouseWorldPosition() { var mouseScreenPos = Input.mousePosition; mouseScreenPos.z = Camera.main.WorldToScreenPoint(transform.position).z; return Camera.main.ScreenToWorldPoint(mouseScreenPos); } }
Launching Projectile | Drawing Trajectory | Projectile Motion | Line Renderer |C#| Unity Game Engine
ProjectileLauncher.cs
using UnityEngine; public class ProjectileLauncher : MonoBehaviour { public Transform launchPoint; public GameObject projectile; public float launchSpeed = 10f; [Header("****Trajectory Display****")] public LineRenderer lineRenderer; public int linePoints = 175; public float timeIntervalInPoints = 0.01f; void Update() { if(lineRenderer != null) { if(Input.GetMouseButton(1)) { DrawTrajectory(); lineRenderer.enabled = true; } else lineRenderer.enabled = false; } if (Input.GetMouseButtonDown(0)) { var _projectile = Instantiate(projectile, launchPoint.position, launchPoint.rotation); _projectile.GetComponent<Rigidbody>().velocity = launchSpeed * launchPoint.up; } } void DrawTrajectory() { Vector3 origin = launchPoint.position; Vector3 startVelocity = launchSpeed * launchPoint.up; lineRenderer.positionCount = linePoints; float time = 0; for (int i = 0; i < linePoints; i++) { // s = u*t + 1/2*g*t*t var x = (startVelocity.x * time) + (Physics.gravity.x / 2 * time * time); var y = (startVelocity.y * time) + (Physics.gravity.y / 2 * time * time); Vector3 point = new Vector3(x, y, 0); lineRenderer.SetPosition(i, origin + point); time += timeIntervalInPoints; } } }
Animation Events | Event Functions | Animator Events | C# | Unity Game Engine
AnimationEventsDemo.cs
using UnityEngine; public class AnimationEventsDemo : MonoBehaviour { //no parameter public void TriggerEvent() { Debug.Log("event triggered"); } //float parameter public void ScaleDownEvent(float scale) { transform.localScale = new Vector3(scale, scale, scale); } //int parameter public void ScaleUpEvent(int scale) { transform.localScale = new Vector3(scale, scale, scale); } //string parameter public void PrintLogEvent(string str) { Debug.Log(str); } //Object parameter public void SpawnObjectEvent(Object gameObject) { if (gameObject != null) Instantiate(gameObject); } //multiple parameters(float,int,string,object) public void MultiParamEvent(AnimationEvent e) { Debug.Log(e.floatParameter + "," + e.intParameter + "," + e.stringParameter); if (e.objectReferenceParameter != null) { Instantiate(e.objectReferenceParameter); } } }
Subscribe to:
Posts (Atom)