Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by taggartbostian · May 21, 2020 at 06:16 PM · rotationcolliderspritecollidersdirectional

How do you make 4 sided Doom like directional sprites?

I have tried every single thing I tried animator floats that trigger the transitions according to the rotation it would happen at.

All of the maths and scripts line up with the -179 to 179 ratio of rotation. Also it is an enemy that is acting on 3D plane as a 2D object and that means it has a trigger to stop animations once you get near it which means that the attempt to put another childed object that has a collider disabling the not near me animation trigger I have doesn't work because I am shooting with raycasts which will identify itself as hitting the collider of the childed object used to disable the trigger on the enemy so that it can start attacking.

Also the enemy will have 3 hit boxes one on the arm, another on the stomach and one on the head.

In my game you can't jump or rotate the camera up for raycast shooting like in Doom. The problem with adding box colliders to be animation triggers is that the ontriggerenter command I have immediately turns off the not near me trigger so it can attack.


Is there any approach to this I can take or anyway to make directional sprites somebody knows I can use.

The enemy will always face forward once you have disabled the not near me trigger.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by YoannHx · May 22, 2020 at 09:16 PM

Hi. Sorry your post is a bit confuse... I hope I am understanding correctly that your current problem is to display the correct sprite depending on the orientation of the enemy relatively to the camera.

If that's so I would suggest to simply delegate that to the shader!

I was needing some practice with Shader Graph so... I grabbed some sprites and made a shader and a simple scene. alt text alt text

The shader is made for some spritesheets with animation frames along X and angles along Y, you can tune the number of frames and angles (8 is so much more pleasant to the eye than 4 ;-) ), as well as animation speed simply by changing the shader exposed properties (see the use of MaterialPropertyBlock in the attached CharacterController script).

Here's :

  • a short video of the result and

  • the complete Unity project (2019.3.14/URP) (it's a temporary Weshare link, grab it while it's alive! I'll update this post when it's cleaned and uploaded on Github)

Feel free to use (sprite credits in the project files).


In case you prefer to go for the script way, you can do something similar to the original post of this forum thread:

 int GetAngleIndex()
      {
          var dir = GetComponent<Camera>().transform.position - transform.parent.forward;
          var enemyAngle = Mathf.Atan2(dir.z, dir.x) * Mathf.Rad2Deg;
          if (enemyAngle < 0.0f)
              enemyAngle += 360;
          Debug.Log("Angle from the player is: " + enemyAngle);
          if (enemyAngle >= 292.5f && enemyAngle < 337.5f)
              return 8;
          else if (enemyAngle >= 22.5f && enemyAngle < 67.5f)
              return 2;
          else if (enemyAngle >= 67.5f && enemyAngle < 112.5f)
              return 3;
          else if (enemyAngle >= 112.5f && enemyAngle < 157.5f)
              return 4;
          else if (enemyAngle >= 157.5f && enemyAngle < 202.5f)
              return 5;
          else if (enemyAngle >= 202.5f && enemyAngle < 247.5f)
              return 6;
          else if (enemyAngle >= 247.5f && enemyAngle < 292.5f)
              return 7;
          else if (enemyAngle >= 337.5f || enemyAngle < 22.5f)
              return 1;
          else return 0;
      }

You can adjust it to use 4 sprites instead of 8 and use the output of the function to select the correct sprite in your sprite sheet.

Comment
Add comment · Show 1 · Share
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 taggartbostian · May 23, 2020 at 10:00 PM 0
Share

Help! @YoannHx @YoannHx Are you telling me with the script that according that I can apply a script to the enemy to get the angle and then if it is that angle then it returns the number back to the animator variable and I set the animator variables and if statements of the animator accordingly for my angles? I get all of what you're trying to say can you just help me with applying all of the numbers the equation needs properly so it won't have error. In summary I get it I just need to know how to apply it properly if you could help me with that it would be great. Also the Getangleinderx is invalid Please help just a bit more @YoannHx.

avatar image
0

Answer by frilanski · May 22, 2020 at 09:14 PM

If I follow some of the issue (I think you lost me towards the end) I would use the transform.LookAt() to have the 2d sprite face the player at all times like in doom. As for the raycast. You can pass the raycast a layermask. If you put the child object that has the distance collider on a different layer to the enemy that has the hitboxes on then the raycast will ignore the child collider and just hit the enemy.

Hope this helps.

Comment
Add comment · Share
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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

233 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 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

Prevent a collider from affecting the rotation of a sprite,Prevent a collider from affecting players's rotation 1 Answer

Can i Move/Rotate triggers without Rigidbodies? And other collider questions. 3 Answers

How to have a collider inactive only during attack animation and not during 'charging' animation? 1 Answer

I want to attach a collider to the imported animation 1 Answer

sprite rotation 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