Beispiel #1
0
        private void AttachTrailer(VehicleController vc)
        {
            if (_nearestTrailerModule != null)
            {
                _trailer = _nearestTrailerModule;
                if (_trailer == null)
                {
                    return;
                }

                VehicleController trailerVc = _trailer.VehicleController;

                // Position trailer
                trailerVc.vehicleTransform.position = trailerVc.transform.position -
                                                      (_trailer.attachmentPoint.transform.position -
                                                       attachmentPoint.transform.position);

                // Configure joint
                _configurableJoint = trailerVc.gameObject.GetComponent <ConfigurableJoint>();
                if (_configurableJoint == null)
                {
                    _configurableJoint = trailerVc.gameObject.AddComponent <ConfigurableJoint>();
                }

                _configurableJoint.connectedBody = vc.vehicleRigidbody;
                _configurableJoint.anchor        =
                    trailerVc.transform.InverseTransformPoint(attachmentPoint.transform.position);
                _configurableJoint.xMotion        = ConfigurableJointMotion.Locked;
                _configurableJoint.yMotion        = ConfigurableJointMotion.Locked;
                _configurableJoint.zMotion        = ConfigurableJointMotion.Locked;
                _configurableJoint.angularZMotion =
                    useHingeJoint ? ConfigurableJointMotion.Locked : ConfigurableJointMotion.Free;
                _configurableJoint.enableCollision = true;
                _configurableJoint.breakForce      = breakForce;

                // Reset input flag
                vc.input.ResetTrailerAttachDetachFlag();

                // Enable lights if tractor has them enabled
                if (vc.effectsManager.lightsManager.IsEnabled)
                {
                    trailerVc.effectsManager.lightsManager.Enable();
                }
                else
                {
                    trailerVc.effectsManager.lightsManager.Disable();
                }

                attached = true;

                _trailer.OnAttach(this);

                onTrailerAttach.Invoke();
            }
        }
Beispiel #2
0
        public override void FixedUpdate()
        {
            if (_trailer == null)            // Check if already attached
            {
                if (attachmentPoint != null) // Check if attachment point exists
                {
                    if (_trailerModules.Count > 0)
                    {
                        _nearestTrailerModule = null;
                        trailerInRange        = false;
                        foreach (TrailerModule trailerModule in _trailerModules)
                        {
                            if (trailerModule.Active)
                            {
                                Vector3 dir = trailerModule.attachmentPoint.transform.position -
                                              attachmentPoint.transform.position;
                                float dist = dir.sqrMagnitude;

                                if (dist < attachDistanceThreshold)
                                {
                                    _nearestTrailerModule = trailerModule;
                                    trailerInRange        = true;
                                    break;
                                }
                            }
                        }
                    }
                    else
                    {
                        _nearestTrailerModule = null;
                        trailerInRange        = false;
                    }
                }
            }

            // Attach trailer
            if (_nearestTrailerModule != null && _trailer == null && vc.input.TrailerAttachDetach)
            {
                AttachTrailer(vc);
            }
            // Detach trailer
            else if (_trailer != null && vc.input.TrailerAttachDetach)
            {
                DetachTrailer(vc);
            }

            // Detach trailer if joint broke
            if (_trailer != null && _configurableJoint == null)
            {
                DetachTrailer(vc);
            }

            vc.input.ResetTrailerAttachDetachFlag();
        }
Beispiel #3
0
        private void FindSceneTrailerModules(ref List <TrailerModule> trailerModules)
        {
            trailerModules = new List <TrailerModule>();

            foreach (TrailerModuleWrapper trailerModuleWrapper in Object.FindObjectsOfType <TrailerModuleWrapper>())
            {
                TrailerModule trailerModule = trailerModuleWrapper.module;
                if (trailerModule != null && vc.gameObject.scene == SceneManager.GetActiveScene())
                {
                    trailerModules.Add(trailerModule);
                }
            }
        }
Beispiel #4
0
        private void DetachTrailer(VehicleController vc)
        {
            if (!detachable || _trailer == null || _trailer.VehicleController == null)
            {
                return;
            }

            attached = false;

            if (_configurableJoint != null)
            {
                Object.Destroy(_configurableJoint);
                _configurableJoint = null;
            }

            _trailer.OnDetach();
            _trailer = null;

            vc.input.ResetTrailerAttachDetachFlag();

            onTrailerDetach.Invoke();
        }
Beispiel #5
0
 public override void SetModule(VehicleModule module)
 {
     this.module = module as TrailerModule;
 }