You may use Joints if you want the objects to each have their own physics (Rigidbodies).
Alternately you can just parent a bunch of different colliders together to make a composite single physics object, such as making a hammer out of a cylinder and a cube.
Yes, I finally figured it out. It attaches but not on collision. I still can’t figured it out how the two objects attach together if the collision is detected.
Then set the settings however you want. You can see in the API that all of the settings are exposed (All the same settings that are in the editor):
At the very least, you’ll need to set connectedBody to other object’s rigidbody. You can access this through the collision object that’s passed in to the OnCollisionEnter.
Yes exactly. If I were you, though, I would only make one joint for the whole thing. Making a joint for every contact point seems like it might be needlessly complicated and I wouldn’t be surprised if the physics engine has trouble with it.
I assume this attached to your player? OnCollision can be fired-off whenever the player collides with any collider. Could be a wall or the ground, or any other object. Before you add the joint, you want to check and make sure that hit.gameObject is actually the ball that you want attached and not something else. You can do this with a hit.gameObject.CompareTag(), for example.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
private Ball ballAttachedToPlayer;
public Ball BallAttachedToPlayer { get => ballAttachedToPlayer; set => ballAttachedToPlayer = value; }
// Start is called before the first frame update
And this for the Ball:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ball : MonoBehaviour
{
private bool stickToPlayer;
[SerializeField] private Transform transformPlayer;
float speed;
Vector3 previousLocation;
Player scriptPlayer;
public bool StickToPlayer { get => stickToPlayer; set => stickToPlayer = value; }
// Start is called before the first frame update
void Start()
{
scriptPlayer = transformPlayer.GetComponent<Player>;
}
// Update is called once per frame
void Update()
{
if (!StickToPlayer)
{
float distanceToPlayer = Vector3.Distance(transformPlayer.position, transform.position);
if (distanceToPlayer < 0.5)
{
stickToPlayer = true;
scriptPlayer.BallAttachedToPlayer = this;
}
}
}
}
It gives me this error: “Cannot convert method group ‘Identifier’ to non-delegate type ‘Player’. Did you intend to invoke the method?”
I finally made it when player collide with ball the ball attaches to the player. Now I want the player kick the ball. I’ve arranged this stack of code that I attached it to Player:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShootBall : MonoBehaviour
{
private Ball ballAttachedPlayer;
[SerializeField]
public Ball BallAttachedToPlayer { get => ballAttachedPlayer; set => ballAttachedPlayer = value; }
void Update()
{
if (Input.GetKey("k"))
{
if (ballAttachedPlayer != null)
ballAttachedPlayer.StickToPlayer = false;
Rigidbody rigidbody = ballAttachedPlayer.transform.gameObject.GetComponent<Rigidbody>();
rigidbody.AddForce(transform.forward * 20f, ForceMode.Impulse);
ballAttachedPlayer = null;
}
}
}
And I have this attached to Ball object:
public class Ball : MonoBehaviour
{
[SerializeField] private Transform transformPlayer;
private bool stickToPlayer;
private Transform playerBallPosition;
float speed;
Vector3 previousLocation;
ShootBall shootBall;
public bool StickToPlayer;
// Start is called before the first frame update
void Start()
{
playerBallPosition = transformPlayer.Find("BallLocation");
shootBall = transformPlayer.GetComponent<ShootBall>();
}
// Update is called once per frame
void Update()
{
if (!stickToPlayer)
{
float distanceToPlayer = Vector3.Distance(transformPlayer.position, transform.position);
if (distanceToPlayer < 0.5)
{
stickToPlayer = true;
shootBall.BallAttachedToPlayer = this;
}
}
else
{
Vector2 currentLocation = new Vector2(transform.position.x, transform.position.z);
speed = Vector2.Distance(currentLocation, previousLocation) / Time.deltaTime;
transform.position = playerBallPosition.position;
transform.Rotate(new Vector3(transformPlayer.right.x, 0, transformPlayer.right.z), speed, Space.World);
previousLocation = currentLocation;
}
}
}
The ball doesn’t get kicked in runtime. It gives this error: “Object reference not set to an instance of an object (at ShootBall.cs:28).”
What should I do.