- Home /
Question by
dvxl · Apr 02, 2020 at 04:41 PM ·
2dshootingdragtrajectoryprediction
2D Trajectory/Path Predictor
Hello! I am currently working on a 2d game with a drag and shoot mechanic. Now i want to have trajectory/path prediction line or dots, can someone tell me how to do this or actually do this for me? Here is my Drag and Shoot Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DragNShoot : MonoBehaviour
{
public float power = 10f;
public Rigidbody2D rb;
public Vector2 minPower;
public Vector2 maxPower;
DragLine tl;
Camera cam;
Vector2 force;
Vector3 startPoint;
Vector3 endPoint;
public GameObject player;
private void Start()
{
cam = Camera.main;
tl = GetComponent<DragLine>();
}
private void LateUpdate()
{
if (Input.GetMouseButtonDown(0))
{
startPoint = cam.ScreenToWorldPoint(Input.mousePosition);
startPoint.z = 15;
}
if (Input.GetMouseButton(0))
{
Vector3 currentPoint = cam.ScreenToWorldPoint(Input.mousePosition);
currentPoint.z = 15;
tl.RenderLine(startPoint, currentPoint);
}
if (Input.GetMouseButtonUp(0))
{
endPoint = cam.ScreenToWorldPoint(Input.mousePosition);
endPoint.z = 15;
force = new Vector2(Mathf.Clamp(startPoint.x - endPoint.x, minPower.x, maxPower.x),
Mathf.Clamp(startPoint.y - endPoint.y, minPower.y, maxPower.y));
rb.AddForce(force * power, ForceMode2D.Impulse);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Trying to make a trajectory prediction line! 1 Answer
2D projectile trajectory prediction 2 Answers
How to predict where the ball will land after being hit by bat? 4 Answers
Trajectory prediction with linear drag 1 Answer
How to create a 2D parabolic trajectory prediction line with javascript? 1 Answer