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 duckduckmoose · Dec 07, 2013 at 05:57 AM · androidguibuttonfps

[Android] Why does my enemy die no matter where I am in the 3D environment?

Hello peeps, Im kind of new to Unity3D, but a few years ago I was making an FPS for a college project and now I have returned to make an FPS game for Android.

Anyways, I was following a tutorial by Brackeys on Youtube and I wanted to make a separate button for the Melee combat. I created the enemy and then a button and applied the script to the button, the button works fine to play the combat animation, but wherever I move in the map, and looking no where near the enemy, it still manages to kill it?

Im very new to Java script, but getting the hang. Thanks :)

Heres the button script

 pragma strict
 
 var TheWeapon : Transform;
 var TheDamage : int = 50;
 var Distance : float;
 var MaxDistance : float = 1.5;
 
 function Update () 
 {        
         if(Input.touchCount > 0 )
         {
  
             for(var i : int = 0; i < Input.touchCount; i++)
             {
  
                 var touch : Touch = Input.GetTouch(i);
  
                 if(touch.phase == TouchPhase.Began &&
                 guiTexture.HitTest(touch.position))
                 {
                     //Attack animation
                     
                     TheWeapon.animation.Play("Attack");
                     //Attack Function
                     
                     var hit : RaycastHit;
                     if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit))
                         {
                             Distance = hit.distance;
                             if (Distance < MaxDistance)
                                 {
                                     hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);
                                 }
                         }
                 }
                 if (TheWeapon.animation.isPlaying == false)
                     {
                         TheWeapon.animation.CrossFade("Idle");
                     }
  
             }
         }
 }
Comment
Add comment · Show 14
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 robertbu · Dec 07, 2013 at 06:02 AM 0
Share

I don't see anything here. Just to make sure this code is causing the problem, I assume this line is being executed?:

 TheWeapon.animation.Play("Attack");

Also note that the values for TheDamage and $$anonymous$$axDistance will be the ones you see in the Inspector, not the ones in this code (assu$$anonymous$$g you've made in changes to these values).

avatar image duckduckmoose · Dec 09, 2013 at 12:39 AM 0
Share

Thanks for replying so fast! :), Aye the animation line is being executed with no problems, and yeah you can see The Damage and $$anonymous$$axDistance in the inspector, its just confusing why im not even looking at the enemy and it still manages to die :/

avatar image meat5000 ♦ · Dec 09, 2013 at 12:50 AM 0
Share

Is this necessary?

transform.TransformDirection

Also, I don't use sendmessage so I'm not familiar, but isn't it a broadcast?

avatar image duckduckmoose · Dec 09, 2013 at 12:59 AM 0
Share

Well I took out transform.TransformDirection and it still does the same thing, I also just noticed that the distance is not changing value when I move, it stays at 5.85 even though the max distance is a higher value

avatar image meat5000 ♦ · Dec 09, 2013 at 01:00 AM 0
Share

Is distance staying the same with TransformDirection in or out?

Try this

 if (Physics.Raycast (transform.position, Vector3.forward, hit, $$anonymous$$axDistance))
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by duckduckmoose · Dec 09, 2013 at 06:35 AM

Ah wait, I've done it guys :P, turns out I needed a GUI script with Rect LOL, got the new updated one here, basically it shows my texture where my button is on the script and when I hit it, the ray shoots where im looking, took a while but I got there.

Appreciate the help, thanks!

 var ButtonTexture : Texture ;
 var TheWeapon : Transform;
 var TheDamage = 100;
 
 
 function Update()
 {
     var hit : RaycastHit;
     var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
     
     //Touches + Co-ordinates Of button placement via (Rect)
     for(var i: int = 0;i < Input.touchCount; i++)
     {
         var touch : Touch = Input.GetTouch(i);
         if(Rect(0, 135, 100, 55).Contains(touch.position) && touch.phase == TouchPhase.Began)
             {
                    print("Does it work yet?");
                   //My Animation
                    TheWeapon.animation.Play("Attack");
                    if (Physics.Raycast (ray, hit, 100))
                     {
                         hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);
                     }
             }
     }    
     if (TheWeapon.animation.isPlaying == false)
                     {
                         TheWeapon.animation.CrossFade("Idle");
                     }
 }
  //GUI texture with Co-ordinates
 function OnGUI(){GUI.DrawTexture(Rect(0,135,100,55),ButtonTexture );}
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

18 People are following this question.

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

Related Questions

A few android questions. 0 Answers

Embed GUI Skin in GUI Button 2 Answers

Gui Button Turn Off Object on Click 0 Answers

Animator.Update High CPU Usage on Unity 5 resulting in bugs on Android 0 Answers

Button not update()ing,Button not updating 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