void loadJsonJoints(JSONNode jsonJoints) { int JointCount = 0; for (int i = 0, numberOfJoints = jsonJoints.Count; i < numberOfJoints; i++) { JSONNode jsonJoint = jsonJoints[i]; int jointType = jsonJoint["jointType"].AsInt; GameObject bodyA = loadedObjects[jsonJoint["bodyA"].AsInt]; GameObject bodyB = loadedObjects[jsonJoint["bodyB"].AsInt]; JSONNode localAnchorA = jsonJoint["localAnchorA"]; Vector2 anchorA = new Vector2(localAnchorA[0].AsFloat / RATIO, -localAnchorA[1].AsFloat / RATIO); JSONNode localAnchorB = jsonJoint["localAnchorB"]; Vector2 anchorB = new Vector2(localAnchorB[0].AsFloat / RATIO, -localAnchorB[1].AsFloat / RATIO); bool collideConnected = jsonJoint["collideConnected"].AsBool; string userData = jsonJoint["userData"].Value; if (jointType == (int)JointTypes.JOINT_DISTANCE || jointType == (int)JointTypes.JOINT_ROPE) { DistanceJoint2D joint = bodyA.AddComponent <DistanceJoint2D>(); joint.connectedBody = bodyB.GetComponent <Rigidbody2D>(); joint.anchor = anchorA; joint.connectedAnchor = anchorB; // distance joint if (jsonJoint["length"] != null) { joint.distance = jsonJoint["length"].AsFloat / RATIO; joint.maxDistanceOnly = true; } // rope joint else if (jsonJoint["maxLength"] != null) { joint.distance = jsonJoint["maxLength"].AsFloat / RATIO; } joint.enableCollision = collideConnected; joint.name += '_'; joint.name += userData.Length > 0 ? userData : "joint" + JointCount++; } else if (jointType == (int)JointTypes.JOINT_REVOLUTE) { HingeJoint2D joint = bodyA.AddComponent <HingeJoint2D>(); joint.connectedBody = bodyB.GetComponent <Rigidbody2D>(); joint.anchor = anchorA; joint.connectedAnchor = anchorB; joint.enableCollision = collideConnected; joint.name += '_'; joint.name += userData.Length > 0 ? userData : "joint" + JointCount++; // limits are not working properly bool enableLimits = jsonJoint["enableLimit"].AsBool; float referenceAngle = -jsonJoint["referenceAngle"].AsFloat; float angleBetweenBodies = Mathf.Atan2(bodyB.transform.position.y - bodyA.transform.position.y, bodyB.transform.position.x - bodyA.transform.position.x) * 180 / Mathf.PI; float upperAngle = -jsonJoint["lowerAngle"].AsFloat; float lowerAngle = -jsonJoint["upperAngle"].AsFloat; bool enableMotor = jsonJoint["enableMotor"].AsBool; float motorSpeed = -jsonJoint["motorSpeed"].AsFloat; float maxMotorTorque = jsonJoint["maxMotorTorque"].AsFloat; joint.useLimits = enableLimits; JointAngleLimits2D limits = new JointAngleLimits2D(); limits.max = angleBetweenBodies + upperAngle; limits.min = angleBetweenBodies + lowerAngle; joint.limits = limits; joint.useMotor = enableMotor; JointMotor2D motor = new JointMotor2D(); motor.maxMotorTorque = maxMotorTorque; motor.motorSpeed = motorSpeed; joint.motor = motor; } else if (jointType == (int)JointTypes.JOINT_WHEEL) { WheelJoint2D joint = bodyA.AddComponent <WheelJoint2D>(); joint.connectedBody = bodyB.GetComponent <Rigidbody2D>(); joint.anchor = anchorA; joint.connectedAnchor = anchorB; joint.enableCollision = collideConnected; joint.name += '_'; joint.name += userData.Length > 0 ? userData : "joint" + JointCount++; bool enableMotor = jsonJoint["enableMotor"].AsBool; float motorSpeed = -jsonJoint["motorSpeed"].AsFloat; float maxMotorTorque = jsonJoint["maxMotorTorque"].AsFloat; float dampingRatio = jsonJoint["dampingRatio"].AsFloat; float frequency = jsonJoint["frequencyHZ"].AsFloat; JSONNode localAxisA = jsonJoint["localAxisA"]; float angle = Mathf.Atan2(-localAxisA[1].AsFloat, localAxisA[0].AsFloat) * 180 / Mathf.PI; joint.useMotor = enableMotor; JointMotor2D motor = new JointMotor2D(); motor.maxMotorTorque = maxMotorTorque; motor.motorSpeed = motorSpeed; joint.motor = motor; JointSuspension2D suspension = new JointSuspension2D(); suspension.dampingRatio = dampingRatio; suspension.frequency = frequency; suspension.angle = angle; joint.suspension = suspension; } else if (jointType == (int)JointTypes.JOINT_PRISMATIC) { SliderJoint2D joint = bodyA.AddComponent <SliderJoint2D>(); joint.connectedBody = bodyB.GetComponent <Rigidbody2D>(); joint.anchor = anchorA; joint.connectedAnchor = anchorB; joint.enableCollision = collideConnected; joint.name += '_'; joint.name += userData.Length > 0 ? userData : "joint" + JointCount++; bool enableLimits = jsonJoint["enableLimit"].AsBool; float referenceAngle = -jsonJoint["referenceAngle"].AsFloat; float upperTranslation = jsonJoint["upperTranslation"].AsFloat / RATIO; float lowerTranslation = jsonJoint["lowerTranslation"].AsFloat / RATIO; bool enableMotor = jsonJoint["enableMotor"].AsBool; float motorSpeed = -jsonJoint["motorSpeed"].AsFloat; float maxMotorTorque = jsonJoint["maxMotorTorque"].AsFloat; JSONNode localAxisA = jsonJoint["localAxisA"]; float angle = Mathf.Atan2(-localAxisA[1].AsFloat, localAxisA[0].AsFloat) * 180 / Mathf.PI; joint.useLimits = enableLimits; JointTranslationLimits2D limits = new JointTranslationLimits2D(); limits.max = upperTranslation; limits.min = lowerTranslation; joint.limits = limits; joint.useMotor = enableMotor; JointMotor2D motor = new JointMotor2D(); motor.maxMotorTorque = maxMotorTorque; motor.motorSpeed = motorSpeed; joint.motor = motor; joint.angle = angle; } } }
public void SetRefToJoint(WheelJoint2D joint) { wjoint = joint; }
/// <summary> /// Assemble all separated pieces into one object with connected joints /// </summary> /// <param name="motorcycleBase"></param> /// <param name="swingarmParent"></param> /// <param name="leftWheel"></param> /// <param name="rightWheel"></param> /// <param name="driver"></param> /// <param name="genome"></param> /// <returns></returns> private Motorcycle AssembleMotorcycle(Transform motorcycleBase, Transform swingarmParent, Transform leftWheel, Transform rightWheel, Transform driver, Genome genome) { // Save all necessary transforms for later Transform chasis = motorcycleBase.GetChild(0); Transform chasisAnchors = chasis.GetChild(0); Transform chasisRightWheelAnchor = chasisAnchors.Find("Front Wheel"); Transform chasisSwingarmAnchor = chasisAnchors.Find("Swingarm"); Transform chasisDriverAnchor = chasisAnchors.Find("Driver"); Transform swingarm = swingarmParent.GetChild(0); Transform swingarmLeftWheelAnchor = swingarm.GetChild(0); Transform driverBody = driver.GetChild(0); // Set the transform and the hinge joint values of the swingarm swingarmParent.parent = chasisSwingarmAnchor; swingarmParent.localPosition = new Vector3(0.0f, 0.0f, 0.0f); HingeJoint2D swingarmHingeJoint = swingarm.GetComponent <HingeJoint2D>(); swingarmHingeJoint.connectedBody = chasis.GetComponent <Rigidbody2D>(); swingarmHingeJoint.connectedAnchor = new Vector2(chasisSwingarmAnchor.localPosition.x, chasisSwingarmAnchor.localPosition.y); // Set the transform and the wheel joint values of the left wheel leftWheel.parent = swingarm; leftWheel.localPosition = new Vector3(0.0f, 0.0f, 0.0f); WheelJoint2D leftWheelJoint = leftWheel.GetComponent <WheelJoint2D>(); leftWheelJoint.connectedBody = swingarm.GetComponent <Rigidbody2D>(); leftWheelJoint.connectedAnchor = new Vector2(swingarmLeftWheelAnchor.localPosition.x, swingarmLeftWheelAnchor.localPosition.y); // Set the transform and the wheel joint values of the right wheel rightWheel.parent = chasis; rightWheel.localPosition = new Vector3(0.0f, 0.0f, 0.0f); WheelJoint2D rightWheelJoint = rightWheel.GetComponent <WheelJoint2D>(); rightWheelJoint.connectedBody = chasis.GetComponent <Rigidbody2D>(); rightWheelJoint.connectedAnchor = new Vector2(chasisRightWheelAnchor.localPosition.x, chasisRightWheelAnchor.localPosition.y); // Set the driver's transform and hinge joint values driver.parent = chasisDriverAnchor; driver.localPosition = new Vector3(0.0f, 0.0f, 0.0f);; driverBody.GetComponent <HingeJoint2D>().connectedBody = chasis.GetComponent <Rigidbody2D>(); // Set parents of each part to the base object swingarm.parent = motorcycleBase; leftWheel.parent = motorcycleBase; rightWheel.parent = motorcycleBase; driver.parent = motorcycleBase; // Destroy all anchors Destroy(swingarmParent.gameObject); Destroy(swingarmLeftWheelAnchor.gameObject); Destroy(chasisRightWheelAnchor.gameObject); Destroy(chasisDriverAnchor.gameObject); Destroy(chasisAnchors.gameObject); // To finish, add a motorcycle controller component to the parent object Motorcycle motorcycleComp = motorcycleBase.gameObject.AddComponent <Motorcycle>(); Transform head = driver.Find("head").transform; motorcycleComp.Initialize(genome, leftWheel, rightWheel, driver, head); // Set the head collision detector (needs a delegate) CollisionDetector collisionDetector = head.gameObject.AddComponent <CollisionDetector>(); collisionDetector.SetCollisionCommunication(motorcycleComp.HeadCollided); return(motorcycleComp); }
void Start() { //Metemos la información del componente WheelJoint2D en la variable union_rueda = GetComponent <WheelJoint2D> (); motor = union_rueda.motor; }
// Start is called before the first frame update void Start() { wheel = GetComponent <WheelJoint2D>(); motor = wheel.motor; }
// Use this for initialization void Start() { wheelJoint = GetComponent <WheelJoint2D>(); }
// Use this for initialization void Start() { union_rueda = GetComponent <WheelJoint2D> (); motor = union_rueda.motor; }
private void Awake() { wheelJoint = GetComponent <WheelJoint2D>(); motor = new JointMotor2D(); StartCoroutine("PlayRotationPattern"); }
void Start() { kütükWheel = GetComponent <WheelJoint2D>(); motorum = new JointMotor2D(); StartCoroutine("DönüsIslemi"); }
// public void ApplyAnchors(Joint2D joint) { // if (jointType == JointType2D.RelativeJoint2D) // return; // if (jointType == JointType2D.TargetJoint2D) { // ((TargetJoint2D) joint).anchor = anchor; // ((TargetJoint2D) joint).target = target; // needs to be world relative // return; // } // ((AnchoredJoint2D) joint).anchor = anchor; // ((AnchoredJoint2D) joint).connectedAnchor = connectedAnchor; // } // public void ApplyLimits(Joint2D joint, GameObject go, Rigidbody2D connectedBody) { // if (jointType != JointType2D.HingeJoint2D) // return; // Quaternion startRotation = go.transform.rotation; // if (useLimits && limits.relativeTo == AngleLimitsRelativeTo.ZeroRotation && connectedBody) { // go.transform.rotation = connectedBody.transform.rotation; // // setting the connectedBody triggers a re-calculation of the reference angle // joint.connectedBody = connectedBody; // } // ((HingeJoint2D) joint).limits = limits.ToJointAngleLimits2D(); // if (useLimits && limits.relativeTo == AngleLimitsRelativeTo.ZeroRotation && connectedBody) // go.transform.rotation = startRotation; // } public Joint2D CreateOrApply(GameObject go, Rigidbody2D connectedBody, Joint2D joint = null) { if (joint && !JointMatchesJointType(joint)) { GameObject.Destroy(joint); joint = null; } if (jointType == JointType2D.DistanceJoint2D) { DistanceJoint2D distanceJoint = (DistanceJoint2D)joint ?? go.AddComponent <DistanceJoint2D>(); ConfigureAnchoredJoint(distanceJoint, connectedBody); distanceJoint.maxDistanceOnly = maxDistanceOnly; distanceJoint.autoConfigureDistance = autoConfigureDistance; if (!autoConfigureDistance) { distanceJoint.distance = distance; } joint = distanceJoint; } if (jointType == JointType2D.FixedJoint2D) { FixedJoint2D fixedJoint = (FixedJoint2D)joint ?? go.AddComponent <FixedJoint2D>(); ConfigureAnchoredJoint(fixedJoint, connectedBody); fixedJoint.frequency = frequency; fixedJoint.dampingRatio = dampingRatio; joint = fixedJoint; } if (jointType == JointType2D.FrictionJoint2D) { FrictionJoint2D frictionJoint = (FrictionJoint2D)joint ?? go.AddComponent <FrictionJoint2D>(); ConfigureAnchoredJoint(frictionJoint, connectedBody); frictionJoint.maxForce = maxFrictionForce; frictionJoint.maxTorque = maxFrictionTorque; joint = frictionJoint; } if (jointType == JointType2D.HingeJoint2D) { Quaternion startRotation = go.transform.rotation; // if (useLimits && limits.relativeTo == AngleLimitsRelativeTo.ZeroRotation && connectedBody) // go.transform.rotation = connectedBody.transform.rotation; HingeJoint2D hingeJoint = (HingeJoint2D)joint ?? go.AddComponent <HingeJoint2D>(); ConfigureAnchoredJoint(hingeJoint, connectedBody); hingeJoint.limits = limits.ToJointAngleLimits2D(); hingeJoint.useLimits = useLimits; JointMotor2D m = new JointMotor2D(); m.maxMotorTorque = motor.maxMotorTorque; m.motorSpeed = motor.motorSpeed; hingeJoint.motor = m; hingeJoint.useMotor = useMotor; joint = hingeJoint; // if (useLimits && limits.relativeTo == AngleLimitsRelativeTo.ZeroRotation && connectedBody) // go.transform.rotation = startRotation; } if (jointType == JointType2D.RelativeJoint2D) { RelativeJoint2D relativeJoint = (RelativeJoint2D)joint ?? go.AddComponent <RelativeJoint2D>(); ConfigureJoint(relativeJoint, connectedBody); relativeJoint.autoConfigureOffset = autoConfigureOffset; if (!autoConfigureOffset) { relativeJoint.linearOffset = linearOffset; relativeJoint.angularOffset = angularOffset; } relativeJoint.correctionScale = correctionScale; relativeJoint.maxForce = maxForce; relativeJoint.maxTorque = maxTorque; joint = relativeJoint; } if (jointType == JointType2D.SliderJoint2D) { SliderJoint2D sliderJoint = (SliderJoint2D)joint ?? go.AddComponent <SliderJoint2D>(); ConfigureAnchoredJoint(sliderJoint, connectedBody); joint = sliderJoint; } if (jointType == JointType2D.SpringJoint2D) { SpringJoint2D springJoint = (SpringJoint2D)joint ?? go.AddComponent <SpringJoint2D>(); ConfigureAnchoredJoint(springJoint, connectedBody); springJoint.autoConfigureDistance = autoConfigureDistance; if (!autoConfigureDistance) { springJoint.distance = distance; } springJoint.dampingRatio = dampingRatio; springJoint.frequency = frequency; joint = springJoint; } if (jointType == JointType2D.TargetJoint2D) { TargetJoint2D targetJoint = (TargetJoint2D)joint ?? go.AddComponent <TargetJoint2D>(); targetJoint.autoConfigureTarget = false; targetJoint.anchor = anchor; targetJoint.target = target; targetJoint.maxForce = maxForce; targetJoint.dampingRatio = dampingRatio; targetJoint.frequency = frequency; joint = targetJoint; } if (jointType == JointType2D.WheelJoint2D) { WheelJoint2D wheelJoint = (WheelJoint2D)joint ?? go.AddComponent <WheelJoint2D>(); ConfigureJoint(wheelJoint, connectedBody); joint = wheelJoint; } return(joint); }
public RenderedCar RenderCar(Car car, Vector3 chassisPos, bool isGame, bool flipped, Game game, int player, bool playerControls) { GameObject chassis = null; GameObject attack1 = null; GameObject attack2 = null; GameObject forkliftObj = null; GameObject frontWheel = null; GameObject backWheel = null; Vector3[] attack1Pos = new Vector3[4]; Vector3 attack2Pos = Vector3.zero; Vector3 forkliftPos = Vector3.zero; Vector3 frontWheelPos = Vector3.zero; Vector3 backWheelPos = Vector3.zero; Vector3 anchorPos = Vector3.zero; Vector3 connectedAnchor = Vector3.zero; Vector3 bladeAnchorPos = Vector3.zero; Vector3 bladeConnectedAnchor = Vector3.zero; float speed = (flipped) ? -300f : 300f; if (car.chassis != null) { switch (car.chassis.id) { case 1: chassis = Instantiate(chassis1, chassisPos, Quaternion.identity); if (!flipped) { attack1Pos[0] = new Vector3(1.21f, -0.14f); attack1Pos[1] = new Vector3(1.21f, -0.14f); attack1Pos[2] = new Vector3(0.5f, -0.17f); attack1Pos[3] = new Vector3(1.21f, -0.14f); forkliftPos = new Vector3(-0.62f, -0.7f); anchorPos = new Vector3(1.84f, 0f); connectedAnchor = new Vector3(0.3f, -0.7f); frontWheelPos = new Vector3(0.55f, -1.24f); backWheelPos = new Vector3(-0.55f, -1.24f); bladeAnchorPos = new Vector3(-1.25f, -0.09f); bladeConnectedAnchor = new Vector3(0.585f, -0.185f); } else { attack1Pos[0] = new Vector3(-1.16f, -0.11f); attack1Pos[1] = new Vector3(-1.16f, -0.11f); attack1Pos[2] = new Vector3(-0.55f, -0.16f); attack1Pos[3] = new Vector3(-1.16f, -0.11f); forkliftPos = new Vector3(0.63f, -0.67f); anchorPos = new Vector3(-1.78f, 0f); connectedAnchor = new Vector3(-0.26f, -0.67f); frontWheelPos = new Vector3(-0.48f, -1.24f); backWheelPos = new Vector3(0.53f, -1.24f); bladeAnchorPos = new Vector3(1.35f, -0.09f); bladeConnectedAnchor = new Vector3(-0.485f, -0.155f); } break; case 2: chassis = Instantiate(chassis2, chassisPos, Quaternion.identity); if (!flipped) { attack1Pos[0] = new Vector3(1.65f, 0.3f); attack1Pos[1] = new Vector3(1.65f, 0.3f); attack1Pos[2] = new Vector3(0.97f, 0.24f); attack1Pos[3] = new Vector3(1.65f, 0.3f); forkliftPos = new Vector3(-0.29f, -0.08f); anchorPos = new Vector3(1.83f, 0f); connectedAnchor = new Vector3(0.625f, -0.08f); frontWheelPos = new Vector3(0.55f, -0.48f); backWheelPos = new Vector3(-0.91f, -0.48f); bladeAnchorPos = new Vector3(-1.29f, -0.09f); bladeConnectedAnchor = new Vector3(1.005f, 0.255f); } else { attack1Pos[0] = new Vector3(-1.61f, 0.29f); attack1Pos[1] = new Vector3(-1.61f, 0.29f); attack1Pos[2] = new Vector3(-0.99f, 0.25f); attack1Pos[3] = new Vector3(-1.61f, 0.29f); forkliftPos = new Vector3(0.27f, -0.09f); anchorPos = new Vector3(-1.8f, 0f); connectedAnchor = new Vector3(-0.63f, -0.09f); frontWheelPos = new Vector3(-0.49f, -0.48f); backWheelPos = new Vector3(0.92f, -0.48f); bladeAnchorPos = new Vector3(1.33f, -0.09f); bladeConnectedAnchor = new Vector3(-0.945f, 0.245f); } break; case 3: chassis = Instantiate(chassis3, chassisPos, Quaternion.identity); if (!flipped) { attack1Pos[0] = new Vector3(1.77f, 0.18f); attack1Pos[1] = new Vector3(1.77f, 0.18f); attack1Pos[2] = new Vector3(1.13f, 0.14f); attack1Pos[3] = new Vector3(1.77f, 0.18f); attack2Pos = new Vector3(-0.2f, 0.08f); forkliftPos = new Vector3(-0.05f, -0.1f); anchorPos = new Vector3(1.8f, 0f); connectedAnchor = new Vector3(0.85f, -0.1f); frontWheelPos = new Vector3(1.07f, -0.57f); backWheelPos = new Vector3(-0.17f, -0.57f); bladeAnchorPos = new Vector3(-1.26f, -0.11f); bladeConnectedAnchor = new Vector3(1.14f, 0.125f); } else { attack1Pos[0] = new Vector3(-1.77f, 0.23f); attack1Pos[1] = new Vector3(-1.77f, 0.23f); attack1Pos[2] = new Vector3(-1.11f, 0.18f); attack1Pos[3] = new Vector3(-1.77f, 0.23f); attack2Pos = new Vector3(0.26f, 0.1f); forkliftPos = new Vector3(0.1f, -0.15f); anchorPos = new Vector3(-1.78f, 0f); connectedAnchor = new Vector3(-0.79f, -0.15f); frontWheelPos = new Vector3(-1.03f, -0.57f); backWheelPos = new Vector3(0.2f, -0.57f); bladeAnchorPos = new Vector3(1.33f, -0.11f); bladeConnectedAnchor = new Vector3(-1.105f, 0.175f); } break; } if (flipped) { chassis.GetComponent <SpriteRenderer>().flipX = true; } if (isGame) { chassis.AddComponent <Rigidbody2D>().mass = 5; chassis.AddComponent <PolygonCollider2D>(); WallAttackDetection wallAttack = chassis.AddComponent <WallAttackDetection>(); wallAttack.game = game; wallAttack.player = player; chassis.tag = "Chassis"; } } if (car.attack1 != null) { switch (car.attack1.id) { case 7: attack1 = Instantiate(blade, new Vector3(0, 0), Quaternion.identity); attack1.transform.position = chassisPos + attack1Pos[0]; break; case 8: attack1 = Instantiate(chainsaw, new Vector3(0, 0), Quaternion.identity); attack1.transform.position = chassisPos + attack1Pos[1]; break; case 9: attack1 = Instantiate(rocket, new Vector3(0, 0), Quaternion.identity); attack1.transform.position = chassisPos + attack1Pos[2]; break; case 10: attack1 = Instantiate(stinger, new Vector3(0, 0), Quaternion.identity); attack1.transform.position = chassisPos + attack1Pos[3]; break; } attack1.transform.SetParent(chassis.transform); attack1.transform.localScale = new Vector3(0.5f, 0.5f); attack1.GetComponent <SpriteRenderer>().sortingOrder = 1; if (flipped) { attack1.GetComponent <SpriteRenderer>().flipX = true; } if (isGame) { if (car.attack1.id == 7) { HingeJoint2D joint = attack1.AddComponent <HingeJoint2D>(); joint.connectedBody = chassis.GetComponent <Rigidbody2D>(); joint.anchor = bladeAnchorPos; joint.connectedAnchor = bladeConnectedAnchor; JointMotor2D motor = joint.motor; motor.motorSpeed = 200; motor.maxMotorTorque = 50; joint.motor = motor; attack1.GetComponent <Rigidbody2D>().mass = 0.3f; } else { FixedJoint2D joint = attack1.AddComponent <FixedJoint2D>(); joint.connectedBody = chassis.GetComponent <Rigidbody2D>(); attack1.GetComponent <Rigidbody2D>().mass = 0.1f; } PolygonCollider2D polygonCollider = attack1.AddComponent <PolygonCollider2D>(); polygonCollider.isTrigger = true; if (car.attack1.id != 9) { AttackDetection attackDetection = attack1.AddComponent <AttackDetection>(); attackDetection.game = game; attackDetection.player = player; } } } if (car.frontWheel != null) { float radius = -1; switch (car.frontWheel.id) { case 4: radius = 0.73f; frontWheel = Instantiate(wheel1, new Vector3(0, 0), Quaternion.identity); break; case 5: radius = 0.61f; frontWheel = Instantiate(wheel2, new Vector3(0, 0), Quaternion.identity); break; } frontWheel.transform.SetParent(chassis.transform); frontWheel.transform.position = chassisPos + frontWheelPos; frontWheel.transform.localScale = new Vector3(0.5f, 0.5f); frontWheel.GetComponent <SpriteRenderer>().sortingOrder = 1; if (flipped) { frontWheel.GetComponent <SpriteRenderer>().flipX = true; } if (isGame) { WheelJoint2D wheelJoint2D = frontWheel.AddComponent <WheelJoint2D>(); wheelJoint2D.connectedBody = chassis.GetComponent <Rigidbody2D>(); wheelJoint2D.connectedAnchor = frontWheelPos; JointSuspension2D jointSuspension = wheelJoint2D.suspension; jointSuspension.dampingRatio = 0.9f; jointSuspension.frequency = 10; wheelJoint2D.suspension = jointSuspension; wheelJoint2D.useMotor = true; JointMotor2D motor = wheelJoint2D.motor; motor.motorSpeed = speed; wheelJoint2D.motor = motor; wheelJoint2D.useMotor = !playerControls; CircleCollider2D circleCollider = frontWheel.AddComponent <CircleCollider2D>(); circleCollider.radius = radius; } } if (car.backWheel != null) { float radius = -1; switch (car.backWheel.id) { case 4: radius = 0.73f; backWheel = Instantiate(wheel1, new Vector3(0, 0), Quaternion.identity); break; case 5: radius = 0.61f; backWheel = Instantiate(wheel2, new Vector3(0, 0), Quaternion.identity); break; } backWheel.transform.SetParent(chassis.transform); backWheel.transform.position = chassisPos + backWheelPos; backWheel.transform.localScale = new Vector3(0.5f, 0.5f); backWheel.GetComponent <SpriteRenderer>().sortingOrder = 1; if (flipped) { backWheel.GetComponent <SpriteRenderer>().flipX = true; } if (isGame) { WheelJoint2D wheelJoint2D = backWheel.AddComponent <WheelJoint2D>(); wheelJoint2D.connectedBody = chassis.GetComponent <Rigidbody2D>(); wheelJoint2D.connectedAnchor = backWheelPos; JointSuspension2D jointSuspension = wheelJoint2D.suspension; jointSuspension.dampingRatio = 0.9f; jointSuspension.frequency = 10; wheelJoint2D.suspension = jointSuspension; wheelJoint2D.useMotor = true; JointMotor2D motor = wheelJoint2D.motor; motor.motorSpeed = speed; wheelJoint2D.motor = motor; wheelJoint2D.useMotor = !playerControls; CircleCollider2D circleCollider = backWheel.AddComponent <CircleCollider2D>(); circleCollider.radius = radius; } } if (car.forklift != null) { forkliftObj = Instantiate(forklift); forkliftObj.transform.position = chassisPos + forkliftPos; forkliftObj.transform.localScale = new Vector3(0.5f, 0.5f); forkliftObj.GetComponent <SpriteRenderer>().sortingOrder = 1; forkliftObj.transform.SetParent(chassis.transform); if (!flipped) { forkliftObj.GetComponent <SpriteRenderer>().flipX = true; } if (isGame) { forkliftObj.AddComponent <PolygonCollider2D>(); HingeJoint2D joint = forkliftObj.AddComponent <HingeJoint2D>(); joint.connectedBody = chassis.GetComponent <Rigidbody2D>(); joint.anchor = anchorPos; joint.connectedAnchor = connectedAnchor; JointMotor2D motor = joint.motor; if (player == 0) { motor.motorSpeed = -50; } else { motor.motorSpeed = 50; } motor.maxMotorTorque = 500; joint.motor = motor; joint.useMotor = !playerControls; forkliftObj.GetComponent <Rigidbody2D>().mass = 0.3f; } } if (car.attack2 != null) { attack2 = Instantiate(rocket); attack2.transform.position = chassisPos + attack2Pos; attack2.transform.localScale = new Vector3(0.5f, 0.5f); attack2.GetComponent <SpriteRenderer>().sortingOrder = 1; attack2.transform.SetParent(chassis.transform); if (flipped) { attack2.GetComponent <SpriteRenderer>().flipX = true; } if (isGame) { FixedJoint2D joint = attack2.AddComponent <FixedJoint2D>(); joint.connectedBody = chassis.GetComponent <Rigidbody2D>(); attack2.GetComponent <Rigidbody2D>().mass = 0; } } RenderedCar renderedCar = new RenderedCar(); renderedCar.chassis = chassis; renderedCar.attack1 = attack1; renderedCar.attack2 = attack2; renderedCar.forklift = forkliftObj; renderedCar.frontWheel = frontWheel; renderedCar.backWheel = backWheel; return(renderedCar); }
public void ConstructBike(BikeConstructionData data) { GameObject frame; if (data.frame != null) { frame = Instantiate(data.frame.gameObject, transform.position, transform.rotation); _frame = frame.GetComponent <FrameItem>(); _frameRB = frame.GetComponent <Rigidbody2D>(); _backWheelJoint = _frame.backWheelJoint; _frontWheelJoint = _frame.frontWheelJoint; _backWheelMotor = _backWheelJoint.motor; _frontWheelMotor = _frontWheelJoint.motor; } else { _isDead = true; raceOverlay.Wasted(); return; } if (data.backWheel != null) { //Create back wheel Vector3 backWheelWorldPos = frame.transform.TransformPoint(_backWheelJoint.anchor); GameObject backWheel = Instantiate(data.backWheel.gameObject, backWheelWorldPos, frame.transform.rotation); _backWheelJoint.connectedBody = backWheel.GetComponent <Rigidbody2D>(); } else { _backWheelJoint.enabled = false; } if (data.frontWheel != null) { // Create front wheel Vector3 frontWheelWorldPos = frame.transform.TransformPoint(_frontWheelJoint.anchor); GameObject frontWheel = Instantiate(data.frontWheel.gameObject, frontWheelWorldPos, frame.transform.rotation); _frontWheelJoint.connectedBody = frontWheel.GetComponent <Rigidbody2D>(); } else { _backWheelJoint.enabled = false; } if (data.handlebars != null) { GameObject handlebars = Instantiate(data.handlebars.gameObject, _frame.handlebarTransform); var handlebarItem = handlebars.GetComponent <HandlebarItem>(); Vector3 localHandlebarPosition = _frameRB.transform.InverseTransformPoint(handlebarItem.handPosition.position); _ragdollController.SetHandJointAnchor(_frameRB, localHandlebarPosition); } else { _ragdollController.DisableHandJoint(); } if (data.seatItem != null) { GameObject seat = Instantiate(data.seatItem.gameObject, _frame.seatTransform); var seatItem = seat.GetComponent <SeatItem>(); Vector3 localSeatPosition = _frameRB.transform.InverseTransformPoint(seatItem.assPosition.position); _ragdollController.SetSeatJointAnchor(_frameRB, localSeatPosition); } else { _ragdollController.DisableSeatJoint(); } SetConfiguredValues(); CameraDirector.Instance.SetTarget(_ragdollController.torso.transform); _ragdollController.headCollisionEventSystem.onValidCollision += DestroyBike; }
void Awake() { wheelJoint = GetComponent <WheelJoint2D> (); }