public class TrajectoryRenderer : MonoBehaviour{ [SerializeField] LineRenderer lineRenderer; // LineRenderer [SerializeField] int lineCount; // , , [SerializeField] float lineLenght; // [SerializeField] Ball ball; // }
inicio privado vac铆o ( ){ lineRenderer . positionCount = lineCount ;}
Vector2 mousePos = C谩mara . principal . ScreenToWorldPoint ( Input . MousePosition ) ;
Vector2 direction = mousePos - ( Vector2 ) transform . posici贸n ;direcci贸n . Normalizar ( ) ;
public class Ball : MonoBehaviour{ Rigidbody2D rb; [SerializeField] float velocityScaler; public float velocity; Vector2 mousePos; private void Start() { rb = GetComponent<Rigidbody2D>(); } private void Update() { mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); velocity = Vector2.Distance(transform.position, mousePos) * velocityScaler; if (Input.GetMouseButtonUp(0)) { Vector2 direction = (mousePos - (Vector2)rb.transform.position); direction.Normalize(); rb.velocity = direction * velocity; } }}
var vx = ball.velocity * direction.x;var vy = ball.velocity * direction.y;
for (int i = 0; i < lineCount; i++){ float t = i * lineLenght; var nextPos = new Vector2(vx * t, vy * t - (-Physics2D.gravity.y * t*t) / 2); lineRenderer.SetPosition(i, (Vector2)transform.position + nextPos);}
public class BallTrajectory : MonoBehaviour{ [SerializeField] LineRenderer lineRenderer; [SerializeField] int lineCount; [SerializeField] float lineLenght; Ball ball; private void Start() { ball = GetComponent<BallController>(); lineRenderer.positionCount = lineCount; } private void Update() { Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); Vector2 direction = mousePos - (Vector2)transform.position; direction.Normalize(); var vx = ball.velocity * direction.x; var vy = ball.velocity * direction.y; for (int i = 0; i < lineCount; i++) { float t = i * 0.1f; var nextPos = new Vector2(vx * t, vy * t - (-Physics2D.gravity.y * t*t) / 2); lineRenderer.SetPosition(i, (Vector2)transform.position + nextPos); } } }