- Home /
OnTriggerEnter behaving oddly?!?
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); }
}
It is not entirely clear, what exactly your symptoms are "... works flawlessly" means that case is entered if (and only if!) .getVikingState() really is !=block? If that's the case, "This ought to Work" must be printed in all other cases. Try improving your debug messages, e.g. Debug.Log("Detecting the hit, viking state is "+objectHit.getVikingState());. We know too little about .getVikingState() to really help you, there might be problems with case sensitivity, etc.
Works flawlessly, means that it detects the hit, every time it happens, and gives me the Debug information, and spawns the SmallHitEffect.
The VikingState comes from the $$anonymous$$ovementScript that is attached to the GameObject that is hit. It is a rather simple string, that is set when key er pressed, or animations are played. I have made a teporary GUI that shows the VikingStateString.
What really puzzles me, is that while the viking is Blocking( ObjectHit.VikingState() == "block") OnTriggerEnter is not detected at all!
I will try to add the $$anonymous$$ovementScript as well.
Again.. what really puzzles me is that it "sometimes" work, If I move a bit around and then hit.
I have exported my testlevel, all nessecary files should be there: http://nuf.dk/download/TotalVikingPower.unitypackage
I added some more debug lines in the weaponscript.
Thank you very much for the help, it is very much appresiated :)
And sorry for the bad code ;) I am an artist by trade
I reported this as a bug. However, I found a workaround, it puzzles me why it works, but it does? $$anonymous$$oving the Damage$$anonymous$$anagment gameobject onto the animated bones solves this issue?
Answer by qJake · Aug 06, 2010 at 09:13 PM
You're probably re-setting your variable (you're doing something like this):
if(something is false) { something = true }
if(something is true) { do something else }
You just need to add an "else" to this line:
else if (objectHit.getVikingState() != "block")
Your answer
Follow this Question
Related Questions
How to figure out the contact point of OnTriggerEnter? 1 Answer
Simple script... 1 Answer
Teleporting projectiles, and maintaining speed 3 Answers
Is this a problem with OnTriggerEnter? 0 Answers
Player Colision help 0 Answers