Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jul 26, 2018 at 08:26 AM by DeusDeAugur.
avatar image
0
Question by DeusDeAugur · Jul 26, 2018 at 06:30 AM · 2dinstantiate2d gameaimingsword

I'm trying to make it so when I left click, my character's sword slash sprite appears towards the direction of the mouse

This is my code so far:

     [Command]
     void CmdSlash() { 
 
         GameObject go = Instantiate (CurrentAttackPrefab);
         float speedTurn = 10f;
         Vector2 direction = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         float angle = Mathf.Atan2 (direction.y, direction.x) * Mathf.Rad2Deg;
         Quaternion rotation = Quaternion.AngleAxis (angle, Vector3.forward);
         transform.rotation = Quaternion.Slerp (transform.rotation, rotation, speedTurn * Time.deltaTime);
         NetworkServer.Spawn (go);
     }

And this is in my update

 if(Input.GetKeyUp(KeyCode.Mouse0)) {
             CmdSlash();
         }

I'm sure most of this is wrong, I'm trying to put together code from different tutorials and guides. What ends up happening is with each left mouse click, my character will rotate away in some increment, until he is facing 180 degrees away, and the sword slash sprite just spawns at 0, 0, 0. I don't want my character sprite to rotate, i just want the slash sprite to appear towards the direction of the mouse relative to the character. Later I will implement the sprite's arm moving appropriately relative to the direction of the slash, so for now I just want it to not rotate at all. It's also supposed to be online, but I don't think that changes a whole lot of code. Any help or guidance would be greatly appreciated, thanks.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Shameness · Jul 26, 2018 at 08:35 AM 0
Share
  • Ins$$anonymous$$d of Instantiate, attach slash sprite to your pawn, then disable it. When it comes to slash command just enable it again. That way you wouldn't deal with it position because its attached to pawn. Also it is proper way because instantiate is performance poor, and you use it when you need another distinct copy of the object

  • transform.rotation is bound to your pawn, not the instantiated slash sprite, that's why you pawn rotates ins$$anonymous$$d of slash.

Hope these helps.

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

185 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Cannot instantiate rotation on Prefab 1 Answer

How to move Instantiated 2D objects by 0.5 using arrows(or mouse) 1 Answer

How to move Instantiated 2D objects by 0.5 using arrows 1 Answer

Unity 5 - 2d Instantiate at mouse position 1 Answer

Instantiate Prefab dont work 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges