Esempio n. 1
0
    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.collider.CompareTag("Deadly"))
        {
            print("Collided with other enemy");
            return;
        }
        float current_point = body.GetRelativePoint(relative_point).y;

        if (collision.collider.CompareTag("Walls") && current_point > previous_point && current_point > start_point)
        {
            previous_point = body.GetRelativePoint(body.position).y;
            print("Has collided with top");
            collided_bottom = false;
            collided_top    = true;
        }

        if (collision.collider.CompareTag("Walls") && current_point < start_point && current_point < previous_point)
        {
            previous_point = body.GetRelativePoint(body.position).y;
            print("Has collided with bottom");
            collided_top    = false;
            collided_bottom = true;
        }
    }
Esempio n. 2
0
    void FixedUpdate()
    {
        // Inverted horizontal value so that it maps to clockwise (positive) and counter-clockwise (negative)
        h = -Input.GetAxis("Horizontal");
        Debug.DrawLine(new Vector2(0, 0), new Vector2(-h * 5, 0), Color.blue);
        v = Input.GetAxis("Vertical");

        // Steering
        if (steerable)
        {
            rb.rotation += h * steerPower;
        }

        acceleration = transform.up * v * power;
        rb.AddForce(acceleration);

        // Calculate the sideways drift velocity
        driftForce = new Vector2(transform.InverseTransformVector(rb.velocity).x, 0);
        Debug.DrawLine(rb.position, rb.GetRelativePoint(driftForce * 5), Color.red);

        // Calculate an opposing friction force to counteract drift, limited by a maximum type grip
        frictionForce = Vector2.ClampMagnitude(driftForce * -1 * surfaceFriction, maxTyreFriction);
        Debug.DrawLine(rb.position, rb.GetRelativePoint(frictionForce * 5), Color.green);

        // TODO
        // Improve the friction response to reduce drift at low speeds and stabilise drift at higher speeds

        // Apply the friction force to counteract drift, i.e. so wheels 'roll' forwards/backwards but not sideways
        rb.AddForce(rb.GetRelativeVector(frictionForce), ForceMode2D.Impulse);

        // Draw skidmarks when drift force exceeds maximum friction
        skidmarks.emitting = driftForce.sqrMagnitude > frictionForce.sqrMagnitude * skidmarkThreshold ? true : false;
    }
Esempio n. 3
0
    void FixedUpdate()
    {
        if (rb.velocity.magnitude > maxSpeed)
        {
            Vector2 setSpeed = rb.velocity.normalized * maxSpeed;
            rb.velocity = setSpeed;
        }

        Vector2 forward = new Vector2(0.0f, 0.5f);
        float   steeringRightAngle;

        if (rb.angularVelocity > 0)
        {
            steeringRightAngle = -90;
        }
        else
        {
            steeringRightAngle = 90;
        }

        Vector2 rightAngleFromForward = Quaternion.AngleAxis(steeringRightAngle, Vector3.forward) * forward;

        Debug.DrawLine((Vector3)rb.position, (Vector3)rb.GetRelativePoint(rightAngleFromForward), Color.yellow);

        float driftForce = Vector2.Dot(rb.velocity, rb.GetRelativeVector(rightAngleFromForward.normalized));

        Vector2 relativeForce = (rightAngleFromForward.normalized * -1.0f) * (driftForce * 10.0f);


        Debug.DrawLine((Vector3)rb.position, (Vector3)rb.GetRelativePoint(relativeForce), Color.red);

        rb.AddForce(rb.GetRelativeVector(relativeForce));
    }
    private void ProcessPhysics()
    {
        var steering = _currentVehicle.Performance.Steering;

        Vector2 speed = HandlingObject.transform.up * (_v * _currentVehicle.Performance.Acceleration);

        _vehicleRb.AddForce(speed);

        // Speed vector * Car forward vector in global space
        // Shows how is speed vector equals car forward direction
        // 0 when moving sidewais, positive when moving forward, negative when moving backward
        float direction = Vector2.Dot(_vehicleRb.velocity, _vehicleRb.GetRelativeVector(Vector2.up));

        var curAngle = HandlingObject.transform.rotation.eulerAngles.z;

        _debugLabel.text = "Angle: " + curAngle + " H: " + _h;

        // Moving forward
        if (direction >= 0.0f)
        {
            if (_h >= 0 && (curAngle <= _turnAngle || curAngle > 180) || _h <= 0 && (curAngle >= 360 - _turnAngle || curAngle < 180))
            {
                _vehicleRb.AddTorque((_h * steering) * (_vehicleRb.velocity.magnitude / 10.0f));
            }

            else
            {
                if (curAngle > _turnAngle && curAngle < 180)
                {
                    _vehicleRb.AddTorque(-steering * (_vehicleRb.velocity.magnitude / 10.0f));
                }
                else if (curAngle < 360 - _turnAngle && curAngle > 180)
                {
                    _vehicleRb.AddTorque(steering * (_vehicleRb.velocity.magnitude / 10.0f));
                }
            }
        }

        // Moving backward, we shouldn't do that
        else
        {
            _vehicleRb.AddTorque((-_h * steering) * (_vehicleRb.velocity.magnitude / 10.0f));
        }

        Vector2 forward            = new Vector2(0.0f, 0.5f);
        float   steeringRightAngle = _vehicleRb.angularVelocity > 0 ? -90 : 90;

        Vector2 rightAngleFromForward = Quaternion.AngleAxis(steeringRightAngle, Vector3.forward) * forward;

        Debug.DrawLine((Vector3)_vehicleRb.position, (Vector3)_vehicleRb.GetRelativePoint(rightAngleFromForward), Color.green);
        float   driftForce    = Vector2.Dot(_vehicleRb.velocity, _vehicleRb.GetRelativeVector(rightAngleFromForward.normalized));
        Vector2 relativeForce = (rightAngleFromForward.normalized * -1.0f) * (driftForce * 10.0f);

        Debug.DrawLine((Vector3)_vehicleRb.position, (Vector3)_vehicleRb.GetRelativePoint(relativeForce), Color.red);

        _vehicleRb.AddForce(_vehicleRb.GetRelativeVector(relativeForce));
    }
Esempio n. 5
0
    void FixedUpdate()
    {
        float h = -Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");

        Vector2 speed = transform.up * (v * acceleration);

        rb.AddForce(speed);

        // Engine sound
        if (engineSound.clip != null)
        {
            engineSound.pitch = 1 + Mathf.Abs(v / 2);
        }

        float direction = Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.up));

        if (direction >= 0.0f)
        {
            rb.rotation += h * steering * (rb.velocity.magnitude / 5.0f);
            //rb.AddTorque((h * steering) * (rb.velocity.magnitude / 10.0f));
        }
        else
        {
            rb.rotation -= h * steering * (rb.velocity.magnitude / 5.0f);
            //rb.AddTorque((-h * steering) * (rb.velocity.magnitude / 10.0f));
        }

        Vector2 forward = new Vector2(0.0f, 0.5f);
        float   steeringRightAngle;

        if (rb.angularVelocity > 0)
        {
            steeringRightAngle = -90;
        }
        else
        {
            steeringRightAngle = 90;
        }

        Vector2 rightAngleFromForward = Quaternion.AngleAxis(steeringRightAngle, Vector3.forward) * forward;

        Debug.DrawLine((Vector3)rb.position, (Vector3)rb.GetRelativePoint(rightAngleFromForward), Color.green);

        float driftForce = Vector2.Dot(rb.velocity, rb.GetRelativeVector(rightAngleFromForward.normalized));

        Vector2 relativeForce = (rightAngleFromForward.normalized * -1.0f) * (driftForce * 10.0f);


        Debug.DrawLine((Vector3)rb.position, (Vector3)rb.GetRelativePoint(relativeForce), Color.red);

        rb.AddForce(rb.GetRelativeVector(relativeForce));

        ShowPlayerShooterOrDirectionIndicator();
    }
Esempio n. 6
0
    void FixedUpdate()
    {
        float h = -Input.GetAxis(horAxis);
        float v = Input.GetAxis(verAxis);
        //Debug.Log(v);
        Vector2 speed = transform.up * (v * acceleration);

        rb.AddForce(speed);

        if (rb.velocity.magnitude > maxSpeed)
        {
            rb.velocity = rb.velocity.normalized * maxSpeed;
        }

        float direction = Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.up));

        if (direction >= 0.0f)
        {
            //rb.rotation += h * steering * (rb.velocity.magnitude / 5.0f);
            rb.AddTorque((h * steering) * (rb.velocity.magnitude / 10.0f));
        }
        else
        {
            //rb.rotation -= h * steering * (rb.velocity.magnitude / 5.0f);
            rb.AddTorque((-h * steering) * (rb.velocity.magnitude / 10.0f));
        }

        Vector2 forward = new Vector2(0.0f, 0.5f);
        float   steeringRightAngle;

        if (rb.angularVelocity > 0)
        {
            steeringRightAngle = -90;
        }
        else
        {
            steeringRightAngle = 90;
        }

        Vector2 rightAngleFromForward = Quaternion.AngleAxis(steeringRightAngle, Vector3.forward) * forward;

        Debug.DrawLine((Vector3)rb.position, (Vector3)rb.GetRelativePoint(rightAngleFromForward), Color.green);

        float driftForce = Vector2.Dot(rb.velocity, rb.GetRelativeVector(rightAngleFromForward.normalized));

        Vector2 relativeForce = (rightAngleFromForward.normalized * -1.0f) * (driftForce * 10.0f);


        Debug.DrawLine((Vector3)rb.position, (Vector3)rb.GetRelativePoint(relativeForce), Color.red);

        rb.AddForce(rb.GetRelativeVector(relativeForce));
    }
Esempio n. 7
0
    void FixedUpdate()
    {
        //using these feels more natural & sort of simulates jerk (velocity, acceleration, jerk, snap, crackle, pop)
        h = -Input.GetAxis("Horizontal");
        v = Input.GetAxis("Vertical");

        //move forward & back
        Vector2 speed = transform.up * (v * acceleration);

        body.AddForce(speed);

        //turn left & right
        float direction = Vector2.Dot(body.velocity, body.GetRelativeVector(Vector2.up));

        if (direction >= 0.0f)
        {
            body.rotation += h * steering * (body.velocity.magnitude / 5.0f);
            //body.AddTorque((h * steering) * (body.velocity.magnitude / 10.0f));
        }
        else
        {
            body.rotation -= h * steering * (body.velocity.magnitude / 5.0f);
            //body.AddTorque((-h * steering) * (body.velocity.magnitude / 10.0f));
        }


        //calculate drag
        Vector2 forward = new Vector2(0.0f, 0.5f);
        float   steeringRightAngle;

        if (body.angularVelocity > 0)
        {
            steeringRightAngle = -90;
        }
        else
        {
            steeringRightAngle = 90;
        }
        Vector2 rightAngleFromForward = Quaternion.AngleAxis(steeringRightAngle, Vector3.forward) * forward;

        driftForce = Vector2.Dot(body.velocity, body.GetRelativeVector(rightAngleFromForward.normalized));
        Vector2 relativeForce = (rightAngleFromForward.normalized * -1.0f) * (driftForce * 10.0f);

        body.AddForce(body.GetRelativeVector(relativeForce));


        //drawing debug lines
        topGear = body.velocity.magnitude;
        Debug.DrawLine((Vector3)body.position, (Vector3)body.GetRelativePoint(rightAngleFromForward), Color.green);
        Debug.DrawLine((Vector3)body.position, (Vector3)body.GetRelativePoint(relativeForce), Color.red);
    }
    void FixedUpdate()
    {
        if (!isLocalPlayer)
        {
            return;
        }
        float h = -Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");

        Vector2 speed = transform.up * (v * acceleration);

        rb.AddForce(speed);

        float direction = Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.up));

        if (direction >= 0.0f)
        {
            rb.rotation += h * steering * (rb.velocity.magnitude / 5.0f);
        }
        else
        {
            rb.rotation -= h * steering * (rb.velocity.magnitude / 5.0f);
        }

        Vector2 forward = new Vector2(0.0f, 0.5f);

        float steeringRightAngle;

        if (rb.angularVelocity > 0)
        {
            steeringRightAngle = -90;
        }
        else
        {
            steeringRightAngle = 90;
        }

        Vector2 rightAngleFromForward = Quaternion.AngleAxis(steeringRightAngle, Vector3.forward) * forward;

        Debug.DrawLine((Vector3)rb.position, (Vector3)rb.GetRelativePoint(rightAngleFromForward), Color.green);

        float driftForce = Vector2.Dot(rb.velocity, rb.GetRelativeVector(rightAngleFromForward.normalized));

        Vector2 relativeForce = (rightAngleFromForward.normalized * -1.0f) * (driftForce * 10.0f);


        Debug.DrawLine((Vector3)rb.position, (Vector3)rb.GetRelativePoint(relativeForce), Color.red);

        rb.AddForce(rb.GetRelativeVector(relativeForce));
    }
Esempio n. 9
0
    void FixedUpdate()
    {
        Vector2 myPos   = rigidbody2D.GetRelativePoint(anchor);
        Vector2 bodyPos = connectedAnchor;

        if (connectedBody)
        {
            bodyPos = connectedBody.GetRelativePoint(connectedAnchor);
        }
        float   distance  = (bodyPos - myPos).magnitude;
        Vector2 direction = (bodyPos - myPos);

        direction.Normalize();
        rigidbody2D.AddForceAtPosition(direction * distance * elasticity, myPos);
        if (connectedBody != null)
        {
            connectedBody.rigidbody2D.AddForceAtPosition(direction * -1 * distance * elasticity, bodyPos);
        }
        Debug.DrawLine(myPos, bodyPos, Color.red);
        //Debug.Log("Glue Force = " + (distance * elasticity).ToString());
        if (distance * elasticity > maxForce)
        {
            Destroy(this);
        }
    }
Esempio n. 10
0
    public void ApplyThrust(ref float totalThrust, Rigidbody2D rb2d, float xAxis, Vessel.Throttle throttle)
    {
        if (!this.engineOn.boolValue)
        {
            return;
        }
        xAxis *= (float)(this.part.orientation.x * this.part.orientation.y);
        this.nozzleMove.SetTargetTime(xAxis);
        if (!throttle.throttleOn)
        {
            return;
        }
        if (throttle.throttle == 0f)
        {
            return;
        }
        if (!this.CheckResourceSource(true))
        {
            this.ToggleEngine(false);
            return;
        }
        this.resourceSource.TakeResource(this.resourceConsuption * throttle.throttle * Time.fixedDeltaTime);
        if (this.fuelIcon.bar != null)
        {
            this.fuelIcon.bar.fillAmount = this.resourceSource.resourcePercent;
        }
        float num = this.thrust * throttle.throttle;

        totalThrust += num;
        Vector2 a     = Quaternion.Euler(0f, 0f, base.transform.rotation.eulerAngles.z) * new Vector2(0f, (float)this.part.orientation.y);
        Vector2 force = a * num;
        Vector2 a2    = (base.transform.localPosition + this.part.centerOfMass * this.part.orientation) * num;

        rb2d.AddForceAtPosition(force, rb2d.GetRelativePoint(a2 / num));
        EngineModule.Flame[] array = this.flamesData;
        for (int i = 0; i < array.Length; i++)
        {
            EngineModule.Flame flame = array[i];
            flame.flame.transform.localScale = new Vector3(UnityEngine.Random.Range(flame.sizeBounds.x, flame.sizeBounds.y), UnityEngine.Random.Range(flame.sizeBounds.z, flame.sizeBounds.w), 1f);
        }
        bool flag = false;

        if (Ref.mainVesselTerrainHeight < 100.0)
        {
            RaycastHit2D hit = Physics2D.Raycast(base.transform.position, -a, this.flameLenght, this.particleCollisionMask);
            if (hit)
            {
                flag = true;
                this.deflectedParticle1.particle.transform.localPosition = new Vector3(0f, -hit.distance / 1.5f + 0.45f, 0f);
                this.deflectedParticle2.particle.transform.localPosition = new Vector3(0f, -hit.distance / 1.5f + 0.45f, 0f);
                this.deflectedParticle1.particleMain.startColor          = new Color(1f, 1f, 1f, throttle.throttleRaw * (1f - hit.distance / this.flameLenght));
                this.deflectedParticle2.particleMain.startColor          = new Color(1f, 1f, 1f, throttle.throttleRaw * (1f - hit.distance / this.flameLenght));
            }
        }
        if (this.deflectedParticle1.particle.gameObject.activeSelf != flag)
        {
            this.deflectedParticle1.particle.gameObject.SetActive(flag);
            this.deflectedParticle2.particle.gameObject.SetActive(flag);
        }
    }
        // Use this for calculate
        public override void OnCalculate()
        {
            Rigidbody2D rigidbody2D = _Rigidbody2D.value;

            if (rigidbody2D != null)
            {
                _Result.SetValue(rigidbody2D.GetRelativePoint(_RelativePoint.value));
            }
        }
Esempio n. 12
0
    // Update is called once per frame
    void FixedUpdate()
    {
        Vector2 speed = transform.up * (v * acceleration) * 20;

        RigidBodycar.AddForce(speed);

        float direction = Vector2.Dot(RigidBodycar.velocity, RigidBodycar.GetRelativeVector(Vector2.up));

        if (direction >= 0.0f)
        {
            RigidBodycar.rotation += h * steering * (RigidBodycar.velocity.magnitude / 5.0f);
            //rb.AddTorque((h * steering) * (rb.velocity.magnitude / 10.0f));
        }
        else
        {
            RigidBodycar.rotation -= h * steering * (RigidBodycar.velocity.magnitude / 5.0f);
            //rb.AddTorque((-h * steering) * (rb.velocity.magnitude / 10.0f));
        }

        Vector2 forward = new Vector2(0.0f, 0.5f);
        float   steeringRightAngle;

        if (RigidBodycar.angularVelocity > 0)
        {
            steeringRightAngle = -90;
        }
        else
        {
            steeringRightAngle = 90;
        }

        Vector2 rightAngleFromForward = Quaternion.AngleAxis(steeringRightAngle, Vector3.forward) * forward;

        Debug.DrawLine((Vector3)RigidBodycar.position, (Vector3)RigidBodycar.GetRelativePoint(rightAngleFromForward), Color.green);

        float driftForce = Vector2.Dot(RigidBodycar.velocity, RigidBodycar.GetRelativeVector(rightAngleFromForward.normalized));

        Vector2 relativeForce = (rightAngleFromForward.normalized * -1.0f) * (driftForce * 10.0f);


        Debug.DrawLine((Vector3)RigidBodycar.position, (Vector3)RigidBodycar.GetRelativePoint(relativeForce), Color.red);

        RigidBodycar.AddForce(RigidBodycar.GetRelativeVector(relativeForce));
    }
Esempio n. 13
0
        void FixedUpdate()
        {
            Vector2 speed = transform.up * (_vInput * Acceleration * _rigidbody.mass);

            _rigidbody.AddForce(speed);

            float direction = Vector2.Dot(_rigidbody.velocity, _rigidbody.GetRelativeVector(Vector2.up));

            if (direction >= 0.0f)
            {
                _rigidbody.rotation += _hInput * Steering * (_rigidbody.velocity.magnitude / 5.0f);
                //_rigidbody.AddTorque((_hInput * Steering) * (_rigidbody.velocity.magnitude / 10.0f));
            }
            else
            {
                _rigidbody.rotation -= _hInput * Steering * (_rigidbody.velocity.magnitude / 5.0f);
                //_rigidbody.AddTorque((-_hInput * Steering) * (_rigidbody.velocity.magnitude / 10.0f));
            }

            Vector2 forward = new Vector2(0.0f, 0.5f);
            float   steeringRightAngle;

            if (_rigidbody.angularVelocity > 0)
            {
                steeringRightAngle = -90;
            }
            else
            {
                steeringRightAngle = 90;
            }

            Vector2 rightAngleFromForward = Quaternion.AngleAxis(steeringRightAngle, Vector3.forward) * forward;

            Debug.DrawLine((Vector3)_rigidbody.position, (Vector3)_rigidbody.GetRelativePoint(rightAngleFromForward), Color.green);

            float driftForce = Vector2.Dot(_rigidbody.velocity, _rigidbody.GetRelativeVector(rightAngleFromForward.normalized));

            Vector2 relativeForce = (rightAngleFromForward.normalized * -1.0f) * (driftForce * 10.0f);

            Debug.DrawLine((Vector3)_rigidbody.position, (Vector3)_rigidbody.GetRelativePoint(relativeForce), Color.red);

            _rigidbody.AddForce(_rigidbody.GetRelativeVector(relativeForce));
        }
Esempio n. 14
0
    static int GetRelativePoint(IntPtr L)
    {
        LuaScriptMgr.CheckArgsCount(L, 2);
        Rigidbody2D obj  = LuaScriptMgr.GetNetObject <Rigidbody2D>(L, 1);
        Vector2     arg0 = LuaScriptMgr.GetNetObject <Vector2>(L, 2);
        Vector2     o    = obj.GetRelativePoint(arg0);

        LuaScriptMgr.PushValue(L, o);
        return(1);
    }
Esempio n. 15
0
        void FixedUpdate()
        {
            float inputH = Input.GetAxis("Horizontal");
            float inputV = Input.GetAxis("Vertical");

            float acc = inputV * acceleration;

            rigid.AddForce(transform.up * acc);

            float direction = Vector2.Dot(rigid.velocity, rigid.GetRelativeVector(Vector2.up));

            if (direction >= 0.0f)
            {
                rigid.rotation -= inputH * steering * (rigid.velocity.magnitude / 5.0f);
                //rb.AddTorque((h * steering) * (rb.velocity.magnitude / 10.0f));
            }
            else
            {
                rigid.rotation += inputH * steering * (rigid.velocity.magnitude / 5.0f);
                //rb.AddTorque((-h * steering) * (rb.velocity.magnitude / 10.0f));
            }

            Vector2 forward = new Vector2(0.0f, 0.5f);
            float   steeringRightAngle;

            if (rigid.angularVelocity > 0)
            {
                steeringRightAngle = -90;
            }
            else
            {
                steeringRightAngle = 90;
            }

            Vector2 rightAngleFromForward = Quaternion.AngleAxis(steeringRightAngle, Vector3.forward) * forward;

            Debug.DrawLine(rigid.position, rigid.GetRelativePoint(rightAngleFromForward), Color.green);
            float   driftForce    = Vector2.Dot(rigid.velocity, rigid.GetRelativeVector(rightAngleFromForward.normalized));
            Vector2 relativeForce = (rightAngleFromForward.normalized * -1.0f) * (driftForce * 10.0f);

            Debug.DrawLine(rigid.position, rigid.GetRelativePoint(relativeForce), Color.red);
            rigid.AddForce(rigid.GetRelativeVector(relativeForce));
        }
Esempio n. 16
0
    public void ApplyThrust(ref float totalThrust, Rigidbody2D rb2d, float xAxis, Vessel.Throttle throttle, bool mainVessel)
    {
        if (!this.engineOn.boolValue)
        {
            return;
        }
        xAxis *= (float)(this.part.orientation.x * this.part.orientation.y);
        if (this.nozzleMove != null)
        {
            this.nozzleMove.SetTargetTime(xAxis);
        }
        if (!throttle.throttleOn)
        {
            return;
        }
        if (throttle.throttle == 0f)
        {
            return;
        }
        float num = this.thrust * throttle.throttle;

        totalThrust += num;
        Vector2 a     = Quaternion.Euler(0f, 0f, base.transform.rotation.eulerAngles.z) * new Vector2(0f, (float)this.part.orientation.y);
        Vector2 force = a * num;
        Vector2 a2    = (base.transform.localPosition + this.part.centerOfMass * this.part.orientation) * num;

        rb2d.AddForceAtPosition(force, rb2d.GetRelativePoint(a2 / num));
        if (this.deflectedParticleHolder == null)
        {
            return;
        }
        bool flag = false;

        if (Ref.mainVesselTerrainHeight < 100.0)
        {
            RaycastHit2D hit = Physics2D.Raycast(base.transform.position, -a, this.flameLenght, this.particleCollisionMask);
            if (hit)
            {
                flag = true;
                this.deflectedParticle1.particle.transform.localPosition = new Vector3(0f, -hit.distance / this.deflectedParticle1.particle.transform.lossyScale.y * Mathf.Abs(this.deflectedParticle1.particle.transform.localScale.y), 0f);
                this.deflectedParticle2.particle.transform.localPosition = new Vector3(0f, -hit.distance / this.deflectedParticle2.particle.transform.lossyScale.y * Mathf.Abs(this.deflectedParticle2.particle.transform.localScale.y), 0f);
                this.deflectedParticle1.particleMain.startColor          = new Color(1f, 1f, 1f, throttle.throttleRaw * (1f - hit.distance / this.flameLenght));
                this.deflectedParticle2.particleMain.startColor          = new Color(1f, 1f, 1f, throttle.throttleRaw * (1f - hit.distance / this.flameLenght));
            }
        }
        if (this.deflectedParticle1.particle.gameObject.activeSelf != flag)
        {
            this.deflectedParticle1.particle.gameObject.SetActive(flag);
            this.deflectedParticle2.particle.gameObject.SetActive(flag);
        }
    }
Esempio n. 17
0
    // Update is called once per frame
    void Update()
    {
        velocity = body.GetRelativePointVelocity(relative_point);
        mag      = velocity.magnitude;
        if (mag > TOP_SPEED)
        {
            body.velocity = velocity.normalized * TOP_SPEED;
        }
        // print(Mathf.Sin(t + (body.GetRelativePoint(relative_point).x)));

        change_v      = new Vector2(LEFT.x, Mathf.Sin(t + (body.GetRelativePoint(relative_point).x)));
        body.velocity = change_v;
        t            += Time.deltaTime / 2;
    }
Esempio n. 18
0
    public void Set_Parent(Rigidbody2D i_rb)
    {
        parent_rigidbody = i_rb;

        if (i_rb != null)
        {
            parent_grid       = parent_rigidbody.GetComponent <Grid>();
            last_frame_anchor = parent_rigidbody.GetRelativePoint(ship_anchor);
            Set_Anchor_Point(true);
        }
        else
        {
            parent_grid = null;
        }
    }
Esempio n. 19
0
    public void UseRcs(Rigidbody2D rb2d, Vessel vessel, float horizontalAxis, float rcsInputDirection, bool directionInput, ref double fuelUsed)
    {
        this.effect.SetTargetTime(0f);
        Vector2 vector            = this.effect.transform.TransformDirection(Vector3.left);
        Vector2 vector2           = base.transform.localPosition + this.part.centerOfMass * this.part.orientation;
        Vector2 posToCenterOfMass = rb2d.centerOfMass - vector2;

        if (this.TorqueThrust(vector, posToCenterOfMass, rb2d, vessel, horizontalAxis) || (directionInput && this.DirectionThrust(vector, rcsInputDirection)))
        {
            fuelUsed += (double)this.resourceConsuption * (double)Time.fixedDeltaTime;
            rb2d.AddForceAtPosition(vector * this.thrust, rb2d.GetRelativePoint(vector2));
            this.effect.SetTargetTime(0.9f);
            return;
        }
    }
Esempio n. 20
0
    void FixedUpdate()
    {
        // rotation
        targetTransform = target.transform;
        rotateToTarget();
        var carinfo = GetComponent <Car_info>();


        // forward motion and drift (same as player car), always accelerate
        Vector2 speed = transform.up * (1 * acceleration);

        rb.AddForce(speed);

        float drift = Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.left)) * 2.0f;

        Vector2 relativeForce = Vector2.right * drift;

        Debug.DrawLine(rb.position, rb.GetRelativePoint(relativeForce), Color.green);
        rb.AddForce(rb.GetRelativeVector(relativeForce));

        if (rb.velocity.magnitude > maxSpeed)
        {
            rb.velocity = rb.velocity.normalized * maxSpeed;
        }

        carinfo.drift = drift;

        curSpeed = rb.velocity.magnitude;


        RaycastHit2D hit = CheckRaycast();

        if (hasWeapon == true && weapon == 0)
        {
            if (hit.collider.tag == "Car")
            {
                GameObject go = (GameObject)Instantiate(missile, transform.position + (transform.up * 5), transform.rotation);
                hasWeapon = false;
            }
        }
        if (hasWeapon == true && weapon == 1)
        {
            Invoke("dropOil", Random.Range(0, 10));
            hasWeapon = false;
        }
    }
Esempio n. 21
0
    // Start is called before the first frame update
    void Start()
    {
        TOP_SPEED = 4.0f;
        SPEED     = 1.0f;
        t         = 0.0f;

        relative_point = new Vector2(0.0f, 0.0f);
        body           = GetComponent <Rigidbody2D>();

        start_point = body.GetRelativePoint(relative_point).y;
        UP          = new Vector2(0.0f, SPEED);
        DOWN        = new Vector2(0.0f, -SPEED);
        LEFT        = new Vector2(-SPEED, 0.0f);
        RIGHT       = new Vector2(SPEED, 0.0f);

        collided_top    = false;
        collided_bottom = false;
    }
Esempio n. 22
0
 void FixedUpdate()
 {
     if (splatterCountdown > 0f)
     {
         splatterCountdown -= Time.fixedDeltaTime;
     }
     else if (tankLevel != 0)
     {
         Rigidbody2D body = GetComponent <Rigidbody2D>();
         float       avel = body.angularVelocity;
         if (Mathf.Abs(avel) >= threshold)
         {
             float      distance = avel * scale;
             Vector2    target   = body.GetRelativePoint(sideVector * distance);
             GameObject obj      = Instantiate(splatterPrefabs[Random.Range(0, splatterPrefabs.Length)], target, Quaternion.Euler(0f, 0f, Random.Range(0f, 360f)), null);
             obj.GetComponent <SpriteRenderer>().color = juiceType.color;
             splatterCountdown = splatterPeriod;
             --tankLevel;
         }
     }
 }
Esempio n. 23
0
    void FixedUpdate()
    {
        time = Time.time - Starttime;
        float milliseconds = 100.0f;

        milliseconds -= Time.deltaTime;
        tex.text      = "Time: " + ((int)time / 60).ToString() + ":" + ((int)time % 60).ToString() + ":" + ((Time.time - (int)time % 60) * 100).ToString("f0");//Mathf.RoundToInt (time).ToString () + ":";//+(time-Mathf.RoundToInt(time)).ToString("f2");
        float h = -Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");

        Vector2 speed = transform.up * (v * acceleration);

        rb.AddForce(speed);

        /*float h = Input.GetAxis ("Horizontal");
         * float v = Input.GetAxis ("Vertical");
         * print (v);
         * rb.AddForce (transform.up*v*acceleration);
         * float direction = Vector2.Dot (rb.velocity, rb.GetRelativeVector(Vector2.up));
         *
         * rb.rotation += h * steering*(rb.velocity.magnitude/5.0f);*/


        float direction = Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.up));

        if (direction >= 0.0f)
        {
            rb.rotation += h * steering * (rb.velocity.magnitude / 5.0f);
            //rb.AddTorque((h * steering) * (rb.velocity.magnitude / 10.0f));
        }
        else
        {
            rb.rotation -= h * steering * (rb.velocity.magnitude / 5.0f);
            //rb.AddTorque((-h * steering) * (rb.velocity.magnitude / 10.0f));
        }



        if (rb.angularVelocity > 0)
        {
            steeringRightAngle = -90;
        }
        else
        {
            steeringRightAngle = 90;
        }


        Vector2 forward = new Vector2(0.0f, 0.5f);
        Vector2 rightAngleFromForward = Quaternion.AngleAxis(steeringRightAngle, Vector3.forward) * forward;

        Debug.DrawLine((Vector3)rb.position, (Vector3)rb.GetRelativePoint(rightAngleFromForward), Color.green);

        float driftForce = Vector2.Dot(rb.velocity, rb.GetRelativeVector(rightAngleFromForward.normalized));

        Vector2 relativeForce = (rightAngleFromForward.normalized * -1.0f) * (driftForce * 10.0f);


        Debug.DrawLine((Vector3)rb.position, (Vector3)rb.GetRelativePoint(relativeForce), Color.red);

        rb.AddForce(rb.GetRelativeVector(relativeForce));
        Vector2 offset = cam.transform.position;

        cam.transform.position = new Vector3(transform.position.x, transform.position.y, cam.transform.position.z);
    }
Esempio n. 24
0
    // Update is called once per frame
    void Update()
    {
        float h = 0.0f;
        float v = 0.0f;

        if (owner == Player.Red)
        {
            if (Input.GetKey(KeyCode.W))
            {
                v = 1.0f;
            }
            if (Input.GetKey(KeyCode.S))
            {
                v = -1.0f;
            }
            if (Input.GetKey(KeyCode.A))
            {
                h = 1.0f;
            }
            if (Input.GetKey(KeyCode.D))
            {
                h = -1.0f;
            }
        }

        Vector2 speed = transform.up * (v * acceleration);

        rb.AddForce(speed);

        float direction = Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.up));

        if (direction >= 0.0f)
        {
            rb.rotation += h * steering * (rb.velocity.magnitude / 5.0f);
            //rb.AddTorque((h * steering) * (rb.velocity.magnitude / 10.0f));
        }
        else
        {
            rb.rotation -= h * steering * (rb.velocity.magnitude / 5.0f);
            //rb.AddTorque((-h * steering) * (rb.velocity.magnitude / 10.0f));
        }

        Vector2 forward = new Vector2(0.0f, 0.5f);
        float   steeringRightAngle;

        if (rb.angularVelocity > 0)
        {
            steeringRightAngle = -90;
        }
        else
        {
            steeringRightAngle = 90;
        }

        Vector2 rightAngleFromForward = Quaternion.AngleAxis(steeringRightAngle, Vector3.forward) * forward;

        Debug.DrawLine(rb.position, rb.GetRelativePoint(rightAngleFromForward), Color.green);

        float driftForce = Vector2.Dot(rb.velocity, rb.GetRelativeVector(rightAngleFromForward.normalized));

        Vector2 relativeForce = (rightAngleFromForward.normalized * -1.0f) * (driftForce * 10.0f);


        Debug.DrawLine(rb.position, rb.GetRelativePoint(relativeForce), Color.red);

        rb.AddForce(rb.GetRelativeVector(relativeForce));
    }
Esempio n. 25
0
    // Update is called once per frame
    void FixedUpdate()
    {
        float h;
        float v;
        var   carinfo = GetComponent <Car_info>();

        if (this.gameObject.name == "Player1")
        {
            h = -Input.GetAxis("P_1Horizontal");
            v = Input.GetAxis("P_1Vertical");
        }
        else
        {
            h = -Input.GetAxis("P_2Horizontal");
            v = Input.GetAxis("P_2Vertical");
        }


        Vector2 speed = transform.up * (v * acceleration);

        rb.AddForce(speed);

        float direction = Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.up));

        if (direction >= 0.0f)
        {
            rb.rotation += h * steering * (rb.velocity.magnitude / maxSpeed);
        }
        else
        {
            rb.rotation -= h * steering * (rb.velocity.magnitude / maxSpeed);
        }

        float drift = Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.left)) * 2.0f;

        Vector2 relativeForce = Vector2.right * drift;

        Debug.DrawLine(rb.position, rb.GetRelativePoint(relativeForce), Color.green);
        rb.AddForce(rb.GetRelativeVector(relativeForce));

        if (rb.velocity.magnitude > maxSpeed)
        {
            rb.velocity = rb.velocity.normalized * maxSpeed;
        }



        if (this.gameObject.name == "Player1")
        {
            if (Input.GetAxis("Fire1") > 0)
            {
                if (weapon == 0 && hasWeapon == true)
                {
                    GameObject go = (GameObject)Instantiate(missile, transform.position + (transform.up * 5), transform.rotation);
                    hasWeapon = false;
                }
                else if (weapon == 1 && hasWeapon == true)
                {
                    GameObject go = (GameObject)Instantiate(oil, transform.position - (transform.up * 5), transform.rotation);
                    hasWeapon = false;
                }
            }
        }



        else
        {
            if (Input.GetAxis("Fire2") > 0)
            {
                if (weapon == 0 && hasWeapon == true)
                {
                    GameObject go = (GameObject)Instantiate(missile, transform.position + (transform.up * 5), transform.rotation);
                    hasWeapon = false;
                }
                else if (weapon == 1 && hasWeapon == true)
                {
                    GameObject go = (GameObject)Instantiate(oil, transform.position - (transform.up * 5), transform.rotation);
                    hasWeapon = false;
                }
            }
        }

        carinfo.drift = drift;

        curSpeed = rb.velocity.magnitude;
    }
Esempio n. 26
0
    private void FixedUpdate()
    {
        if (do_relative_movement)
        {
            Vector3Int floor_pos = new Vector3Int();
            floor_pos.x = Mathf.RoundToInt(transform.position.x);
            floor_pos.y = Mathf.RoundToInt(transform.position.y);

            Vector3Int floor_pos_floor = new Vector3Int();
            floor_pos_floor.x = Mathf.FloorToInt(transform.position.x);
            floor_pos_floor.y = Mathf.FloorToInt(transform.position.y);

            if (parent_grid != null)
            {
                if (ship_floor_tilemap.GetTile(parent_grid.WorldToCell(transform.position)) != null)
                {
                    on_ship_floor = true;
                }
                else
                {
                    on_ship_floor = false;
                }
            }
            else
            {
                on_ship_floor = false;
            }

            if (floor_tilemap.GetTile(floor_pos_floor) != null)
            {
                on_floor = true;
            }
            else
            {
                on_floor = false;
            }

            if ((parent_rigidbody != null) && (on_ship_floor))
            {
                is_in_space = false;
                if (!has_entered_ship)
                {
                    last_frame_anchor = parent_rigidbody.GetRelativePoint(ship_anchor);
                    Set_Anchor_Point(true);

                    rb.velocity        = parent_rigidbody.velocity;
                    rb.angularVelocity = parent_rigidbody.angularVelocity;

                    is_anchor_point_set = false;
                }

                has_entered_ship = true;
                parent_speed     = parent_rigidbody.velocity.magnitude;

                Vector2 conversion_pos = parent_rigidbody.GetPoint(new Vector2(transform.position.x, transform.position.y));

                if (!on_ship_floor)
                {
                    last_frame_anchor = parent_rigidbody.GetRelativePoint(ship_anchor);
                    Set_Anchor_Point(true);
                }

                if (on_ship_floor)
                {
                    //if (last_frame_anchor != parent_rigidbody.GetRelativePoint(ship_anchor))
                    //{
                    if (is_anchor_point_set)
                    {
                        rb.velocity        = ((parent_rigidbody.GetRelativePoint(ship_anchor) - last_frame_anchor) * anchor_constant);
                        rb.angularVelocity = parent_rigidbody.angularVelocity;
                    }
                    else
                    {
                        is_anchor_point_set = true;
                    }

                    last_frame_anchor = parent_rigidbody.GetRelativePoint(ship_anchor);
                    //}

                    if (Vector2.Distance(parent_rigidbody.GetRelativePoint(ship_anchor), transform.position) > 0.5f)
                    {
                        Set_Anchor_Point(true);
                        is_anchor_point_set = false;
                    }
                }
            }
            else if (!on_floor && !on_ship_floor)
            {
                if (!is_in_space && !has_entered_ship)
                {
                    rb.angularVelocity = rb.angularVelocity / drag_force;
                }

                rb.angularVelocity = 0.0f;
                has_entered_ship   = false;
                is_in_space        = true;
            }
            else
            {
                rb.velocity        = new Vector2(0.0f, 0.0f);
                rb.angularVelocity = 0.0f;
                has_entered_ship   = false;
                is_in_space        = false;
            }
        }
    }