public class TrajectoryRenderer : MonoBehaviour{ [SerializeField] LineRenderer lineRenderer; // LineRenderer [SerializeField] int lineCount; // , , [SerializeField] float lineLenght; // [SerializeField] Ball ball; // }
рдирд┐рдЬреА рд╢реВрдиреНрдп рдкреНрд░рд╛рд░рдВрдн ( ){ рд▓рд╛рдЗрдирд░реЗрдВрдбрд░ ред positionCount = lineCount ;}
рд╡реЗрдХреНрдЯрд░ 2 рдорд╛рдЙрд╕рдкреЛрд╕ = рдХреИрдорд░рд╛ ред рдореБрдЦреНрдп ред ScreenToWorldPoint ( рдЗрдирдкреБрдЯ ред рдорд╛рдЙрд╕рдкреЛрдЬрд┐рд╢рди ) ;
Vector2 рджрд┐рд╢рд╛ = mousePos - ( Vector2 ) рдХреЛ рдмрджрд▓рдиреЗ ред рд╕реНрдерд┐рддрд┐ ;рджрд┐рд╢рд╛ ред рд╕рд╛рдорд╛рдиреНрдпреАрдХреГрдд ( ) ;
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); } } }