I think the variable *distance* should really be called *direction* for clarity. Sorry about that :)
@dragontailsb19712 жыл бұрын
if you want you can also use these 4 lines that are much more simple: private void Update(){ Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position; difference.Normalize(); float rotation_z = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg; transform.rotation = Quaternion.Euler(0f, 0f, rotation_z + offset); }
@dragontailsb19712 жыл бұрын
ps. if you want to add the special scale change add this: Vector2 scale = transform.localScale; if(Mathf.Abs(rotation_z) > 90) { scale.y = -1; }else if(Mathf.Abs(rotation_z) < 90) { scale.y = 1; } transform.localScale = scale;
@srkydev57272 жыл бұрын
@@dragontailsb1971 You guys are amazing! I didn't even need to watch the vid 🤣🤣
@dragontailsb19712 жыл бұрын
@@srkydev5727 thank you!
@vheath47172 жыл бұрын
@@dragontailsb1971 Thank you guys!
@dragontailsb19712 жыл бұрын
@@vheath4717 no problem
@lonewanderer63332 жыл бұрын
Awesome stuff. You are a legend Peter! Really appreciate what you have been doing for us beginner developers!
@SunnyValleyStudio2 жыл бұрын
Hey! Thanks a lot! I'm really glad that you like my vids 🙂
@durrium2 жыл бұрын
Very well made as always!
@Eaaaaaattmyasss Жыл бұрын
Thank you for the tutorial , i was having issues at first trying to get the weapon to rotate on my mouse pointer and it was rotating around my player when i was walking instead but in the end i managed to get it working because of a comment by DragonTailSB
@SunnyValleyStudio Жыл бұрын
Sorry to hear that. Glad that you were able to work things out!
@oarl13302 жыл бұрын
Hello @SunnyValleyStudio. Can you show how you animated your character in the playlist about the new input system?
@deniscooper092 жыл бұрын
Good, thanks for tutorials!
@JakeMakesGames Жыл бұрын
can you make a tutorial on how to do this with a joystick using the new input system please :)
@uwinhong28 күн бұрын
Thanks for your tutorial, but I found that when rotate, jaggedness occurs in pixel weapon. Is there a way to fix this🤔
@SunnyValleyStudio25 күн бұрын
I think you could fix that by using docs.unity3d.com/Packages/com.unity.2d.pixel-perfect@1.0/manual/index.html
@yutsuneko2 жыл бұрын
Nice tutorial as always!
@falier5795 Жыл бұрын
Thank you so much for the tutorial, really helped a lot !
@SunnyValleyStudio Жыл бұрын
Glad it helped!
@TiagoMaiaPereira11 ай бұрын
I have a question about the tutorial: I am trying to make my game have inputs for both keyboard/mouse, and a gamepad. The mouse rotates the weapon just fine, the problem comes with the input for the gamepad. How would you make it so the weapon would rotate with the Right Stick of a gamepad?
@SunnyValleyStudio11 ай бұрын
So if you are designing the game for mobile / console I would just make the weapon point towards an "invisible cursor" that we would offset from the player positions using the direction in which your right stick is pointing in. So when you move your right stick what you get as an input is the direction vector (representing the direction in which your are pushing the stick). You could send it directly to your weapon as the direction in which our weapon should be rotated. I hope it makes sense.
@whyno6807 Жыл бұрын
you are my hero
@SunnyValleyStudio Жыл бұрын
Glad I could help!
@artohaapanen7764 Жыл бұрын
Awesome stuff. How does the Get Pointer Input method of the Player class change if I use a virtual camera?
@SunnyValleyStudio Жыл бұрын
Hey! What do you mean? If you are using the Cinemachine camera it still references the Main scene Camera. In general you need to grab the camera that is currently acting as the "main" camera for it to work.
@artohaapanen7764 Жыл бұрын
@@SunnyValleyStudio ok
@Antdromeda Жыл бұрын
It says that "The type or namespace name "WeaponParent" could not be found (are you missing a using directive or an assembly reference?)" Please help.
@SunnyValleyStudio Жыл бұрын
It means that you don't have the script called "WeaponParent" in your scripts. Maybe check the name of the script and the class.
@mikebb79996 ай бұрын
The series is very nice and usefull, but you have made stuff before, then you add something only to remove it in the next video, then add it again... It is a bit difficult to follow the tutorials in the yt playlist order.
@SunnyValleyStudio6 ай бұрын
Sorry about that. I was trying a different approach where i don't show everything but just the specific feature and assume that you know how to use this code. Thanks for the feedback
@Bruno-vn3qq Жыл бұрын
muuuuito foda, valeu meu mano
@minima_studios Жыл бұрын
for some reason, my sortingorder will keep counting up or down base on the rotation
@neppallv6 ай бұрын
How do you control the mouse using the right joystick on a game controller?
@SunnyValleyStudio6 ай бұрын
I think that in the new input system you can get the joystick input directly as a Vector2. This is the same as the input from your arrow keys. Just normalize it and pass it to our functions
@senordygus9197 ай бұрын
Followed your tutorial, for some reason I'm getting a null reference error, any tips?
@SunnyValleyStudio7 ай бұрын
Null just indicates that we haven't set something. Maybe its as simple as an empty field in the inspector? Or a missing GetComponent() in a script?
@aimeeharper5130 Жыл бұрын
Hey, does anyone know how I could equip / un-equip my weapon simply? Thanks in advance for help.
@Ananzi372 Жыл бұрын
You can make weapon active when you press the input and make a boolean false or true when you press the input if bool is true or false before attack lines
@Blue_Grego4 ай бұрын
How do you get the character to flip with the mouse?
@SunnyValleyStudio4 ай бұрын
You can use DOT product using the up direction and the direction from player to mouse position. Just flip the X (I think) axis of rotation (transform component) of the character gameobjects.
@itsPersonal921 Жыл бұрын
When I flip the player to the -z the weapon rotates and rotate incorrectly. How would I change this? Do I have to add a animation?
@SunnyValleyStudio Жыл бұрын
Sorry about that. I think at 8:07 I showed that we are modifying the scale of the weapon but in the script we are indeed affecting the scale of the WEAPON TRANSFORM. Maybe because of this you have added WeaponParent script on the weapon hence your trouble with rotation?
@itsPersonal921 Жыл бұрын
@@SunnyValleyStudio Thank this got it working! :)
@rigbard48252 жыл бұрын
you are good 👍
@SunnyValleyStudio2 жыл бұрын
Thanks 😁
@twisky3751 Жыл бұрын
how are you making the character face the mouse?
@SunnyValleyStudio Жыл бұрын
You just calculate the direction from the player to mouse and if X is > 0 face RIGHT if
@itsPersonal921 Жыл бұрын
@@SunnyValleyStudio But how?
@itsPersonal921 Жыл бұрын
did you find a solution, becuase Im having the same problem
@angelajohnson5509 Жыл бұрын
@@itsPersonal921 Went I change the direction of my player on the scale, the weapon goes weird and doesn't follow my mouse any more. I goes to the opposite direction. How do I fix this?
@twisky3751 Жыл бұрын
@@itsPersonal921 yeah heres my script: float direction = Mathf.Sign(mouse.transform.position.x - transform.position.x); transform.localScale = new Vector3(direction, transform.localScale.y, transform.localScale.z); Basically what this does is gives you either a value of 1 or -1 based on if the mouse is to the right or left of the player, and then sets the scale to be that number. Unfortunately this means you can't have your weapon be parented to the player because it will flip that too and the rotation will look weird, so put this in your weapon script: transform.position = new Vector2(player.transform.position.x, player.transform.position.y); and it will act like it is parented to the player.
@gamedevbaiyi9362 жыл бұрын
Awosome.
@tarkerro2 жыл бұрын
How to rotate sprite without distortion? My sprite is turning into a mess on rotating
@SunnyValleyStudio2 жыл бұрын
Are you sure you have the sprite file in the Project section to "Filter Mode -> Point(no filter)" ? It might be that or search for Pixel Perfect camera but I doubt it is the issue here.
@Llamit42 жыл бұрын
@@SunnyValleyStudio Same issue is happening to me, when i start rotating my sprite it gets all compressed. Also happens only while being a child of the player, when you make it a stand alone object it works fine
@Llamit42 жыл бұрын
Nevermind fixed it
@ind1ewave2 жыл бұрын
@@Llamit4 might be a little late but how did you fix it?
@Blue_Grego2 ай бұрын
@@Llamit4 how?
@jackeygaming74542 жыл бұрын
Can you make this in unity bolt
@angelajohnson5509 Жыл бұрын
when i change the scale the weaoong points the oposite way
@SunnyValleyStudio Жыл бұрын
Sorry about that. I think I have messed up the explanation arount 8:07 since we want to change the scale of the WEAPON PARENT object not the weapon. Sorry about that!
@Shergon_41k4 ай бұрын
How to do this in 3d?
@SunnyValleyStudio3 ай бұрын
I will be adding more 3d projects to the channel! In 3D its a bit more challenging because you have a quaternion based rotation due to 3rd dimension. It needs clamping so you don't do 360 rotation Up/down. I will try to post a video about it soon!
@Shergon_41k3 ай бұрын
@@SunnyValleyStudio I managed to do it in 3D, but I would watch your video about it.
@tredutore Жыл бұрын
what happens you want to switch weapons
@SunnyValleyStudio Жыл бұрын
Hey! To solve this it would be best to just pass the AimDirection to the weapon and it should process it. So a sword would be at 90 degree angle so that we can swing it while a gun would be aiming its barrel at this direction and an attack button would just trigger the shooting. It would require us to spli this system into more granular parts /objects. I hope it answers your question.
@Lohimies2 ай бұрын
There is a weird flip of the weapon at 180 degrees that assigns the 180 to rotation.y. Fixed it with a snippet of code: if (transform.eulerAngles.y == 180 || transform.eulerAngles.y == -180) { transform.rotation = Quaternion.Euler(0,0,180); } Why is this happening? The solution I found on unity discussions forum explained that it has something to do with transform.right.
@SunnyValleyStudio2 ай бұрын
Thanks for pointing that out and sharing the solution 👍