DRAG & SHOOT in Unity | 2D Game Dev Tutorial

DRAG & SHOOT in Unity | 2D Game Dev Tutorial

Muddy Wolf

4 года назад

63,090 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Jorge Ponce
Jorge Ponce - 28.08.2023 23:21

Hello! Thank you very much for the tutorial! I'm having issues when I set the camera to perspective mode. It only works correctly if it's in orthographic mode. Has anyone else experienced this? How can I solve it?

Ответить
LorshDev
LorshDev - 29.06.2023 01:34

Followed the video and on finish the line renderer seems like it's attached to the ball object, so when i drag it spawns the line next to the ball rather than where I click. Did I miscode it or miss box tick in a menu or something?

Either way awesome video :) thanks for putting it out

Ответить
BAE-2837 Mars
BAE-2837 Mars - 20.06.2023 14:55

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerScript : MonoBehaviour
{

public float power = 10f;
public Rigidbody2D rb;

public Vector2 minPower;
public Vector2 maxPower;

Camera cam;
Vector2 force;
Vector3 startPoint;
Vector3 endPoint;

private void Start()
{
cam = Camera.main;

}

private void Update()
{
if (Input.GetMouseButtonDown(0))
{
startPoint = cam.ScreenToWorldPoint(Input.mousePosition);
startPoint.z = 15;
Debug.Log(startPoint);
}

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);
}
}


}

Ответить
КУМАР
КУМАР - 26.04.2023 22:13

Неверно рассчитывается вектор импульса (46 строка)

Ответить
Abhinaw Ram
Abhinaw Ram - 19.02.2023 19:00

On dragging it actually control the ball throwing force like( if mouse dragging length is short the force applied on the ball is low competitive the longer drag).

Ответить
jakeyyyyyyyy
jakeyyyyyyyy - 25.01.2023 20:20

My left ear really enjoyed that one

Ответить
Flimzy
Flimzy - 12.12.2022 11:09

If you want to make it so you can only drag when the object is still, Then the DragNShoot script will be this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Movement : MonoBehaviour
{
public float power = 10f;
public Rigidbody2D rb;

public Vector2 minPower;
public Vector2 maxPower;

TragectoryLine tl;

Camera cam;
Vector2 force;
Vector3 startPoint;
Vector3 endPoint;

bool isStill = true;

private void Start()
{
cam = Camera.main;
tl = GetComponent<TragectoryLine>();
}

private void Update()
{
if (rb.velocity == new Vector2(0, 0))
{
isStill = true;
}
else
{
isStill = false;
}

if (Input.GetMouseButtonDown(0) && isStill == true)
{
startPoint = cam.ScreenToWorldPoint(Input.mousePosition);
startPoint.z = 15;
}

if (Input.GetMouseButton(0) && isStill == true)
{
Vector3 currentPoint = cam.ScreenToWorldPoint(Input.mousePosition);
currentPoint.z = 15;
tl.RenderLine(startPoint, currentPoint);
}

if (Input.GetMouseButtonUp(0) && isStill == true)
{
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);

tl.EndLine();


}
}
}

Here I created a bolean called "isStill".
And in the void Update() I made isStill = true if the velocity of the rigid body is = new Vector2(0, 0)
And if its not (else), then isStill = false.
And then for each if statement we made I added (&& isStill ==true) to make sure the object is still to be able to perform this action

Ответить
Johannes
Johannes - 09.12.2022 20:17

What if I want to check is gameObject still? (Cant shoot mid air)...

Ответить
Rootbin
Rootbin - 06.12.2022 21:51

I subscribed

Ответить
Squarerians
Squarerians - 12.11.2022 19:29

i want to add cooldown for this shooting thing. please answer and tell us how we can add cooldown for this game please

Cheers
-your lovely followers :)

Ответить
Jim Mayal
Jim Mayal - 15.07.2022 03:57

Dummy question here: I have a ball with 9 child bones( to create the soft body effect). Initially, the script applied only to the rigid body "Ball" had no effect, so I've added the same script also to each bone and it works perfectly. With that said, is there another way of doing it? Not sure if that's the most lean way. Thanks

Ответить
Fraser
Fraser - 21.04.2022 18:40

Thank You For The Tutorial! I Am Working On A Stickman Game And With This I Can Make It Have Unique Movement

Ответить
Pasante0
Pasante0 - 19.04.2022 01:27

idk if you are gonna respond to this because it's been 2 years sience you made this video

but the tutorial is great i just have an issue with it: the players can spam it and they can fly so how do i make like a cooldown system or that if the player stops moving then they can move again or something?

Ответить
SimpleDeck
SimpleDeck - 24.03.2022 15:15

The script worked but the effect would only show a dot

Ответить
GV Gaming
GV Gaming - 06.03.2022 00:37

Great Tutorial! But unfortunetly it didn't work for me but you know it's okay, It was good explaining and atleast i learned alot with input, Thank you!

Ответить
Катюша
Катюша - 04.03.2022 23:38

I can't thank you enough, it works, figured the first time didn't work because I got some lines wrong

Ответить
GTS | GupatikTheSuperstar
GTS | GupatikTheSuperstar - 18.02.2022 21:32

I just don't understand what's the first parameter in the Mathf.Clamp( ) function and I don't get the use of it. Otherwise, everything else is clear.
Thank You!

Ответить
G8torx
G8torx - 29.01.2022 18:58

When I try to add another line renderer for my player to make it so it has a trail while its moving and its not working. Does anyone know how to fix this?

Ответить
SmashBros4339
SmashBros4339 - 19.11.2021 13:30

Also, anytime I drag and shoot and collide with 2 or 3 box collider platforms, the drag and shoot doesnt work. Any fix?

Ответить
Shav
Shav - 15.11.2021 10:52

Your video helped me to start developing a game that will be amazing. When I finish and upload it on steam you will get it for free.

Ответить
Maddhi
Maddhi - 01.11.2021 01:19

Ive 1 to 1 followed the tutorial. The ball doesnt shoot. Help

Ответить
AlexDev
AlexDev - 19.10.2021 13:46

For some reason didn't work for me . Debug force showing (0,0,0)

here is my code: , I think it's the same as on video


public class dragNShoot : MonoBehaviour
{
public float power;
public Rigidbody2D rb;

public Vector2 minPower;
public Vector2 maxPower;

Camera cam;

Vector2 force;
Vector3 startPoint;
Vector3 endPoint;

private void Start()
{
cam = Camera.main;
}

private void Update()
{
if(Input.GetMouseButtonDown(0))
{

startPoint = cam.ScreenToWorldPoint(Input.mousePosition);
startPoint.z = 15;

}

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);
Debug.Log(force);
}
}
}
**

Ответить
Simon Triulzi
Simon Triulzi - 14.10.2021 01:54

so i was wondering, how do you make it so you can only use it a certain amount of times when you are in the air and when you touch the ground it resets?

Ответить
Mark Star
Mark Star - 18.09.2021 12:10

How do I attach the line to the ball so no matter where a touch,it looks like i started from the ball

Ответить
sali
sali - 09.09.2021 23:51

Awesome tutorial.
First thing i will try tomorrow morning after my round of golf.

Ответить
Vraj Soni
Vraj Soni - 23.08.2021 18:55

Thanks a lot.

Ответить
Subha Ranjan Ghoshal
Subha Ranjan Ghoshal - 21.08.2021 18:35

This was definitely useful. Thanks a lot for creating this tutorial.

Ответить
Orkhan Haddad
Orkhan Haddad - 12.08.2021 15:55

this didn't work for me and i have no idea what to do he tell me the cam doesn't exist

Ответить
Ferfelekis Totoridis
Ferfelekis Totoridis - 01.08.2021 01:10

nice tutorial, but how about on a 2d project with perspective camera?

Ответить
Fares Tutorials
Fares Tutorials - 25.07.2021 20:18

Force = Vector2(Mathf.Clamp(startPoint.x - endPoint.x, minPower.x, maxPower.x), Mathf.Clamp(startPoint.y - endPoint.y, minPower.y, maxPower.y));, this is and error it says u cant use VEctor 2 like a method.

Ответить
Rey A.
Rey A. - 24.07.2021 08:16

Awesome tutorial!

Ответить
Tarek Alhafi
Tarek Alhafi - 14.07.2021 02:24

How to make it limited like 3 shots then its gameover

Ответить
Xyzed1000
Xyzed1000 - 13.07.2021 23:35

A great video! Just what I'm looking for, thanks!!

Ответить
CodeBlues
CodeBlues - 08.07.2021 06:03

It's a 3D project, but we can change the z value to y value. Thank you very much for making a very useful tutorial. I wish you happiness.

Ответить
SmashBros4339
SmashBros4339 - 28.06.2021 14:02

Great Vid! But is there a way you can drag and shoot once until you hit the ground so you won't keep shooting up in the air?

Ответить
Magikalo
Magikalo - 22.06.2021 00:59

OMG thank you so much, i've tried to do this for 3 days and finally it's working, thank you !

Ответить
Organ L
Organ L - 05.06.2021 01:22

thank you so much Muddy Wolf games. This was really helpful for me!

Ответить
Matthew Gruman
Matthew Gruman - 22.04.2021 23:24

Great video - thanks!

Ответить
Edwin
Edwin - 27.03.2021 18:27

Thanks

Ответить
Mr. Waddles202
Mr. Waddles202 - 10.02.2021 00:08

Hey excellent video but I have a question
How do you get the autocomplete and definition thing for unity because when I type it doesn't show all the options you have

Ответить
khalid mokhliss
khalid mokhliss - 18.01.2021 02:54

heemmmmm why the ball do not move in my variant

Ответить
trkkk matmat
trkkk matmat - 11.01.2021 21:06

Why my player jump in only y axis

Ответить
Eric Russell
Eric Russell - 23.12.2020 06:43

Can someone help for some reason the ball only goes in on direction

Ответить
Alexandru Aioanei
Alexandru Aioanei - 15.12.2020 10:24

Hey man, I've really liked your tutorial and it helped me a lot; but can you help me with something? After I added animation to the script the ball only jumps straight in the air, it doesn't jump on the right or left part, do you know any solution for this? Thanks in advance!

Ответить