Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by unity_120007130 · Dec 14, 2020 at 01:12 AM · transformpositionbeginnerparentoverlap

Move Overlapbox/GameObject depending on Parent

So I've been making a 2D Top-Down RPG, and am currently figuring out how to do the combat. Using tutorials, I chose to do this:

 Collider2D[] enemiesToDamage = Physics2D.OverlapBoxAll(attackPos.position, new Vector2(attackRangeX, attackRangeY), 0, whatIsEnemies);
         for (int i = 0; i < enemiesToDamage.Length; i++)
         {
             enemiesToDamage[i].GetComponent<EnemyHealthManager>().TakeDamage(damage);
         }

However, I would like to move the "hitbox" (called "AttackPos"), depending on the direction the character is looking. How can I put this into my scripts? I also made a string called "direction" on my PlayerMovement script, but I don't know how to proceed.

Also, how can I rotate the OverlapBoxAll when looking to the left/right?

Sorry for any English mistakes and if this is a stupid question, I'm stuck :(

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
0

Answer by wertyq · Dec 14, 2020 at 04:31 AM

You can rotate the parent in the PlayerMovementScript by using Quaternion.LookRotation().

Comment
Add comment · Show 5 · 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 unity_120007130 · Dec 14, 2020 at 04:34 AM 0
Share

Do you know how I can change the position of the Empty Gameobject? If I could learn how to move it by, i.e., 5pixels to the right, and rotate it, that would help me a lot

avatar image wertyq unity_120007130 · Dec 14, 2020 at 04:40 AM 0
Share

Is the empty gameobject the parent of your hitbox and a child of your player?

avatar image unity_120007130 wertyq · Dec 14, 2020 at 04:43 AM 0
Share

I don't know what you mean by "parent of your hitbox"

The empty gameobject is a child of my player

The hitbox is basically the position of this empty gameobject, with an attackRange I can set myself

I just need to know how to move that empty gameobject relative to my player :(

Show more comments
avatar image
0

Answer by wertyq · Dec 14, 2020 at 05:14 AM

Great! So, from your PlayerMovement script, you can access your child Game Object by using

 void Start()
 {
 GameObject childGameObject = transform.Find("Name of your child game object");
 }

The code below this goes under Update(), might be a good idea to put this inside your if (movement != Vector2.zero) block

 if (movement.x > 0)
 {
     childGameObject.transform.localPosition = new Vector3 (5,0);
 }
 
 if (movement.x < 0)
 {
     childGameObject.transform.localPosition = new Vector3 (-5,0);
 }
 
 if (movement.y > 0)
 {
     childGameObject.transform.localPosition = new Vector3 (0,5);
 }
 
 if (movement.y < 0)
 {
     childGameObject.transform.localPosition = new Vector3 (0,-5);
 }

Also, might want to consider changing your EmptyGameObject name to something like, I don't know, AttackPosHolder. So, instead of childGameObject, its easier to read your code in the future by using the name attackPosHolder.

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

219 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

Related Questions

Following another object's position/rotation like parent/child relationship? 4 Answers

parent Transform.position unexpected behaviour when changing position 1 Answer

Controlling parent by Inputs 1 Answer

Transform position in editor confusion (screenshot examples) 1 Answer

Moving parent to position of child 0 Answers


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