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 Xatoku · Jan 29, 2012 at 10:59 PM · javascriptplayerangletargetfind

Else Statement Not Working

I'm checking to see if an object is in front of another before continuing, and this works. But when I try to do an alternate action for when the opposite happens (they're behind), it will always do the latter. Any help out there? I can not figure out why this doesn't work and it's driving me insane.

What should happen is that if we're not blocking, get hit by the ball. If we are blocking and facing the ball, do the action, if we are blocking and not facing the ball, get hit. But instead, if we're blocking, it always hits and never catches - regardless of if they're in front of us or not.

 function OnTriggerEnter(hit : Collider){
     if(hit.gameObject.tag == "Ball"){
         var toTarget = (hit.transform.position - transform.position).normalized;
         
         if(!isBlocking){
             GetHit();
         }else if(Vector3.Dot(toTarget, transform.forward) < 0){
             CatchBall();
         }else{
             GetHit();
         }
     }
 }
Comment
Add comment · Show 9
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 Jessy · Jan 30, 2012 at 03:31 PM 0
Share

This is not enough info, because we don't know how far away hit could be, and you don't normalize heading. You also don't explain what Random.value is for. Also, use CompareTag ins$$anonymous$$d: http://answers.unity3d.com/questions/40975/why-does-comparetag-exist.html

avatar image Xatoku · Jan 30, 2012 at 04:42 PM 0
Share

Okay, I changed it up a bit and I'm still having the same problems. Change is in the initial question.

avatar image Jessy · Jan 30, 2012 at 04:54 PM 0
Share

Change and tell us if that fixed it.

avatar image Xatoku · Jan 30, 2012 at 05:08 PM 0
Share

Okay. Now it works, but it's backwards. I catch the ball when I turn around, but get hit when I'm facing it. It should be the other way around, but turning it back to < will always make me get hit.

avatar image Jessy · Jan 30, 2012 at 05:30 PM 0
Share

Transform.forward is the local Z vector; it may not be "forward" at all. We don't know how your mesh is oriented, or where the local origins are. With the greater than symbol, I'd say your code looks usable, but there's no way to write this code for every case. It's dependent on alignment.

Show more comments

1 Reply

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

Answer by Xatoku · Jan 31, 2012 at 07:07 PM

I figured it out. What happened was that the ball was being caught properly the entire time, it just hit the player afterward because the else statement did not check if the ball had been caught or not. Everything is fixed now, thanks for the help.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

make player that enters collider tagert 1 Answer

Find Transform in the scene 2 Answers

Lock player position till loading is finished 2 Answers

I use this script, but the enemy lose health if i don´t target him. 1 Answer

Help with bounce angle and velocity change 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