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
0
Question by $$anonymous$$ · May 18, 2015 at 10:16 AM · jointsfixedupdatejointquaternions

Connecting gameobject to specific part of player

Right now I have a gameobject that attaches to a player when they touch it, however I'm trying to make it connect right in front of them instead of wherever it initially touches them:

 void OnCollisionEnter(Collision collision) 
 {
     ContactPoint contact = collision.contacts[0];
     Quaternion rot = Quaternion.FromToRotation(Vector3.up, contact.normal);
     //Vector3 pos = contact.point; //(-2.5, 0.7, 13.1) is near ideal location
     Vector3 pos = new Vector3(-2, 1, 13); //(-2.5, 0.7, 13.1) is near ideal location
     var joint1 = collision.gameObject.AddComponent<FixedJoint> ();
     joint1.connectedBody = transform.GetComponent<Rigidbody> ();

}

Is there a way to have it connect right in front of the player each time?

Thanks!

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
Best Answer

Answer by $$anonymous$$ · May 21, 2015 at 09:25 AM

Thank you for the help.

I kept trying every variation of forward. This is the solution that worked:

         var new_pos = collision.gameObject.transform.position;
         new_pos += collision.gameObject.transform.forward * 2;
         new_pos += collision.gameObject.transform.up * 1;
         transform.position = new_pos;


         var joint1 = collision.gameObject.AddComponent<FixedJoint> ();
         joint1.connectedBody = transform.GetComponent<Rigidbody> ();
         soccer_ball_joint = joint1;
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
avatar image
1

Answer by FortisVenaliter · May 18, 2015 at 06:18 PM

As far as I can tell, you're not setting the transform's position anywhere. Set it to whatever you want after you parent the object. If it doesn't receive a new position, it will just use the old one.

Comment
Add comment · Show 4 · 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 $$anonymous$$ · May 19, 2015 at 10:21 AM 0
Share

It seems to act very strangely when I do this:

         var joint1 = collision.gameObject.AddComponent<FixedJoint> ();
         joint1.connectedBody = transform.GetComponent<Rigidbody> ();
         soccer_ball_joint = joint1;
         transform.localPosition = collision.gameObject.transform.position;
         transform.localEulerAngles = collision.gameObject.transform.localEulerAngles;
         Vector3 temp_pos = new Vector3(transform.position.x, transform.position.y, transform.position.z+1);
         transform.localPosition = temp_pos;

When anyone touches it they sometimes go flying in the air like the gameobject isn't going to the correct place.

avatar image $$anonymous$$ $$anonymous$$ · May 19, 2015 at 06:54 AM 0
Share

This code makes any character that touches it go flying in the air sometimes. Like the gameobject doesn't attach at z+1.

         var joint1 = collision.gameObject.AddComponent<FixedJoint> ();
         joint1.connectedBody = transform.GetComponent<Rigidbody> ();
         soccer_ball_joint = joint1;
         transform.localPosition = collision.gameObject.transform.position;
         transform.localEulerAngles = collision.gameObject.transform.localEulerAngles;
         Vector3 temp_pos = new Vector3(transform.position.x, transform.position.y, transform.position.z+1);
         transform.localPosition = temp_pos;
avatar image FortisVenaliter · May 19, 2015 at 02:09 PM 0
Share

Yes, because you're setting the transform.localPosition (local to the parent) to the transform.position (world position), so you're using entirely different contexts for those two vectors.

If you want it to be in front, set the localPosition to something like Vector3.forward.

avatar image $$anonymous$$ · May 19, 2015 at 09:52 PM 0
Share

I've tried every variation I can find in the forums and answer section with Vector3.forward and localposition, but each one will not place the ball in front of the player. This seems like it should be so simple, and I just am not grasping it. If you have a gameobject.transform, what result will give you z+1 in front of that? (If that makes sense). Thanks again, I really appreciate your help in this one, it's been annoying me for weeks.

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

1 Person is following this question.

avatar image

Related Questions

Attaching game objects with joints - Matching angles 0 Answers

How to make Hinge/Distance Joint 2D not break away from its anchor 1 Answer

Joints placed incorrectly after FBX import from Maya 1 Answer

Why do my rope behave strange? Stretching rope problem. 0 Answers

Connect rigidbody to another moving rigidbody to simulate a hanging weight 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