Question by
AkagamiSoul · May 24 at 02:27 PM ·
unity 2dlinerenderermousepositioncurves
Create a curve with the mouse at the apex of the curve ?
(For context: 2d plateformer game with a stone(projectile) that light up everything when shooted)
Hey so it's been a long time I wanted to do this, but I can't resolve it by myself so i'am asking all of you.
I want the to have the screen positon of the mouse to define the apex of the curve that will shoot a projectile.
So i've made this but impossible for me to do what I want... any Idea ?
public float MouseX = 0f;
public float MouseY = 0f;
public Transform Heropos;
private Camera _cam;
public LineRenderer lr;
void Start()
{
_cam = Camera.main;
}
public void Update()
{
Vector2 MousePosWorld = Camera.main.ScreenToWorldPoint(Input.mousePosition); //mouse world positon
Vector2 MousePosScreen = Input.mousePosition; //mouse positon by the leftbottom of the screen
transform.position = new Vector2(MousePosWorld.x, MousePosWorld.y);
MouseX = (MousePosScreen.x + (_cam.pixelWidth / 2)); //find the center of screen
MouseY = (MousePosScreen.y -+(_cam.pixelHeight / 2)); // same
Vector3 position = new Vector2(Heropos.position.x, Heropos.position.y);
Vector3 velocity = new Vector2(MouseX, MouseY);
List<Vector3> path = new List<Vector3>();
float duration = 300f;
float timestep = Time.fixedDeltaTime;
for (float t = 0f; t < duration; t += timestep)
{
velocity += Physics.gravity * timestep;
position += velocity * timestep;
path.Add(position);
}
if (Time.timeScale == 1)
{
lr.positionCount = path.Count;
lr.SetPositions(path.ToArray());
}
}
i've already a projectile behavior script and I think I just need the coordinates of my mouse to create a vector2 for it's velocity. (tell me if I'm wrong but that not what I want to be help with RN)
Thanks ya (And sorry for the bad english i'm not anglophone)
Comment