예제 #1
0
	// Update is called once per frame
	void Update () {
		// Create a spring and set strength and damper to it
		spring = new JointSpring();
		spring.spring = hitStrength;
		spring.damper = flipDamper;

		//Keyboard input. Checked keys explicitly so touch events do not register.
		if (Input.GetKey ("a") || Input.GetKey("d")) {
			// If a key is pressed, move the paddle to pressed position, otherwise move the paddle to rest position
			if (Input.GetAxis (inputName) == 1) {
				spring.targetPosition = pressedPosition;
			} else {
				spring.targetPosition = restPosition;
			}
		}
		//Touch input
		else if (Input.touchCount > 0) {
			foreach (Touch touch in Input.touches) {
				//For the left half of the screen
				if (touch.position.x < Screen.width / 2) {
					if (gameObject.name == "Flipper l") {
						spring.targetPosition = pressedPosition;
					}
				} else if (touch.position.x > Screen.width / 2 && plung.getPressedState () == false) {
					if (gameObject.name == "Flipper R") {
						spring.targetPosition = pressedPosition;
					}
				}
			}
		} else {
			spring.targetPosition = restPosition;
		}
		// Set the newly defined spring for the hinge and set it using limits.
		hinge.spring = spring;
	}
예제 #2
0
    // Update is called once per frame
    void Update()
    {
        if (trap.trapStatus() == false && plung.getPressedState() == false && Input.acceleration.magnitude > 1.3f)
        {
            shakeActive = true;
            //0.82f and 4f are offsets to center the shake
            transform.position = new Vector3(Mathf.Sin(Time.time * speed) * amount - 0.82f, transform.position.y, Mathf.Sin(Time.time * speed) * 0.01f + 4f);

            //add force to ball
            float directionZ = Random.Range(-1f, 1f);
            ballBody.AddForce(new Vector3(directionZ, 0, 0) * force);
        }
        else
        {
            shakeActive        = false;
            transform.position = originalPos;
        }
    }
예제 #3
0
    // Update is called once per frame
    void FixedUpdate()
    {
        //Keyboard input. Checked key explicitly so touch events do not register.
        if (Input.GetKey("s"))
        {
            // if s is pressed
            if (Input.GetAxis(inputName) == 1)
            {
                pressed = true;
                // If current position of the plunger is greater than the max position
                if (current > max)
                {
                    // free Z and move the plunger while reducing position and adding energy to plunger
                    //=> plunger moves downwards because of this

                    plunger.constraints = origConstr;
                    plunger.MovePosition(transform.position - transform.forward * Time.deltaTime * plungerLoadSpeed);
                    currentPos    = plunger.position.z;
                    plungerEnergy = plungerEnergy + plungerEnergyAddition;
                    curPos        = plunger.position; // Store current position
                    current      -= 1;                // Integer value marking the position of the plunger
                }
                else
                {
                    // Freeze y and z and set position to current position
                    plunger.constraints = RigidbodyConstraints.FreezePositionZ;
                    plunger.constraints = RigidbodyConstraints.FreezePositionY;
                    plunger.position    = curPos;
                }
            }
            else
            {
                if (pressed)
                {
                    // Applies once an impulse to the plunger that in turn applies the force to the ball if the plunger and the ball collide.
                    if (!forceApplied)
                    {
                        plunger.AddForce(transform.forward * plungerEnergy * damper);


                        forceApplied = true;
                    }
                    // if plunger going up
                    if (current < min && forceApplied)
                    {
                        // release Z, move and add to position while reducing energy
                        plunger.constraints = origConstr;

                        plunger.MovePosition(transform.position + new Vector3(0, 0, plungerEnergy * damper) * Time.deltaTime);

                        currentPos = plunger.position.z;
                        // Change current indicator and plunger energy only if plunger energy is higher than zero.
                        if (plungerEnergy > 0)
                        {
                            plungerEnergy -= plungerEnergyAddition;
                            current       += 1 * (int)plungerEnergy;
                        }
                    }
                    else
                    {
                        // freeze y and z set position to rest and energy to 0.
                        plunger.constraints = RigidbodyConstraints.FreezePositionZ;
                        plunger.constraints = RigidbodyConstraints.FreezePositionY;
                        plungerEnergy       = 0f;
                        plunger.position    = restPos;
                        curPos       = restPos;
                        current      = min - 1;
                        currentPos   = plunger.position.z;
                        forceApplied = false;
                        pressed      = false;
                    }
                }
            }
        }
        //Touch input
        else if (plungB.getPressedState() == true)              //Attach PlungerBase script to plungerBase gameobject!
        //When plunger base is pressed
        {
            if (Input.touchCount > 0)
            {
                pressed = true;
                // If current position of the plunger is greater than the max position
                if (current > max)
                {
                    // free Z and move the plunger while reducing position and adding energy to plunger
                    //=> plunger moves downwards because of this

                    plunger.constraints = origConstr;
                    plunger.MovePosition(transform.position - transform.forward * Time.deltaTime);
                    currentPos    = plunger.position.z;
                    plungerEnergy = plungerEnergy + plungerEnergyAddition;
                    curPos        = plunger.position; // Store current position
                    current      -= 1;                // Integer value marking the position of the plunger
                }
                else
                {
                    // Freeze y and z and set position to current position
                    plunger.constraints = RigidbodyConstraints.FreezePositionZ;
                    plunger.constraints = RigidbodyConstraints.FreezePositionY;
                    plunger.position    = curPos;
                }
            }
            else
            {
                if (pressed)
                {
                    // Applies once an impulse to the plunger that in turn applies the force to the ball if the plunger and the ball collide.
                    if (!forceApplied)
                    {
                        plunger.AddForce(transform.forward * plungerEnergy * damper);

                        forceApplied = true;
                    }
                    // if plunger going up
                    if (current < min && forceApplied)
                    {
                        // release Z, move and add to position while reducing energy
                        plunger.constraints = origConstr;

                        plunger.MovePosition(transform.position + new Vector3(0, 0, plungerEnergy * damper) * Time.deltaTime);

                        currentPos = plunger.position.z;
                        // Change current indicator and plunger energy only if plunger energy is higher than zero.
                        if (plungerEnergy > 0)
                        {
                            plungerEnergy -= plungerEnergyAddition;
                            current       += 1 * (int)plungerEnergy;
                        }
                    }
                    else
                    {
                        // freeze y and z set position to rest and energy to 0.
                        plunger.constraints = RigidbodyConstraints.FreezePositionZ;
                        plunger.constraints = RigidbodyConstraints.FreezePositionY;
                        plungerEnergy       = 0f;
                        plunger.position    = restPos;
                        curPos       = restPos;
                        current      = min - 1;
                        currentPos   = plunger.position.z;
                        forceApplied = false;
                        pressed      = false;
                    }
                }
            }
        }
        else
        {
            if (pressed)
            {
                // Applies once an impulse to the plunger that in turn applies the force to the ball if the plunger and the ball collide.
                if (!forceApplied)
                {
                    plunger.AddForce(transform.forward * plungerEnergy * damper);

                    forceApplied = true;
                }
                // if plunger going up
                if (current < min && forceApplied)
                {
                    // release Z, move and add to position while reducing energy
                    plunger.constraints = origConstr;

                    plunger.MovePosition(transform.position + new Vector3(0, 0, plungerEnergy * damper) * Time.deltaTime);

                    currentPos = plunger.position.z;
                    // Change current indicator and plunger energy only if plunger energy is higher than zero.
                    if (plungerEnergy > 0)
                    {
                        plungerEnergy -= plungerEnergyAddition;
                        current       += 1 * (int)plungerEnergy;
                    }
                }
                else
                {
                    // freeze y and z set position to rest and energy to 0.
                    plunger.constraints = RigidbodyConstraints.FreezePositionZ;
                    plunger.constraints = RigidbodyConstraints.FreezePositionY;
                    plungerEnergy       = 0f;
                    plunger.position    = restPos;
                    curPos       = restPos;
                    current      = min - 1;
                    currentPos   = plunger.position.z;
                    forceApplied = false;
                    pressed      = false;
                }
            }
        }
    }