public override void OnWagonActivate(Wagon wagon, ActivateInfo info) { var localHit = wagon.transform.InverseTransformPoint(info.hit.point); var mag = wagon.rigidbody.mass * trainImpulseSpeed * (1.0f / Time.deltaTime); if (localHit.z < 0) { mag = -mag; } // Push wagon wagon.ApplyForce(mag); }
private void Update() { if (other != null) { yawTransform.LookAt(other.yawTransform); // Apply impact forces at slack extremes var distanceBetween = Vector3.Distance(yawTransform.position, other.transform.position) - transform.localPosition.z; var factor = other.wagon.isKinematic ? 1.0f : 2.0f; if (distanceBetween < 0) { // Push wagons away from each other // forward is towards the other wagon, however distancebetween is negative, so the force goes backwards wagon.ApplyForce(yawTransform.forward * (distanceBetween / (Time.deltaTime * factor)) * wagon.rigidbody.mass); } else if (distanceBetween > maxSlack) { // Pull wagons towards each other wagon.ApplyForce(yawTransform.forward * ((distanceBetween - maxSlack) / (Time.deltaTime * factor)) * wagon.rigidbody.mass); } } }