Beispiel #1
0
    public static void MoveBallInQuad(Transform cube, float radius, Vector3 hitPoint, ref Vector3 position)
    {
        if (sphereInCube(hitPoint, radius, cube))
        {
            position = hitPoint;
        }
        else
        {
            Vector3 _ballMoveLocalPosition = VectorOperator.getLocalPosition(cube, hitPoint);

            if (!(Mathf.Abs(_ballMoveLocalPosition.x) <= -radius + 0.5f * cube.localScale.x))
            {
                if (Mathf.Abs(_ballMoveLocalPosition.z) <= -radius + 0.5f * cube.localScale.z)
                {
                    Vector3 ballMovePositionX = _ballMoveLocalPosition.x > 0.0f? VectorOperator.getWordPosition(cube, (-radius + 0.5f * cube.localScale.x) * Vector3.right): VectorOperator.getWordPosition(cube, (radius - 0.5f * cube.localScale.x) * Vector3.right);
                    position = new Vector3(ballMovePositionX.x, position.y, hitPoint.z);
                }
            }
            else
            {
                if (Mathf.Abs(_ballMoveLocalPosition.x) <= -radius + 0.5f * cube.localScale.x)
                {
                    Vector3 ballMovePositionZ = _ballMoveLocalPosition.z > 0.0f? VectorOperator.getWordPosition(cube, (-radius + 0.5f * cube.localScale.z) * Vector3.forward):
                                                VectorOperator.getWordPosition(cube, (radius - 0.5f * cube.localScale.z) * Vector3.forward);
                    position = new Vector3(hitPoint.x, position.y, ballMovePositionZ.z);
                }
            }
        }
    }
Beispiel #2
0
    public static void KeepInCube(Transform cube, float radius, Transform sphere)
    {
        if (!sphereInCube(sphere.position, radius, cube))
        {
            Vector3 _ballLocalPosition = VectorOperator.getLocalPosition(cube, sphere.position);
            float   displacementX1     = _ballLocalPosition.x + radius - 0.5f * cube.localScale.x;
            float   displacementX2     = _ballLocalPosition.x - radius + 0.5f * cube.localScale.x;

            if (displacementX1 > 0.0f)
            {
                sphere.position -= displacementX1 * cube.right;
            }
            else
            if (displacementX2 < 0.0f)
            {
                sphere.position -= displacementX2 * cube.right;
            }

            float displacementZ1 = _ballLocalPosition.z + radius - 0.5f * cube.localScale.z;
            float displacementZ2 = _ballLocalPosition.z - radius + 0.5f * cube.localScale.z;

            if (displacementZ1 > 0.0f)
            {
                sphere.position -= displacementZ1 * cube.forward;
            }
            else
            if (displacementZ2 < 0.0f)
            {
                sphere.position -= displacementZ2 * cube.forward;
            }
        }
    }
    public bool OnButton(RaycastHit curentHit)
    {
        if (!isSelected)
        {
            return(false);
        }

        if (curentHit.collider == GetComponent <Collider>() && Vector3.Distance(curentHit.point, strPosition) < radius)
        {
            touch.position = curentHit.point;
        }
        else
        {
            Vector3 displacement = curentHit.point - strPosition;

            touch.position = radius * (displacement - Vector3.Project(displacement, cueCamera.transform.forward)).normalized + strPosition;
        }

        displacementX = Mathf.Clamp((1.0f / radius) * VectorOperator.getLocalPosition(transform, touch.position).x, -1.0f, 1.0f);
        displacementZ = Mathf.Clamp((1.0f / radius) * VectorOperator.getLocalPosition(transform, touch.position).z, -1.0f, 1.0f);

        if (CircularSliderPress != null)
        {
            CircularSliderPress(this);
        }

        return(true);
    }
Beispiel #4
0
 void Awake()
 {
     _transform      = transform;
     deltaPosition   = _transform.position - aviator.position;
     localPosition   = VectorOperator.getLocalPosition(aviator, _transform.position);
     aviatorPosition = aviator.position;
 }
Beispiel #5
0
        public void SetParent(Transform upDetailParent, Transform downDetailParent)
        {
            if (!springDownDetail || !springUpDetail || !spring || !downPivot || !upPivot)
            {
                enabled = false;
                return;
            }
            springUpDetail.parent   = upDetailParent;
            springDownDetail.parent = upDetailParent;
            this.downDetailParent   = downDetailParent;
            if (!downDetailParent)
            {
                enabled = false;
                return;
            }

            localPosition = VectorOperator.getLocalPosition(downDetailParent, springDownDetail.position);
        }
Beispiel #6
0
        void OnEnable()
        {
            vehicle = transform.GetComponentInParent <Vehicle>();
            root    = vehicle.transform;

            wheelPivot = transform.parent;
            #if UNITY_EDITOR
            if (!Application.isPlaying)
            {
                if (!body)
                {
                    GameObject wheel     = new GameObject(name + "_Joint");
                    GameObject wheelTurn = null;
                    if (wheelPivot.name.Contains("WheelTurn"))
                    {
                        wheelTurn = new GameObject(name + "_Turn_Joint");
                        steering  = root.GetComponentInChildren <Steering>();
                    }


                    Transform physics = root.Find("Physics");
                    if (!physics)
                    {
                        physics                      = new GameObject("Physics").transform;
                        physics.parent               = root.transform;
                        physics.position             = root.position;
                        physics.transform.localScale = Vector3.one;
                    }

                    wheel.transform.parent   = physics;
                    wheel.transform.position = transform.position;
                    wheel.transform.rotation = root.rotation;

                    if (wheelTurn)
                    {
                        wheelTurn.transform.parent   = physics;
                        wheelTurn.transform.position = wheelPivot.position;
                        lineralDamper = GetComponentInParent <Absorber>();
                        if (!lineralDamper.name.Contains("LineralAbsorber"))
                        {
                            lineralDamper = null;
                        }
                        if (lineralDamper)
                        {
                            wheelTurn.transform.LookAt(wheelTurn.transform.position + steering.transform.up, -steering.transform.forward);
                        }
                        else
                        {
                            wheelTurn.transform.rotation = root.rotation;
                        }
                    }

                    joint        = wheel.AddComponent <HingeJoint>();
                    bodyCollider = wheel.AddComponent <MeshCollider>();
                    body         = wheel.GetComponent <Rigidbody>();

                    PhysicMaterial physicMaterial = (PhysicMaterial)AssetDatabase.LoadAssetAtPath("Assets/Vehicles/WheelSystem/PhysicsMaterials/Wheel.physicMaterial", typeof(PhysicMaterial));

                    parameters   = new Parameters(name, true, true, 2.0f, 0.0f, 0.0f, physicMaterial);
                    joint.anchor = Vector3.zero;
                    joint.axis   = Vector3.right;

                    if (wheelTurn)
                    {
                        turnJoint        = wheelTurn.AddComponent <HingeJoint>();
                        turnBody         = wheelTurn.GetComponent <Rigidbody>();
                        turnBody.mass    = parameters.mass;
                        turnJoint.anchor = Vector3.zero;
                        turnJoint.axis   = Vector3.up;

                        UpdateSteeringParameters();
                    }

                    if (!turnJoint)
                    {
                        Absorber aAbsorber = transform.GetComponentInParent <Absorber>();
                        if (aAbsorber)
                        {
                            connectedBody = aAbsorber.body;
                            absorber      = aAbsorber.transform;
                        }
                        else
                        {
                            connectedBody = root.GetComponent <Rigidbody>();
                            absorber      = root;
                        }
                    }
                    else
                    {
                        Absorber aAbsorber = transform.GetComponentInParent <Absorber>();
                        if (aAbsorber)
                        {
                            turnJoint.connectedBody = aAbsorber.body;
                            absorber = aAbsorber.transform;
                        }
                        else
                        {
                            turnJoint.connectedBody = root.GetComponent <Rigidbody>();
                            absorber = root;
                        }
                        connectedBody = turnBody;
                    }

                    joint.connectedBody = connectedBody;

                    //absorber = connectedBody.transform;

                    horsepower           = 10.0f;
                    maxVelocityMPS       = 55.0f;
                    newtonMeterPerSecond = 746.0f * horsepower;
                    tractionForce        = newtonMeterPerSecond / maxVelocityMPS;
                    CalculateCollider();
                }
            }
            else
            {
            #endif
            if (!absorber)
            {
                enabled = false;
                return;
            }
            newtonMeterPerSecond = 746.0f * horsepower;
            tractionForce        = newtonMeterPerSecond / maxVelocityMPS;
            radius = 0.5f * body.transform.lossyScale.y;
            body.maxAngularVelocity = maxVelocityMPS / radius;

            JointMotor motor = new JointMotor();
            motor.targetVelocity = (180.0f / Mathf.PI) * velocityMPS / radius;
            motor.force          = tractionForce * radius;;
            joint.motor          = motor;

            Absorber aAbsorber = transform.GetComponentInParent <Absorber>();

            if (aAbsorber)
            {
                aAbsorber.OnUpdade += OnUpdade;
            }
            else if (vehicle)
            {
                vehicle.OnUpdade += OnUpdade;
            }

            if (connectedBody)
            {
                localPosition     = VectorOperator.getLocalPosition(absorber, wheelPivot.position);
                wheelPivot.parent = root;
            }
            foreach (var item in root.GetComponentsInChildren <Collider>())
            {
                Physics.IgnoreCollision(bodyCollider, item);
            }

                #if UNITY_EDITOR
        }
                #endif
        }