dj3dub
April 4, 2013, 9:59pm
1
I’m looking to modify the player damage. What I want to do is have a short amount of time where the player does not take damage,say 3 seconds for example. I’m thinking along the lines of what was used in Super Mario games where if you had the mushroom for mario he would blink for a short duration and was unable to take damage for a few seconds. Thanks. Here is what I have so far:
if(failHits >= 3)
{
failHits = 0;
AkSoundEngine.PostEvent("Stop_Gameplay", gameObject);
Application.LoadLevel(2);
}
if (failHits == 1)
{
bonusScore = 1500;
}
}
}
dj3dub:
I’m looking to modify the player damage. What I want to do is have a short amount of time where the player does not take damage,say 3 seconds for example. I’m thinking along the lines of what was used in Super Mario games where if you had the mushroom for mario he would blink for a short duration and was unable to take damage for a few seconds. Thanks. Here is what I have so far:
if(failHits >= 3)
{
failHits = 0;
AkSoundEngine.PostEvent("Stop_Gameplay", gameObject);
Application.LoadLevel(2);
}
if (failHits == 1)
{
bonusScore = 1500;
}
}
}
Just do something simple like this.
function DamagePlayer () {
if(invincible == false) {
//deal damage to player like normal
}
}
function ActivateInvincible () {
invincible = true;
yield WaitForSeconds(3);
invincible = false;
}
dj3dub
April 11, 2013, 9:23pm
3
jessee03:
Just do something simple like this.
function DamagePlayer () {
if(invincible == false) {
//deal damage to player like normal
}
}
function ActivateInvincible () {
invincible = true;
yield WaitForSeconds(3);
invincible = false;
}
This is what I came up with:
using UnityEngine;
using System.Collections;
public class Jumpscript : MonoBehaviour {
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
public bool isScared = false;
private Vector3 moveDirection = Vector3.zero;
public bool jumpGo = false;
public float boostSpeed = 0f;
public CharacterController controller;
public static bool jumpLanded = false;
public GameObject jumpCheck;
public bool jumpBoost = false;
public JumpPowerUp jumpPowerUp;
private float forwardSpeed = 0;
public float strafe = 10f;
public float announcedSpeed = 0f;
public float speedForward = 0f;
public alphaCutoff canBoost;
public bool charging = false;
public float boostedSpeed = 25;
public CameraRearView rearSpeed;
public bool canTakeDamage = true;
void Start ()
{
controller = GetComponent<CharacterController>();
jumpPowerUp = GameObject.Find("Player").GetComponent<JumpPowerUp>();
canBoost = GameObject.Find("ChargeBar").GetComponent<alphaCutoff>();
rearSpeed = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<CameraRearView>();
}
void Update()
{
// Debug.Log(wIsClicked);
//Debug.Log("Grounded: " + controller.isGrounded);
if (controller.isGrounded)
{
moveDirection.z = 0;
moveDirection.y = 0;
if (Input.GetButton("Jump"))
{
if (jumpBoost == true)
{
jumpPowerUp.jumpTimer--;
}
if (!isScared)
{
moveDirection.y = jumpSpeed;
AkSoundEngine.PostEvent("Play_Jump", gameObject);
StartCoroutine(DelayJumpSound());
}
if(jumpPowerUp.jumpTimer == 0)
{
jumpSpeed = 10;
gravity = 25;
}
if (isScared)
{
if (jumpGo == false)
{
StartCoroutine(delayJump());
}
}
}
if(jumpLanded == true)
{
AkSoundEngine.PostEvent("Play_JumpLanding", gameObject);
jumpLanded = false;
}
if(Input.GetKey(KeyCode.W) || (Input.GetAxisRaw("VerticalJoy") == 1))
{
forwardSpeed += 4 * Time.deltaTime;
}
if (Input.GetKey(KeyCode.S) || (Input.GetAxisRaw("VerticalJoy") == -1))
{
forwardSpeed -= 4 * Time.deltaTime;
}
}
if(forwardSpeed > 0f)
{
forwardSpeed -=2f * Time.deltaTime;
if (forwardSpeed < 0f)
{
forwardSpeed = 0f;
}
}
if (forwardSpeed < 0f)
{
forwardSpeed += 2f * Time.deltaTime;
if (forwardSpeed > 0f)
{
forwardSpeed = 0f;
}
}
if (Input.GetKey(KeyCode.X) || (Input.GetKeyDown(KeyCode.Joystick1Button2)))
{
if (canBoost.canCharge == true)
{
charging = true;
}
}
if (charging == false)
{
moveDirection.z = speedForward + forwardSpeed;
if (rearSpeed.lookingBehind == true)
{
moveDirection.z = 10f;
}
}
if (charging == true)
{
canBoost.cutOff += .05f;
moveDirection.z = speedForward + boostedSpeed;
if(canBoost.cutOff >= 2f)
{
charging = false;
}
}
moveDirection.y -= gravity * Time.deltaTime;
moveDirection.x = Input.GetAxis("Horizontal") * strafe;
controller.Move(moveDirection * Time.deltaTime);
announcedSpeed = moveDirection.z;
//Debug.Log(announcedSpeed);
}
IEnumerator delayedDamage()
{
canTakeDamage = false;
yield return new WaitForSeconds(3);
canTakeDamage = true;
}
// Update is called once per frame
void FixedUpdate ()
{
}
public float pushPower = 100.0F;
/*void OnControllerColliderHit(ControllerColliderHit hit) {
Rigidbody body = hit.collider.attachedRigidbody;
if (body == null || body.isKinematic)
return;
Vector3 pushDir = new Vector3(hit.moveDirection.x, 0, hit.moveDirection.z);
body.velocity = pushDir * pushPower;
}*/
void OnControllerColliderHit(ControllerColliderHit other)
{
if (other.collider.tag == "Obstacle")
{
Obstacle curActive = other.collider.GetComponent<Obstacle>();
if(canTakeDamage == true)
{
if (curActive.isActive == true)
{
GameController.failHits += 1;
StartCoroutine(delayedDamage());
}
}
Rigidbody body = other.collider.attachedRigidbody;
if (body == null || body.isKinematic)
{return;}
Vector3 pushDir = new Vector3(other.moveDirection.x, 0, other.moveDirection.z);
body.velocity = pushDir * pushPower;
Debug.Log (body.velocity);
curActive.isActive = false;
}
}
IEnumerator delayJump()
{
jumpGo = true;
yield return new WaitForSeconds(1.2f);
moveDirection.y = jumpSpeed;
controller.Move(moveDirection * Time.deltaTime);
AkSoundEngine.PostEvent("Play_Jump", gameObject);
jumpGo = false;
}
IEnumerator DelayJump()
{
yield return new WaitForSeconds(.2f);
}
IEnumerator DelayJumpSound()
{
yield return new WaitForSeconds(.2f);
jumpCheck.SetActive(true);
}
IEnumerator JumpBoost()
{
jumpBoost = false;
yield return new WaitForSeconds(5f);
jumpBoost = true;
}
}