void SetupDoorsHingeJoints() { foreach (var pair in _frames.Where(x => x.Name.StartsWith("door_"))) { var doorAlignment = GetDoorAlignment(pair.Name); if (doorAlignment == DoorAlignment.None) { continue; } var hinge = pair.gameObject.AddComponent <HingeJoint>(); hinge.axis = Vector3.up; hinge.useLimits = true; var limit = 90.0f * ((doorAlignment == DoorAlignment.LeftFront || doorAlignment == DoorAlignment.LeftRear) ? 1.0f : -1.0f); hinge.limits = new JointLimits { min = Mathf.Min(0, limit), max = Mathf.Max(0, limit), }; hinge.connectedBody = gameObject.GetComponent <Rigidbody>(); } }
private void Initialize(VehicleDef def, int[] colors = null) { Definition = def; if (colors != null && colors[0] != -1) { SetColors(colors); } else { var defaultClrs = CarColors.GetCarDefaults(Definition.ModelName); if (defaultClrs != null) { SetColors(defaultClrs[UnityEngine.Random.Range(0, defaultClrs.Count)]); } else { Debug.LogWarningFormat("No colours defined for {0}!", def.GameName); } } name = Definition.GameName; _geometryParts = Geometry.Load(Definition.ModelName, TextureDictionary.Load(Definition.TextureDictionaryName), TextureDictionary.Load("vehicle"), TextureDictionary.Load("misc")); _frames = _geometryParts.AttachFrames(transform, MaterialFlags.Vehicle); var wheelFrame = _frames.FirstOrDefault(x => x.Name == "wheel"); if (wheelFrame == null) { Debug.LogWarningFormat("No wheels defined for {0}!", def.GameName); Destroy(gameObject); return; } foreach (var frame in _frames) { if (!frame.Name.StartsWith("wheel_")) continue; if (!frame.Name.EndsWith("_dummy")) continue; var childFrames = _frames.Where(x => x.ParentIndex == frame.Index); // disable all children of wheel dummies foreach (var childFrame in childFrames) { childFrame.gameObject.SetActive(false); } var wheelAlignment = GetWheelAlignment(frame.Name); Wheel inst; // see if this wheel dummy has a wheel child var wheel = childFrames.FirstOrDefault(x => x.Name == "wheel"); if (wheel == null) { var copy = Instantiate(wheelFrame.transform); copy.SetParent(frame.transform, false); _wheels.Add(inst = new Wheel { Alignment = wheelAlignment, Parent = frame.transform, Child = copy, }); } else { // all children of wheel dummies get set to inactive so activate this one wheel.gameObject.SetActive(true); _wheels.Add(inst = new Wheel { Alignment = wheelAlignment, Parent = frame.transform, Child = wheel.transform, }); } if (inst.IsLeftHand) { frame.transform.Rotate(Vector3.up, 180.0f); } inst.Complement = _wheels.FirstOrDefault(x => (x.Alignment & WheelAlignment.LeftRightMask) != (inst.Alignment & WheelAlignment.LeftRightMask) && (x.Alignment & WheelAlignment.FrontMidRearMask) == (inst.Alignment & WheelAlignment.FrontMidRearMask)); if (inst.Complement != null) { inst.Complement.Complement = inst; } } InitializePhysics(); foreach (var pair in _frames.Where(x => x.Name.StartsWith("door_"))) { var doorAlignment = GetDoorAlignment(pair.Name); if (doorAlignment == DoorAlignment.None) continue; var hinge = pair.gameObject.AddComponent<HingeJoint>(); hinge.axis = Vector3.up; hinge.useLimits = true; var limit = 90.0f * ((doorAlignment == DoorAlignment.LeftFront || doorAlignment == DoorAlignment.LeftRear) ? 1.0f : -1.0f); hinge.limits = new JointLimits { min = Mathf.Min(0, limit), max = Mathf.Max(0, limit), }; hinge.connectedBody = gameObject.GetComponent<Rigidbody>(); } var frontSeat = GetPart("ped_frontseat"); var backSeat = GetPart("ped_backseat"); if (frontSeat != null) { var frontSeatMirror = new GameObject("ped_frontseat").transform; frontSeatMirror.SetParent(frontSeat.parent, false); frontSeatMirror.localPosition = Vector3.Scale(frontSeat.localPosition, new Vector3(-1f, 1f, 1f)); if (frontSeat.localPosition.x > 0f) { AttachSeat(frontSeat, SeatAlignment.FrontRight); AttachSeat(frontSeatMirror, SeatAlignment.FrontLeft); } else { AttachSeat(frontSeatMirror, SeatAlignment.FrontRight); AttachSeat(frontSeat, SeatAlignment.FrontLeft); } DriverTransform = GetSeat(SeatAlignment.FrontLeft).Parent; } if (backSeat != null) { var backSeatMirror = new GameObject("ped_backseat").transform; backSeatMirror.SetParent(backSeat.parent, false); backSeatMirror.localPosition = Vector3.Scale(backSeat.localPosition, new Vector3(-1f, 1f, 1f)); if (backSeat.localPosition.x > 0f) { AttachSeat(backSeat, SeatAlignment.BackRight); AttachSeat(backSeatMirror, SeatAlignment.BackLeft); } else { AttachSeat(backSeatMirror, SeatAlignment.BackRight); AttachSeat(backSeat, SeatAlignment.BackLeft); } } gameObject.SetLayerRecursive(Layer); }
private void Initialize(VehicleDef def, int[] colors = null) { Definition = def; if (colors != null && colors[0] != -1) { SetColors(colors); } else { var defaultClrs = CarColors.GetCarDefaults(Definition.ModelName); if (defaultClrs != null) { SetColors(defaultClrs[UnityEngine.Random.Range(0, defaultClrs.Count)]); } else { Debug.LogWarningFormat("No colours defined for {0}!", def.GameName); } } name = Definition.GameName; _geometryParts = Geometry.Load(Definition.ModelName, TextureDictionary.Load(Definition.TextureDictionaryName), TextureDictionary.Load("vehicle"), TextureDictionary.Load("misc")); _frames = _geometryParts.AttachFrames(transform, MaterialFlags.Vehicle); var wheelFrame = _frames.FirstOrDefault(x => x.Name == "wheel"); if (wheelFrame == null) { Debug.LogWarningFormat("No wheels defined for {0}!", def.GameName); Destroy(gameObject); return; } foreach (var frame in _frames) { if (!frame.Name.StartsWith("wheel_")) { continue; } if (!frame.Name.EndsWith("_dummy")) { continue; } var childFrames = _frames.Where(x => x.ParentIndex == frame.Index); // disable all children of wheel dummies foreach (var childFrame in childFrames) { childFrame.gameObject.SetActive(false); } var wheelAlignment = GetWheelAlignment(frame.Name); Wheel inst; // see if this wheel dummy has a wheel child var wheel = childFrames.FirstOrDefault(x => x.Name == "wheel"); if (wheel == null) { var copy = Instantiate(wheelFrame.transform); copy.SetParent(frame.transform, false); _wheels.Add(inst = new Wheel { Alignment = wheelAlignment, Parent = frame.transform, Child = copy, }); } else { // all children of wheel dummies get set to inactive so activate this one wheel.gameObject.SetActive(true); _wheels.Add(inst = new Wheel { Alignment = wheelAlignment, Parent = frame.transform, Child = wheel.transform, }); } if (inst.IsLeftHand) { frame.transform.Rotate(Vector3.up, 180.0f); } inst.Complement = _wheels.FirstOrDefault(x => (x.Alignment & WheelAlignment.LeftRightMask) != (inst.Alignment & WheelAlignment.LeftRightMask) && (x.Alignment & WheelAlignment.FrontMidRearMask) == (inst.Alignment & WheelAlignment.FrontMidRearMask)); if (inst.Complement != null) { inst.Complement.Complement = inst; } } InitializePhysics(); foreach (var pair in _frames.Where(x => x.Name.StartsWith("door_"))) { var doorAlignment = GetDoorAlignment(pair.Name); if (doorAlignment == DoorAlignment.None) { continue; } var hinge = pair.gameObject.AddComponent <HingeJoint>(); hinge.axis = Vector3.up; hinge.useLimits = true; var limit = 90.0f * ((doorAlignment == DoorAlignment.LeftFront || doorAlignment == DoorAlignment.LeftRear) ? 1.0f : -1.0f); hinge.limits = new JointLimits { min = Mathf.Min(0, limit), max = Mathf.Max(0, limit), }; hinge.connectedBody = gameObject.GetComponent <Rigidbody>(); } var frontSeat = GetPart("ped_frontseat"); var backSeat = GetPart("ped_backseat"); if (frontSeat != null) { var frontSeatMirror = new GameObject("ped_frontseat").transform; frontSeatMirror.SetParent(frontSeat.parent, false); frontSeatMirror.localPosition = Vector3.Scale(frontSeat.localPosition, new Vector3(-1f, 1f, 1f)); if (frontSeat.localPosition.x > 0f) { AttachSeat(frontSeat, SeatAlignment.FrontRight); AttachSeat(frontSeatMirror, SeatAlignment.FrontLeft); } else { AttachSeat(frontSeatMirror, SeatAlignment.FrontRight); AttachSeat(frontSeat, SeatAlignment.FrontLeft); } DriverTransform = GetSeat(SeatAlignment.FrontLeft).Parent; } if (backSeat != null) { var backSeatMirror = new GameObject("ped_backseat").transform; backSeatMirror.SetParent(backSeat.parent, false); backSeatMirror.localPosition = Vector3.Scale(backSeat.localPosition, new Vector3(-1f, 1f, 1f)); if (backSeat.localPosition.x > 0f) { AttachSeat(backSeat, SeatAlignment.BackRight); AttachSeat(backSeatMirror, SeatAlignment.BackLeft); } else { AttachSeat(backSeatMirror, SeatAlignment.BackRight); AttachSeat(backSeat, SeatAlignment.BackLeft); } } gameObject.SetLayerRecursive(Layer); }
private void Initialize(VehicleDef def, int[] colors = null) { Definition = def; if (colors != null && colors[0] != -1) { SetColors(colors); } else { var defaultClrs = CarColors.GetCarDefaults(Definition.ModelName); if (defaultClrs != null) { SetColors(defaultClrs[UnityEngine.Random.Range(0, defaultClrs.Count)]); } else { Debug.LogWarningFormat("No colours defined for {0}!", def.GameName); } } name = Definition.GameName; _geometryParts = LoadGeometryParts(Definition); _frames = _geometryParts.AttachFrames(transform, MaterialFlags.Vehicle); var wheelFrame = _frames.FirstOrDefault(x => x.Name == "wheel"); if (wheelFrame == null) { Debug.LogWarningFormat("No wheels defined for {0}!", def.GameName); Destroy(gameObject); return; } var engineFrame = _frames.FirstOrDefault(x => x.Name == "engine"); if (engineFrame != null) { this.EngineTransform = engineFrame.transform; } var petrolcapFrame = _frames.FirstOrDefault(x => x.Name == "petrolcap"); if (petrolcapFrame != null) { this.PetrolcapTransform = petrolcapFrame.transform; } foreach (var frame in _frames) { if (!frame.Name.StartsWith("wheel_")) { continue; } if (!frame.Name.EndsWith("_dummy")) { continue; } var childFrames = _frames.Where(x => x.ParentIndex == frame.Index); // disable all children of wheel dummies foreach (var childFrame in childFrames) { childFrame.gameObject.SetActive(false); } var wheelAlignment = GetWheelAlignment(frame.Name); Wheel inst; // see if this wheel dummy has a wheel child var wheel = childFrames.FirstOrDefault(x => x.Name == "wheel"); if (wheel == null) { var copy = Instantiate(wheelFrame.transform); copy.SetParent(frame.transform, false); _wheels.Add(inst = new Wheel { Alignment = wheelAlignment, Parent = frame.transform, Child = copy, }); } else { // all children of wheel dummies get set to inactive so activate this one wheel.gameObject.SetActive(true); _wheels.Add(inst = new Wheel { Alignment = wheelAlignment, Parent = frame.transform, Child = wheel.transform, }); } if (inst.IsLeftHand) { frame.transform.Rotate(Vector3.up, 180.0f); } inst.Complement = _wheels.FirstOrDefault(x => (x.Alignment & WheelAlignment.LeftRightMask) != (inst.Alignment & WheelAlignment.LeftRightMask) && (x.Alignment & WheelAlignment.FrontMidRearMask) == (inst.Alignment & WheelAlignment.FrontMidRearMask)); if (inst.Complement != null) { inst.Complement.Complement = inst; } } InitializePhysics(); this.Health = this.MaxHealth = Mathf.Pow(this.HandlingData.Mass, VehicleManager.Instance.massToHealthExponent); //this.SetupDoorsHingeJoints(); var frontSeat = GetPart("ped_frontseat"); var backSeat = GetPart("ped_backseat"); if (frontSeat != null) { var frontSeatMirror = new GameObject("ped_frontseat").transform; frontSeatMirror.SetParent(frontSeat.parent, false); frontSeatMirror.localPosition = Vector3.Scale(frontSeat.localPosition, new Vector3(-1f, 1f, 1f)); if (frontSeat.localPosition.x > 0f) { AttachSeat(frontSeat, SeatAlignment.FrontRight); AttachSeat(frontSeatMirror, SeatAlignment.FrontLeft); } else { AttachSeat(frontSeatMirror, SeatAlignment.FrontRight); AttachSeat(frontSeat, SeatAlignment.FrontLeft); } DriverTransform = GetSeat(SeatAlignment.FrontLeft).Parent; } if (backSeat != null) { var backSeatMirror = new GameObject("ped_backseat").transform; backSeatMirror.SetParent(backSeat.parent, false); backSeatMirror.localPosition = Vector3.Scale(backSeat.localPosition, new Vector3(-1f, 1f, 1f)); if (backSeat.localPosition.x > 0f) { AttachSeat(backSeat, SeatAlignment.BackRight); AttachSeat(backSeatMirror, SeatAlignment.BackLeft); } else { AttachSeat(backSeatMirror, SeatAlignment.BackRight); AttachSeat(backSeat, SeatAlignment.BackLeft); } } // Add vehicle damage /* * var dam = gameObject.AddComponent<VehicleDamage>(); * dam.damageParts = new Transform[] { transform.GetChild(0).Find("engine") }; * dam.deformMeshes = gameObject.GetComponentsInChildren<MeshFilter>(); * dam.displaceParts = gameObject.GetComponentsInChildren<Transform>().Where(x => x.GetComponent<Frame>() != null || x.GetComponent<FrameContainer>() != null).ToArray(); * dam.damageFactor = VehicleAPI.constDamageFactor; * dam.collisionIgnoreHeight = -.4f; * dam.collisionTimeGap = .1f; * * //OptimizeVehicle(); * * dam.deformColliders = gameObject.GetComponentsInChildren<MeshCollider>(); */ gameObject.SetLayerRecursive(Layer); SetupHighDetailMesh(); }
private void Initialize(VehicleDef def, int[] colors = null) { Definition = def; if (colors != null && colors[0] != -1) { SetColors(colors); } else { var defaultClrs = CarColors.GetCarDefaults(Definition.ModelName); if (defaultClrs != null) { SetColors(defaultClrs[UnityEngine.Random.Range(0, defaultClrs.Count)]); } else { Debug.LogWarningFormat("No colours defined for {0}!", def.GameName); } } name = Definition.GameName; _geometryParts = Geometry.Load(Definition.ModelName, TextureDictionary.Load(Definition.TextureDictionaryName), TextureDictionary.Load("vehicle"), TextureDictionary.Load("misc")); _frames = _geometryParts.AttachFrames(transform, MaterialFlags.Vehicle); var wheelFrame = _frames.FirstOrDefault(x => x.Name == "wheel"); if (wheelFrame == null) { Debug.LogWarningFormat("No wheels defined for {0}!", def.GameName); Destroy(gameObject); return; } foreach (var frame in _frames) { if (!frame.Name.StartsWith("wheel_")) { continue; } if (!frame.Name.EndsWith("_dummy")) { continue; } var childFrames = _frames.Where(x => x.ParentIndex == frame.Index); // disable all children of wheel dummies foreach (var childFrame in childFrames) { childFrame.gameObject.SetActive(false); } var wheelAlignment = GetWheelAlignment(frame.Name); Wheel inst; // see if this wheel dummy has a wheel child var wheel = childFrames.FirstOrDefault(x => x.Name == "wheel"); if (wheel == null) { var copy = Instantiate(wheelFrame.transform); copy.SetParent(frame.transform, false); _wheels.Add(inst = new Wheel { Alignment = wheelAlignment, Parent = frame.transform, Child = copy, }); } else { // all children of wheel dummies get set to inactive so activate this one wheel.gameObject.SetActive(true); _wheels.Add(inst = new Wheel { Alignment = wheelAlignment, Parent = frame.transform, Child = wheel.transform, }); } if (inst.IsLeftHand) { frame.transform.Rotate(Vector3.up, 180.0f); } inst.Complement = _wheels.FirstOrDefault(x => (x.Alignment & WheelAlignment.LeftRightMask) != (inst.Alignment & WheelAlignment.LeftRightMask) && (x.Alignment & WheelAlignment.FrontMidRearMask) == (inst.Alignment & WheelAlignment.FrontMidRearMask)); if (inst.Complement != null) { inst.Complement.Complement = inst; } } InitializePhysics(); foreach (var pair in _frames.Where(x => x.Name.StartsWith("door_"))) { var doorAlignment = GetDoorAlignment(pair.Name); if (doorAlignment == DoorAlignment.None) { continue; } var hinge = pair.gameObject.AddComponent <HingeJoint>(); hinge.axis = Vector3.up; hinge.useLimits = true; var limit = 90.0f * ((doorAlignment == DoorAlignment.LeftFront || doorAlignment == DoorAlignment.LeftRear) ? 1.0f : -1.0f); hinge.limits = new JointLimits { min = Mathf.Min(0, limit), max = Mathf.Max(0, limit), }; hinge.connectedBody = gameObject.GetComponent <Rigidbody>(); } var frontSeat = GetPart("ped_frontseat"); var backSeat = GetPart("ped_backseat"); if (frontSeat != null) { var frontSeatMirror = new GameObject("ped_frontseat").transform; frontSeatMirror.SetParent(frontSeat.parent, false); frontSeatMirror.localPosition = Vector3.Scale(frontSeat.localPosition, new Vector3(-1f, 1f, 1f)); if (frontSeat.localPosition.x > 0f) { AttachSeat(frontSeat, SeatAlignment.FrontRight); AttachSeat(frontSeatMirror, SeatAlignment.FrontLeft); } else { AttachSeat(frontSeatMirror, SeatAlignment.FrontRight); AttachSeat(frontSeat, SeatAlignment.FrontLeft); } DriverTransform = GetSeat(SeatAlignment.FrontLeft).Parent; } if (backSeat != null) { var backSeatMirror = new GameObject("ped_backseat").transform; backSeatMirror.SetParent(backSeat.parent, false); backSeatMirror.localPosition = Vector3.Scale(backSeat.localPosition, new Vector3(-1f, 1f, 1f)); if (backSeat.localPosition.x > 0f) { AttachSeat(backSeat, SeatAlignment.BackRight); AttachSeat(backSeatMirror, SeatAlignment.BackLeft); } else { AttachSeat(backSeatMirror, SeatAlignment.BackRight); AttachSeat(backSeat, SeatAlignment.BackLeft); } } // Add vehicle damage //GameObject carObject = GameObject.Find(Definition.GameName.ToLower()); //Debug.Log(gameObject.name); var dam = gameObject.AddComponent <VehicleDamage>(); dam.damageParts = new Transform[] { transform.GetChild(0).Find("engine") }; dam.deformMeshes = gameObject.GetComponentsInChildren <MeshFilter>(); dam.displaceParts = gameObject.GetComponentsInChildren <Transform>().Where(x => x.GetComponent <Frame>() != null || x.GetComponent <FrameContainer>() != null).ToArray(); dam.damageFactor = VehicleAPI.constDamageFactor; dam.collisionIgnoreHeight = -.4f; dam.collisionTimeGap = .1f; //OptimizeVehicle(); dam.deformColliders = gameObject.GetComponentsInChildren <MeshCollider>(); // Implemented: Add lights Transform headlights = this.GetComponentWithName <Transform>("headlights"), taillights = this.GetComponentWithName <Transform>("taillights"); Vehicle vh = gameObject.GetComponent <Vehicle>(); if (headlights != null) { m_frontLeftLight = VehicleAPI.SetCarLight(vh, headlights, VehicleLight.FrontLeft); m_frontRightLight = VehicleAPI.SetCarLight(vh, headlights, VehicleLight.FrontRight); } if (taillights != null) { m_rearLeftLight = VehicleAPI.SetCarLight(vh, taillights, VehicleLight.RearLeft); m_rearRightLight = VehicleAPI.SetCarLight(vh, taillights, VehicleLight.RearRight); } // Apply Light sources directionalLightsMat = Resources.Load <Material>("Materials/directionalLight"); VehicleAPI.SetLightSources(gameObject, directionalLightsMat); m_frontLeftLightOk = m_frontLeftLight != null; m_frontRightLightOk = m_frontRightLight != null; m_rearLeftLightOk = m_rearLeftLight != null; m_rearRightLightOk = m_rearRightLight != null; gameObject.SetLayerRecursive(Layer); }