Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Tabu · Jul 20, 2010 at 07:42 PM · movementrigidbodyontriggerenter

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");
     }
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
5
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tabu · Jul 21, 2010 at 11:54 AM 0
Share

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 :)

avatar image Tabu · Jul 21, 2010 at 07:20 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Unwanted 2d pixel after-image when moving 1 Answer

OnTriggerEnter requiring both to be rigidbodies ? 1 Answer

Making character dash forward 0 Answers

Movement continues after respawn 1 Answer

2D Game, Character Slides off Slopes, Need to be Able to Walk on Them 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges