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 · Aug 06, 2010 at 08:24 PM · ontriggerenter

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

}

Comment
Add comment · Show 4
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 Wolfram · Aug 06, 2010 at 11:46 PM 0
Share

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.

avatar image Tabu · Aug 08, 2010 at 09:57 AM 0
Share

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.

avatar image Tabu · Aug 08, 2010 at 10:13 AM 0
Share

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

avatar image Tabu · Aug 24, 2010 at 09:06 AM 0
Share

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?

1 Reply

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

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")
Comment
Add comment · 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

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

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


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