private void OnCollisionEnter(Collision collision) { if (collision.collider.tag == "Player") { SoundRandomController.Trigger(soundController); } }
// Update is called once per frame private void Update() { dirY = Input.GetAxis("Vertical") * moveSpeed; if (Health <= 0) { Destroy(gameObject); } //IF the Sprint key is pressed then set the moveSpeed to 4 if (Input.GetButtonDown("Sprint")) { moveSpeed = sprintSpeed; CamerController.smoothSpeed = sprintSpeed; } //If the sprint key is released then set the moveSpeed to 2 if (Input.GetButtonUp("Sprint")) { moveSpeed = normalSpeed; CamerController.smoothSpeed = normalSpeed; } //Gets the Input of the L/R , A/D keys and puts it into a variable that we can use later. var movementHor = Input.GetAxis("Horizontal"); /*Takes the position of the object, starts to move it in 3 Vectors, multiplies it by Time.deltaTime to smooth * it out, and multiplies it by moveSpeed. NOTE: if any one of these variable is equal to 0 the object does not move.*/ transform.position += new Vector3(movementHor, 0, 0) * Time.deltaTime * moveSpeed; /*If the jump key is pressed and the objects velocity is less than velocity - then take our Rigidbody2d and add a * force along a Vector 2 with no movement on the x axis and some movement on the y axis as defended by jumpForce also * make sure that this force is an Impulse so that it happens all at once rather that gradually over time.*/ if (Input.GetButtonDown("Jump") && Mathf.Abs(rg2D.velocity.y) < velocity) { rg2D.AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse); SoundRandomController.Trigger(JumpSoundController); } }
// this is a really simple approach to updating wheels // here we simulate a rear wheel drive car and assume that the car is perfectly symmetric at local zero // this helps us to figure our which wheels are front ones and which are rear public void Update() { float angle = maxAngle * Input.GetAxis(Inputs[0]); float torque = maxTorque * ((1.0f + Input.GetAxis(Inputs[2])) * -1) + maxTorque * (1.0f + Input.GetAxis(Inputs[1])); if (_jumpCooldown > 0) { _jumpCooldown -= Time.deltaTime; } //check is any of the wheels are grounded to perform a jump bool grounded = false; foreach (var wheel in wheels) { if (wheel.isGrounded) { grounded = true; break; } } //FlipCar(grounded); if (Input.GetButtonDown(Inputs[3]) && _jumpCooldown < 0) { if (!grounded) { FlipCar(); } else { _jumpVec = JumpDirection * JumpPower; _rig.AddRelativeForce(_jumpVec, ForceMode.Impulse); } _jumpCooldown = JumpCooldown; SoundRandomController.Trigger(jumpSoundController); } foreach (WheelCollider wheel in wheels) { // a simple car where front wheels steer while rear ones drive if (wheel.transform.localPosition.z > 0) { wheel.steerAngle = angle; } if (wheel.transform.localPosition.z < 0) { wheel.motorTorque = (torque / (Mathf.Abs(wheel.rpm / 3) + 1)); } //Debug.Log(string.Format("{0}, {1},{2}",wheel.rpm, torque, torque/Mathf.Abs(wheel.rpm)+1)); // update visual wheels if any if (wheelShape) { Quaternion q; Vector3 p; wheel.GetWorldPose(out p, out q); // assume that the only child of the wheelcollider is the wheel shape Transform shapeTransform = wheel.transform.GetChild(0); shapeTransform.position = p; shapeTransform.rotation = q; } } }