Help with c# Unity code

How can I make my trajectory path rebound off of a box collider with this code below?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
  using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BallLuanchWithTrajectory : MonoBehaviour
{
    public float power = 5f;
    private Vector2 _startPostion;
    Rigidbody2D _rigidBody2D;
    LineRenderer _lineRenderer;
    SpriteRenderer _spriteRenderer;

    Vector2 dragStartPos;


    // Start is called before the first frame update
    void Start()
    {
        _rigidBody2D = GetComponent<Rigidbody2D>();
        _spriteRenderer = GetComponent<SpriteRenderer>();
        _lineRenderer = GetComponent<LineRenderer>();

        _startPostion = _rigidBody2D.position;



        _rigidBody2D.isKinematic = true;


    }

    void OnMouseDown()
    {
        _spriteRenderer.color = Color.red;
    }

    void OnMouseUp()
    {
        var currentPosition = _rigidBody2D.position;
        Vector2 direction = _startPostion - currentPosition;
        direction.Normalize();

        _rigidBody2D.isKinematic = false;

        _spriteRenderer.color = Color.white;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0)) 
        {
            dragStartPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        }

        if (Input.GetMouseButton(0))
        {
            Vector2 dragEndPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            Vector2 _velocity = (dragEndPos - dragStartPos) * power;

            Vector2[] trajectory = Plot(_rigidBody2D, (Vector2)transform.position, _velocity, 500);
            _lineRenderer.positionCount = trajectory.Length;

            Vector3[] positions = new Vector3[trajectory.Length];
            for(int i = 0; i<trajectory.Length; i++)
            {
                positions[i] = trajectory[i];
             }
            _lineRenderer.SetPositions(positions);

        }

        if(Input.GetMouseButtonUp(0))
        {
            Vector2 dragEndPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            Vector2 _velocity = (dragEndPos - dragStartPos) * power;

            _rigidBody2D.velocity = _velocity;
        }
    }
    public Vector2[] Plot(Rigidbody2D rigidBody, Vector2 pos, Vector2 velocity, int steps)
    {
        Vector2[] results = new Vector2[steps];

        float timeStep = Time.fixedDeltaTime / Physics2D.velocityIterations;
        Vector2 gravityAccel = Physics2D.gravity * rigidBody.gravityScale * timeStep * timeStep;

        float drag = 1f - timeStep * rigidBody.drag;
        Vector2 moveStep = velocity * timeStep;

        for(int i = 0; i< steps; i++)
        {
            moveStep += gravityAccel;
            moveStep *= drag;
            pos += moveStep;
            results[i] = pos;
        }
        return results;

    }

}
c# in a C/C++ forum....
Hello. I guess that you could find help on the official Unity forum ++
https://forum.unity.com/
This topic wouldn't be so bad if it were in the General forum instead of here in Beginners.

'netiquette matters, really.

This is IMO decidedly not a beginner's topic, it is about using C# and Unity for some game functioning.

Still, wanting help on very specialized subjects, C# and Unity, is not likely to get much useful help here at cplusplus.
As I said often here, I used Unity for a long time for GameJams like LudumDare. Except for big projects, I don't want to use it anymore, preferring something lighter. However Unity is a really good engine for beginners and also for "serious" Indie Studio. During the last decade, many games have been created using Unity - and they deserve respect according to the quality (SuperHot, Manifold Garden, CupHead, etc). Furthermore there is a big community of Unity users. Here it is not a good place so as to fix a bug under Unity. The Unity forum is really active and its community is kind willing to help everyone ++
Topic archived. No new replies allowed.