예제 #1
0
    protected virtual void FixedUpdate()
    {
        if (!Stunned)
        {
            if (directionOfMovement == 1) // Right
            {
                xMovementDirection = Vector2.right;
                transform.rotation = new Quaternion(transform.rotation.x, 180, 0, 0);
            }
            else if (directionOfMovement == -1) // Left
            {
                xMovementDirection = Vector2.left;
                transform.rotation = new Quaternion(transform.rotation.x, 0, 0, 0);
            }

            if (Mathf.Abs(xDistanceFromPlayer) < minimumRange && Mathf.Abs(yDistanceFromPlayer) < minimumRange + 3 && IsGrounded())
            {
                RB.velocity = new Vector2(0, RB.velocity.y);
            }
            else
            {
                RB.AddForce(new Vector2(xMovementDirection.x * speed, 0), ForceMode2D.Impulse);
            }
        }
    }
예제 #2
0
 protected void Jump()
 {
     if (DG.getGrounded())
     {
         RB.AddForce(Vector2.up * JumpStrength, ForceMode2D.Impulse);
     }
 }
예제 #3
0
    void Update()
    {
        // Go through the "rigidBodyObjects" list
        foreach (Rigidbody RB in rigidBodyObjects)
        {
            // Resultant Force over each rigid body object set to zero at each frame
            Vector3 resultForce = Vector3.zero;
            foreach (Rigidbody RB2 in rigidBodyObjects)
            {
                //  Calculates the force over each object by all the other objects
                if (RB.name != RB2.name)
                {
                    Vector3 forceDirection = RB.transform.position - RB2.transform.position;
                    // Summarises each force in the resultant force
                    resultForce += GC * forceDirection.normalized * ((RB.mass * RB2.mass) / (forceDirection.magnitude * forceDirection.magnitude));
                }
            }
            // Add the resultant force into each rigid body object
            RB.AddForce(-resultForce);
        }

        #region Optional - Center of Mass

        centerOfMassObject.transform.position = centerOfMassObject.transform.position;

        #endregion
    }
예제 #4
0
    public void TakeDamage(int damage, Vector2 knockBackDir, float knockBackForce)
    {
        CurrHealth -= damage;

        if (CurrHealth <= 0)
        {
            Die();
        }
        else
        {
            RB.velocity = Vector2.zero;
            RB.AddForce(knockBackDir * knockBackForce, ForceMode2D.Impulse);
        }
        if (_App.SoundManager)
        {
            _App.SoundManager.Play(_sfx.Get("Hurt").audio);
        }

        if (Anim)
        {
            Anim.SetTrigger("Hit");
        }

        if (_App.Instance)
        {
            _App.Instance.Snooze();
        }
    }
예제 #5
0
    private void HandleBoomJump()
    {
        Movement();
        if (canJump)
        {
            canJump = !canJump;
            OnPlayerJump?.Invoke(canJump);
            ExplosionAnimator.SetBool("isTriggered", true);
            // Trigger Explosion Sound Here
            BoomJumpSource.Play();
            RB.velocity = new Vector2(RB.velocity.x, 0);
            RB.AddForce(Vector2.up * explosiveForce, ForceMode2D.Impulse);
            // Damage player
            DamagePlayer(explosiveDamage);
        }

        Collider2D[] cols = Physics2D.OverlapCapsuleAll(ExplosionCollider.bounds.center, ExplosionCollider.size, CapsuleDirection2D.Horizontal, 0);
        foreach (Collider2D col in cols)
        {
            if (col.GetComponent <TNT>() != null)
            {
                TNT tnt = col.GetComponent <TNT>();
                tnt.Detonate(true);
            }
        }


        if (PlayerAnimator.GetCurrentAnimatorStateInfo(0).normalizedTime > 1)
        {
            if (!grounded)
            {
                SetState(PlayerStates.IN_AIR);
            }
        }
    }
예제 #6
0
    public void Move(Vector3 direction)
    {
        // TODO please recycle your vectors.

        // calculate the velocity
        Vector3 targetVelocity = new Vector3(direction.x, 0, direction.y);

        targetVelocity  = transform.TransformDirection(targetVelocity);
        targetVelocity *= speed;

        // apply force to system
        Vector3 velocity       = RB.velocity;
        Vector3 velocityChange = (targetVelocity - velocity);

        velocityChange.x = 0f;
        velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
        velocityChange.y = 0f;
        RB.AddForce(velocityChange, ForceMode.VelocityChange);

        // TODO make sure to apply forward Lerp force from jump -> idle | walk

        // TODO implement ground check algo for `bool grounded`

        // TODO do not strafe left and right -- use rotate around instead.

        // TODO implement the jump algo -- but in the jump state

        // TODO allow left and right lean amount while in air

        // TODO implement proper gravity algorithm

        //RB.AddForce(new Vector3(0, -gravity * RB.mass, 0)); //gravity
    }
예제 #7
0
        protected override void Initalize()
        {
            Vector3 initForce  = new Vector3(Random.Range(-1f, 1f), 0, Random.Range(-1f, 1f)).normalized *Random.Range(minStartingForce, maxStartingForce);
            Vector3 initTorque = new Vector3(0, 1, 0) * Random.Range(minStartingTorque, maxStartingTorque);

            RB.AddForce(initForce);
            RB.AddTorque(initTorque);
        }
예제 #8
0
 private void Jump(bool doJump, float jumpSpeed)
 {
     if (doJump)
     {
         RB.AddForce(0, jumpSpeed, 0, ForceMode.Impulse);
         anim.SetTrigger("Jump");
     }
 }
예제 #9
0
    IEnumerator MoveOnCollision(float duration)
    {
        Stunned = true;
        RB.AddForce(new Vector2(-directionOfMovement * 5, 5), ForceMode2D.Impulse);
        yield return(new WaitForSeconds(duration));

        Stunned = false;
    }
예제 #10
0
파일: ResSpawn.cs 프로젝트: Metrim1/FI
    void ResSpawner()
    {
        Rigidbody RB;

        RB = Instantiate(stonePref, transform.position, Quaternion.identity) as Rigidbody;
        RB.AddForce(heart.position - RB.transform.position);
        resConuter += 1;
    }
예제 #11
0
 public void ResetParams()
 {
     RB.position = StartPosition;
     //RB.rotation=Quaternion.identity;
     RB.velocity           = Vector3.zero;
     transform.eulerAngles = new Vector3(0, Random.Range(0, 360), 0);
     RB.AddForce(transform.forward * 500);
 }
예제 #12
0
    protected void InduceGravity()
    {
        Vector3 directionToPlanet = planet.transform.position - transform.position;

        directionToPlanet.Normalize();
        Vector3 gravityAcc = directionToPlanet * planet.GetGravity();

        RB.AddForce(gravityAcc, ForceMode2D.Force);
    }
예제 #13
0
        public void ActivateThrusters()
        {
            if (!RB)
            {
                return;
            }

            RB.AddForce(transform.forward * ThrustAmt);
        }
예제 #14
0
    IEnumerator MoveAroundObstruction(float duration)
    {
        Stunned             = true;
        lookingForPlatforms = false;
        RB.AddForce(new Vector2(xMovementDirection.x * 0.1f, 0), ForceMode2D.Impulse);
        yield return(new WaitForSeconds(duration));

        Stunned             = false;
        lookingForPlatforms = true;
    }
예제 #15
0
        protected override void UpdateMovement()
        {
            if (RB == null)
            {
                return;
            }


            RB.AddForce(transform.forward * Thrust);
        }
예제 #16
0
 // Update is called once per frame
 void Update()
 {
     if (Input.GetMouseButtonDown(0) && isFlying && eggCount-- > 0)
     {
         Vector3 direction = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
         Egg     egg       = Instantiate(eggGO, transform.position + direction.normalized * EGG_DISTANCE, Quaternion.identity).GetComponent <Egg>();
         egg.Push(EGG_FORCE * RB.mass * direction, ForceMode2D.Impulse);
         RB.AddForce(EGG_FORCE * RB.mass * -direction, ForceMode2D.Impulse);
     }
 }
예제 #17
0
 protected override void Jump()
 {
     if (Time.time > jumpDelay)
     {
         if (IsGrounded())
         {
             jumpDelay = Time.time + jumpCooldown;
             RB.AddForce(new Vector2(0, jumpStrength), ForceMode2D.Impulse);
         }
     }
 }
예제 #18
0
 private void Jump()
 {
     if (_App.SoundManager)
     {
         _App.SoundManager.Play(_sfx.Get("Jump").audio);
     }
     RB.velocity = Vector2.zero;
     RB.AddForce(Vector2.up * _jumpForce);
     _jumpRequest = false;
     IsGrounded   = false;
 }
예제 #19
0
 protected override void Jump()
 {
     if (Time.time > jumpDelay)
     {
         if (IsGrounded())
         {
             jumpDelay = Time.time + jumpCooldown;
             RB.AddForce(new Vector2(0, jumpStrength), ForceMode2D.Impulse);
             characterAnim.SetBool("IsJumping", true);
         }
     }
 }
예제 #20
0
    //Hold all movement controls -NG
    private void HorizontalMovement()
    {
        if (Input.GetKey(left))
        {
            if (iceCheck)
            {
                RB.AddForce(new Vector2(-moveSpeed * 0.8f, RB.velocity.y), ForceMode2D.Force);
            }
            else
            {
                RB.velocity = new Vector2(-moveSpeed, RB.velocity.y);
            }
        }

        else if (Input.GetKey(right))
        {
            if (iceCheck)
            {
                RB.AddForce(new Vector2(moveSpeed * 0.8f, RB.velocity.y), ForceMode2D.Force);
            }

            else
            {
                RB.velocity = new Vector2(moveSpeed, RB.velocity.y);
            }
        }

        else
        {
            if (iceCheck)
            {
                RB.velocity = new Vector2(RB.velocity.x * iceSlide, RB.velocity.y);
            }
            if (iceLaunch.Launching)
            {
                Debug.Log("Launching the player");
                if (iceLaunch.LaunchRight == true)
                {
                    RB.velocity = new Vector2(iceLaunch.LaunchPower.x, iceLaunch.LaunchPower.y);
                }
                else if (iceLaunch.LaunchRight == false)
                {
                    RB.velocity = new Vector2(-iceLaunch.LaunchPower.x, iceLaunch.LaunchPower.y);
                }
            }
            else
            {
                RB.velocity = new Vector2(0, RB.velocity.y);
            }
        }
    }
예제 #21
0
 void OnCollisionEnter(Collision col)
 {
     Debug.Log("Trigger");
     if (col.collider.tag == "Player" && !triggered)
     {
         triggered = true;
         foreach (Rigidbody RB in RBs)
         {
             RB.isKinematic = false;
             RB.AddForce(Random.rotation.ToEulerAngles() * 100);
         }
         GetComponent <Collider>().enabled = false;
     }
 }
예제 #22
0
    public void FixedUpdate()
    {
        GetInput();
        RB.AddForce(-AirDragCoeff * RB.velocity * RB.velocity.magnitude);          //air drag (quadratic)
        RB.AddForce(-AirDownForceCoeff * RB.velocity.sqrMagnitude * transform.up); //downforce (quadratic)
        RB.AddForceAtPosition(-TireDragCoeff * RB.velocity, transform.position);   //tire drag (Linear)

        SetGearRatio();
        SetRPM();
        ApplySteer();
        ApplyTorque();
        TractionControl();
        SetMileTick();
    }
예제 #23
0
        public void Hurt(Vector2 KnockBack) 
        {
            StartCoroutine(FlashPlayer());
            _audio.Play(_audio.Hurt);
            int oldHealth = Health;

            Health--;

            EventSystem.Current.PlayerHurt(oldHealth, Health);

            if (Health < 1) {
                Kill();
            } else 
                RB.AddForce(KnockBack);
        }
예제 #24
0
파일: EnemyShot.cs 프로젝트: Metrim1/FI
 void Shot()
 {
     if (currDist < 12f)
     {
         shotPoint = shotT.position;
         Rigidbody RB;
         RB = Instantiate(shot, shotPoint, Quaternion.identity) as Rigidbody;
         RB.AddForce((heart.transform.position - RB.transform.position) * 10);
         Destroy(RB.gameObject, 1f);
     }
     else
     {
         return;
     }
 }
예제 #25
0
    IEnumerator UpAndDownFlight(float alternatingDelay)
    {
        coroutineRunning = true;
        RB.AddForce(new Vector2(0, 1f), ForceMode2D.Impulse);

        yield return(new WaitForSeconds(alternatingDelay));

        RB.velocity = new Vector2(RB.velocity.x, 0);
        RB.AddForce(new Vector2(0, -1f), ForceMode2D.Impulse);

        yield return(new WaitForSeconds(alternatingDelay));

        RB.velocity      = new Vector2(RB.velocity.x, 0);
        coroutineRunning = false;
    }
예제 #26
0
        public void Animate()
        {
            Vector3    pos = transform.position;
            Quaternion rot = RB.rotation;
            float      s   = StrengthAtDistance.Evaluate(math.distance(Cached.Position, pos) / Radius);
            Vector3    f   = math.normalize((Cached.Position - pos)) * s * Time.deltaTime * Strength;

            if (f.magnitude > 0f)
            {
                RB.AddForce(f);
            }

            quaternion r = Quaternion.Slerp(RB.rotation, Cached.Rotation, s * Time.deltaTime * Strength);

            RB.MoveRotation(r);
        }
    private void PlayerMovement()
    {
        if (Stunned || dodging)
        {
            movementEnabled = false;
        }

        if (movementEnabled)
        {
            xMovement = (Input.GetAxis("Horizontal"));
            characterAnim.SetFloat("Speed", Mathf.Abs(xMovement));

            Vector2 movement = new Vector2(xMovement * speed, 0);

            if (xMovement != 0)
            {
                RB.AddForce(movement, ForceMode2D.Impulse);
            }
            else if (IsGrounded())
            {
                RB.velocity = new Vector2(0, RB.velocity.y);
            }

            if (xMovement > 0) // Moving Right
            {
                transform.rotation = new Quaternion(transform.rotation.x, 0, 0, 0);
                xMovementDirection = Vector2.right;
            }
            else if (xMovement < 0) // Moving Left
            {
                transform.rotation = new Quaternion(transform.rotation.x, 180, 0, 0);
                xMovementDirection = Vector2.left;
            }
        }
        else
        {
            if (Time.time > stunduration)
            {
                stunduration = Time.time + stunCooldown;

                if (IsGrounded() && (!Stunned || !dodging))
                {
                    movementEnabled = true;
                }
            }
        }
    }
예제 #28
0
    protected void OnTriggerEnter2D(Collider2D coll)
    {
        Character player = coll.GetComponent <Character>();

        if (player)
        {
            var knockBackDir = transform.position - player.transform.position;
            knockBackDir.Normalize();
            player.TakeDamage(_damage, -knockBackDir, _knockBackForce);

            if (RB)
            {
                RB.velocity = Vector2.zero;
                RB.AddForce(knockBackDir * _knockBackForce, ForceMode2D.Impulse);
            }
        }
    }
예제 #29
0
    // Update is called once per frame
    void Update()
    {
        time += Time.deltaTime;



        RaycastHit hit;

        Physics.Raycast(cam.transform.position, cam.transform.forward, out hit);


        if (hand.transform.childCount > 0)
        {
            for (int i = 0; i < hand.transform.childCount; i++)
            {
                if (hand.transform.GetChild(i).transform.gameObject.name == "gun")
                {
                    Debug.Log("we through");
                    if (time >= interpolationPeriod)
                    {
                        time  = 0.0f;
                        shoot = true;
                    }
                    Vector3 diff = hand.transform.GetChild(i).transform.position - aim.transform.position;
                    hand.transform.GetChild(i).transform.rotation = Quaternion.LookRotation(diff);
                    hand.transform.GetChild(i).transform.Rotate(0, 180, 0);
                }
            }
        }

        if (shoot == true)
        {
            if (Input.GetMouseButtonDown(0))
            {
                GetComponent <AudioSource>().Play();
                GameObject tempBullet;
                tempBullet = Instantiate(Bullet, BulletSpawn.transform.position, BulletSpawn.transform.rotation) as GameObject;
                tempBullet.transform.Rotate(Vector3.left * 90);
                Rigidbody RB;
                RB = tempBullet.GetComponent <Rigidbody>();
                RB.AddForce(transform.forward * BulletSpeed);
                Destroy(tempBullet, 3);
                shoot = false;
            }
        }
    }
예제 #30
0
        public void Hurt(Vector2 KnockBack)
        {
            _audio.Play(_audio.Hurt);
            int oldHealth = Health;

            Health--;

            EventSystem.Current.PlayerHurt(oldHealth, Health);

            if (Health < 1)
            {
                Kill();
            }
            else
            {
                RB.AddForce(KnockBack);
            }
        }