I having a real head scratcher here? I cannot understand why the two nested if statements are not working the same way.
`if (objectHit.getVikingState() != "block")` works flawlessly, all the time, there is no problems there.
But `if (objectHit.getVikingState() == "block")` does only work sometimes (and only after having moved the hitter around abit)
void OnTriggerEnter(Collider other)
{
//The location of the Particle effect
Vector3 pos = other.transform.position;
pos.y = pos.y + 2;
DamageManagement objectHit = DamageManagement)other.GetComponent("DamageManagement");
if (objectHit && weaponState == "slash") //Object hit has DamageManagement script attached, Weapon is slashing.
{
Debug.Log("Detecting the hit");
if (objectHit.getVikingState() == "block")
{
Debug.Log("This ought to Work");
setWeaponState("none"); //Makes sure that only one hit per animation goes in.
}
if (objectHit.getVikingState() != "block")
{
Instantiate(SmallHitEffect, pos, Quaternion.identity); //Instantiating the Particle hiteffect.
Debug.Log(getWeaponName() + (" Just hit: ") + objectHit.getVikingState());
objectHit.calcHealth(returnDamage()); //Calculates damage
setWeaponState("none"); //Makes sure that only one hit per animation goes in.
}
}
}
Here is the Movementscript.
using UnityEngine;
using System.Collections;
public class MovementScript : MonoBehaviour
{ public float moveSpeed = 0.06f;
public string axis1;
public string axis2;
public string softHit;
public string jumpHit;
public string block;
private Rigidbody vikingControler; //This rigidbody is found in start().
public WeaponScript myWeapon; // This Weaponscript is found in start().
public DamageManagement myDamage; // This DamageManagementScript is found in start().
Vector3 upAxis = new Vector3(); //Here I have to figure out what is gooing on.
Vector3 moveDirection = new Vector3();
public GameObject directionTarget; //This is the target that the viking will be looking at.
// Use this for initialization
void Start()
{
upAxis = Vector3.up;
animation["hit"].layer = 1;
animation["jumphit"].layer = 1;
animation["block"].layer = 1;
vikingControler = (Rigidbody)this.GetComponent("Rigidbody");
myWeapon = (WeaponScript)this.GetComponentInChildren<WeaponScript>();
myDamage = (DamageManagement)this.GetComponentInChildren<DamageManagement>();
}
// Update is called once per frame
void Update()
{
if (myWeapon.getWeaponState() == "none")
{
// Rotates to look at the target
transform.LookAt(directionTarget.transform, upAxis);
//Move The Viking in different Directions
moveDirection = new Vector3(moveSpeed * Input.GetAxis(axis1), 0, moveSpeed * Input.GetAxis(axis2));
moveDirection = vikingControler.transform.InverseTransformDirection(moveDirection * -1);
vikingControler.transform.Translate(moveDirection);
}
// Normal hit
if (Input.GetKeyDown(softHit))
{
myWeapon.setDamage(1);
setState("slash");
animation["hit"].speed = 3;
animation.Play("hit");
}
// Jump hit
if (Input.GetKeyDown(jumpHit))
{
myWeapon.setDamage(2);
setState("slash");
animation["jumphit"].speed = 2;
animation.Play("jumphit");
}
// Block
if (Input.GetKeyDown(block))
{
myWeapon.setDamage(2);
setState("block");
animation["block"].speed = 3;
animation.Play("block");
}
// Block
if (Input.GetKeyUp(block))
{
myWeapon.setDamage(2);
setState("none");
animation.Stop("block");
}
//Makes sure that player can only damage while his hit animations are playing!
if (!(animation.IsPlaying("hit") || animation.IsPlaying("jumphit") || animation.IsPlaying("block")))
setState("none");
//Animations
if (Input.GetButton(axis1) || Input.GetButton(axis2))
{
animation.CrossFade("walk");
directionTarget.transform.position = new Vector3(rigidbody.position.x - (1 * Input.GetAxis(axis1)), rigidbody.position.y, rigidbody.position.z - (1 * Input.GetAxis(axis2)));
}
if (Input.GetButtonUp(axis1) || Input.GetButtonUp(axis2))
{
animation.CrossFade("idle");
directionTarget.transform.position = new Vector3(rigidbody.position.x - (1 * Input.GetAxis(axis1)), rigidbody.position.y, rigidbody.position.z - (1 * Input.GetAxis(axis2)));
}
if (!animation.isPlaying)
{
setState("none");
animation.Play("idle");
}
}
//This function makes sure that the Viking/WeaponState is the same in all scripts.
public void setState(string y)
{
myWeapon.setWeaponState(y);
myDamage.setVikingState(y);
}
}