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 ItsDJJuju · May 27, 2019 at 07:56 PM · transform.positionraycasthit

Changing the position of an object hit with a RaycastHit

Hey guys so I'm getting sending out a raycast to check if I hit an object with the tag Weapon on it. I then am trying to set the position of the weapon to 0, 0, 0 although for some odd reason it is refusing to work. This is my current code:

 hit.transform.tag = "Weapon1";
                         hit.transform.parent = GameObject.Find("WeaponParent").transform;
                         hit.transform.localPosition = new Vector3(0, 0, 0);
                         hasWeapon1 = true;


Comment
Add comment · Show 6
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 highpockets · May 27, 2019 at 09:33 PM 0
Share

This code looks fine to me. Are you sure the raycast is happening? Have you debugged?

avatar image ItsDJJuju highpockets · May 27, 2019 at 11:26 PM 0
Share

Yes. the debug didn't reveal anything useful and the raycast is happening because everything else in it is working fine. Just the position change is not

avatar image highpockets ItsDJJuju · May 28, 2019 at 07:32 AM 0
Share

Hmm, can you post the rest of the code? It will likely give a clue to what is happening

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by ItsDJJuju · Jun 02, 2019 at 12:57 AM

Instead of getting the object hit and changing the position from that. I changed my code to find the GameObject's tag which I changed to Weapon1 and then changed the position to 0, 0, 0 from there. Changed code: hit.transform.tag = "Weapon1"; hit.transform.parent = GameObject.Find("WeaponParent").transform; GameObject.FindGameObjectWithTag("Weapon1").transform.localPosition = new Vector3(0, 0, 0);

Comment
Add comment · Show 3 · 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 highpockets · Jun 02, 2019 at 01:25 AM 0
Share

But his is the exact same as your original code that you posted, except for changing hasWeapon1 to true

avatar image ItsDJJuju highpockets · Jun 02, 2019 at 12:12 PM 0
Share

Yeah the code in the pastebin and the one I posted above should all be the same other than a few $$anonymous$$or changes. But ins$$anonymous$$d of using hit.transform.localPosition, i changed it to GameObject.FindGameObjectWithTag("Weapon1").transform.localPosition because for some odd reason it refuses to work with hit.transform.

avatar image highpockets ItsDJJuju · Jun 02, 2019 at 06:40 PM 0
Share

That function is pretty expensive to do findgameobjectwithtag, though glad you got it working. Seems like there must be something strange going on.

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

109 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

Related Questions

Select Object and move on Right click Position - Error! 0 Answers

Setting the position of an object which checked the position under with raycast 0 Answers

Transform player to RaycastHit.point depending on speed float 1 Answer

RayCastHit 2 Answers

Why won't my player grab a gameobject 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