Hey,
I am making a small game where you control a forklift and compete to pick up pallets and bring them back to your dock. I am having some trouble with the pallet swinging/sliding off the front of the tines because of the momentum of turning the forklift at a high speed. The physics seems to be working fine but I would like to fix the pallet more to the tines so it isn’t as easy to drop the pallet. I have tried increasing the drag and angular drag but it ends up slowing the forklift down majorly. I have looked at swapping the pallet to “is Kinematic” when the pallet is picked up but it causes more problems along with slowing the forklift down. I have about 9 child empties with box colliders to make up the shape as non-convex mesh colliders don’t work with non Kinematic objects. I don’t know if that is causing problems. I have the pallet parenting to the tines when it is picked up but it still slips off the front.
The optimal final output is the pallet sticks to the tines no matter how the forklift drives around but if the forklift runs into a wall with the pallet lifted above the wall, the pallet can slip off into the ‘goal’. Sorry if that is too much to ask haha, I am quite new to programming and just kind of stumbling my way through. Thank you.
For the first part, to prevent it from sliding off right, but sliding off when enough force is applied, static friction is the field you want to change.
Now that I know that you are parenting the object to the forklift, there is a better solution using the parent method. You can change the rigid body to kinematic when its parented and when the forklift hits the section you want it to drop into to. Detect the collision with the wall using a trigger on the front of the forklift and when this happens, turn the rigid body back to dynamic and add a force to propel the pallet off of the tines. (you can scale the force with forklift velocity to give a more realistic effect. Here is the first pseudocode for the forklift
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ForkliftController : MonoBehaviour
{
public GameObject currentPallet;
public float releaseForce = 10;
public void releasePallet()
{
//Set parent transform reference to null
currentPallet.transform.parent = null;
Rigidbody rigidBody = currentPallet.GetComponent<Rigidbody>();
//enable dynamic rigid body.
rigidBody.isKinematic = false;
//You might need to add or subtract from this angle if the pallet doesnt get pushed in the right direction
float angle = gameObject.transform.eulerAngles.y;
//Add a force to the pallet to send it of the tines
//If you want, you can multiply release force with forklift velocity
rigidBody.AddForce(new Vector3(Mathf.Sin(angle) * releaseForce,0, Mathf.Cos(angle) * releaseForce));
}
public void grabPallet(GameObject pallet_)
{
//Set Reference to pallet
currentPallet = pallet_;
//Disable dynamic rigid body
pallet_.GetComponent<Rigidbody>().isKinematic = true;
//Set its parent to the forklift
pallet_.transform.parent = gameObject.transform;
}
}
Then you want to create a game object with a trigger on it and place it on the front of the forklift where it will collide with the wall. Then add this script and call the releasePallet() from it.
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class CollisionTriggerController : MonoBehaviour
{
public void OnCollisionEnter(Collision collision)
{
MyOwnEventTriggered();
}
//my event
[System.Serializable]
public class MyOwnEvent : UnityEvent { }
[SerializeField]
private MyOwnEvent myOwnEvent = new MyOwnEvent();
public MyOwnEvent onMyOwnEvent { get { return myOwnEvent; } set { myOwnEvent = value; } }
public void MyOwnEventTriggered()
{
onMyOwnEvent.Invoke();
}
}
Feel free to comment more if this does not work or you have some more questions! Good luck with the project.