- Home /
Making a bubble pop game type of shot trajectory
So I am working on a game and I'm wanting my shot trajectory prediction to act similar to that in Bubble Pop. If you are unfamiliar with what I'm talking about see picture below.
Basically, I want my trajectory be able to predict the line my object will follow when it bounces off of walls or obstacles. Either that or make it so my current line detects an obstacle and stops making the line when it collides with and obstacle (similar to peggle). Below is currently how my trajectory prediction sits as well as my code. What could I add to make this perform how I want it to? Currently my bullet does bounce off of colliders, the trajectory just does not show that.
public class Gun : MonoBehaviour
{
public GameObject bullet;
public float launchForce;
public Transform shotPoint;
public GameObject point;
GameObject[] points;
public int numberOfPoints;
public float spaceBetweenPoints;
Vector2 direction;
private void Start()
{
points = new GameObject[numberOfPoints];
for (int i = 0; i < numberOfPoints; i++)
{
points[i] = Instantiate(point, shotPoint.position, Quaternion.identity);
}
}
void Update()
{
Vector2 bowPosition = transform.position;
Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
direction = mousePosition - bowPosition;
transform.right = direction;
if (Input.GetMouseButtonDown(0))
{
Shoot();
}
for (int i = 0; i < numberOfPoints; i++)
{
points[i].transform.position = PointPosition(i * spaceBetweenPoints);
}
}
void Shoot()
{
GameObject newBullet = Instantiate(bullet, shotPoint.position, shotPoint.rotation);
newBullet.GetComponent<Rigidbody2D>().velocity = transform.right * launchForce;
}
Vector2 PointPosition(float t)
{
Vector2 position = (Vector2)shotPoint.position + (direction.normalized * launchForce * t) + 0.5f * Physics2D.gravity * (t * t);
return position;
}
}
Answer by Anonymous620 · Jul 07, 2021 at 02:21 AM
Watch this tutorial: https://www.youtube.com/watch?v=GttdLYKEJAM Here is the code for that tutorial: https://gist.github.com/runewake2/ff564a39ba539a9bb1a71321c9f2723a
even though it's in 3d the concept is still the same
This isn't quite what I'm looking for, it reflects a line based on the direction something is pointed in but it doesn't account for the physics of my bullet. I do believe the Raycast solution is what I need, I'm just not quite sure how to implement it. I'm still pretty new at all of this, I'm currently working on how to implement the raycast into my code properly. Do you know how that might work?
You would have to reflect the raycast using vector3.reflect. although I haven't used it before and I'm not sure if it works in 2d. Hope this helps
Your answer
![](https://koobas.hobune.stream/wayback/20220613035927im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Adding a "paddle bump" to paddle in block breaker game? 0 Answers
How can i add constant rotation speed on 2d object? 1 Answer
Pixel Per Unit usage and behavior 1 Answer
What Should i use rigidbody.AddTorque() or Quaternions ? 1 Answer
Pass raycast line through the target,How to make raycast2d line pass through the target? 0 Answers