- Home /
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!
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;
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.
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.
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;
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.
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
![](https://koobas.hobune.stream/wayback/20220613195847im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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