- Home /
onTriggerEnter only triggers when the gameobject its shall hit moves.
I have this basic setup, a mockup if you want, where two fighters are supposed to fight each other with axes. the characters each have a movement script, and their axe have a weaponscript. Everything should work, but there is this little detail, that the onTriggerEnter only works when the other fighter is moving? I have been looking all over my scripts to figure out whats going on, but really cant figure it out. The project is 7 megs, I can send it if anyone is interested.
using UnityEngine;
using System.Collections;
public class MovementScript : MonoBehaviour
{
public float moveSpeed = 0.06f;
public float rotateSpeed = 3.0f;
public string axis1;
public string axis2;
public string softHit;
public string jumpHit;
public string vikingName;
public int health = 100; //This is the health of the viking.
public Rigidbody vikingControler;
public WeaponScript myWeapon;
public float gravity = 200.0f;
Vector3 upAxis = new Vector3();
Vector3 moveDirection = new Vector3(0, 0, 0);
public Transform 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;
}
// Update is called once per frame
void Update()
{
// Rotates to look at the target
transform.LookAt(directionTarget, 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.setWeaponState("slash");
animation["hit"].speed = 1;
animation.Play("hit");
}
// Jump hit
if (Input.GetKeyDown(jumpHit))
{
myWeapon.setWeaponState("slash");
animation["jumphit"].speed = 1;
animation.Play("jumphit");
}
//Animations
if (Input.GetButton(axis1) || Input.GetButton(axis2))
{
animation.CrossFade("walk");
directionTarget.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.position = new Vector3(rigidbody.position.x - (1 * Input.GetAxis(axis1)), rigidbody.position.y, rigidbody.position.z - (1 * Input.GetAxis(axis2)));
}
if (!animation.isPlaying)
{
myWeapon.setWeaponState("none");
animation.Play("idle");
}
}
//This function returns the health of the Viking
public int returnHealth()
{
return health;
}
public int calcHealth(int x)
{
health -= x;
return health;
}
public string returnVikingState()
{
return myWeapon.getWeaponState();
}
public string getVikingName()
{
return vikingName;
}
}
And the weapon script
using UnityEngine;
using System.Collections;
public class WeaponScript : MonoBehaviour {
public string weaponName;
public int damage;
public string weaponState = "noDamage";
public GameObject SmallHitEffect;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public int returnDamage()
{
return damage;
}
public string getWeaponName()
{
return weaponName;
}
public string setWeaponState(string y)
{
weaponState = y;
return weaponState;
}
public string getWeaponState()
{
return weaponState;
}
/*
* An important note to make here, is that when this onTriggerEnter is done
* in MovementScript, it cannot find the correct weapon, since the call
* aparrently goes to the trigger and not the collider with the rigidbody.. or
* something like that.. anyhow, it works from here, but not from there
*/
void OnTriggerEnter(Collider other)
{
Vector3 pos = other.transform.position;
pos.y = pos.y + 2;
MovementScript objectHit = (MovementScript)other.GetComponent("MovementScript");
if (objectHit && getWeaponState() == "slash")
{
Instantiate(SmallHitEffect, pos, Quaternion.identity);
// Debug.Log(getWeaponName() + (" Just hit: ") + objectHit.getVikingName());
objectHit.calcHealth(returnDamage());
setWeaponState("none");
}
}
}
Answer by Daniel-Brauer · Jul 21, 2010 at 03:57 AM
Do you have a Rigidbody on either the trigger or the Collider you are detecting? If not, try adding one and setting isKinematic to true. The object's motion won't change, but the Rigidbody will ensure that the physics engine tracks it every frame, and OnTriggerEnter() gets called at the right time.
I am gonna try that... that seems to reflect pretty much what the problem is all about, I have a rigidbodu attached to the collider, but it is not $$anonymous$$inematik, I will try that later today and repport back.. thanks :)
This kinda sovles the problem, however, setting it to is$$anonymous$$inematic will remove the the other abilities for the rigidbody that I use.. but definatley it is part of the sollution, I will keep working on this.
The wierd thing is, that when I only had one hittkey it worked like a charm, but with the addition of the jumpHit it stopped working.
Your answer
![](https://koobas.hobune.stream/wayback/20220613065259im_/https://answers.unity.com/themes/thub/images/avi.jpg)