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 7Why · Jul 19, 2013 at 03:29 AM · raycastdestroy

GameObjects that destroy themselves when there raycast hits the player

i am trying to build a boss ai. i need it to strafe side to side (which works fine) and move towards the player when he is in range of its raycast but instead it destroys itself as soon as the ray cast hits the player it also instantiates othe enemies that find and move toward the player but they destroy themselves also it only happens on this level and i cant work out why. i am new to unity and i have searched around but i cant find any similar questions here is the boss blocks code

 public var moveSpeed : Number = 10.0;
 public var moveDirection : Vector3 = Vector3(0,0,1);    //    Initial movement direction
 public var errorComp : Number = 0.95;    //    This prevents a body of the same size from missing the object it is touching
 var life : int = 60;
 
 var target :Transform;
 
 var speed :int = 20;
 var hit : RaycastHit;
 var distance : float = 10;
 private var Direction : Vector3;
 private var ray : Ray;
 var mask : LayerMask;
 
 
 function Move()
 {
     var moveDistance : Number = moveSpeed * Time.deltaTime;    //    The distance to move this update
     var hitInfo : RaycastHit;    //    The raycast info variable
  
     //    Check if there is anything in the way
     if (Physics.Raycast(this.transform.position , moveDirection , hitInfo , moveDistance + this.collider.bounds.extents.z))
     {
         //    Reduce the movement distance so the AI doesn't run into the wall
         moveDistance = hitInfo.distance - this.collider.bounds.extents.z;
     }
         //    If there is nowhere to move, turn around
     if ( moveDistance < 0.001 )
     {
         //    turn around
         moveDirection.z = -moveDirection.z;
     }
     transform.position += moveDirection * moveDistance;
 }
 
 function Update () {
 Move();
 if (target != null)
         {
         Direction = target.transform.position - transform.position;
         }else{target = gameObject.FindWithTag("player").transform;}
 ray = Ray( this.transform.position , Direction);
 if (Physics.Raycast(transform.position, Direction, distance, mask))
 {
 Debug.DrawRay( ray.origin , ray.direction * distance , Color.green );
 transform.position = Vector3(Mathf.MoveTowards(transform.position.x, target.position.x,Time.deltaTime * speed),0,Mathf.MoveTowards(transform.position.z, target.position.z,Time.deltaTime * speed));     //    If it hits the player, move towards it
 }
 else
 {
 Debug.DrawRay( ray.origin , ray.direction * distance , Color.red );    
 transform.position = Vector3(583.6437,this.transform.position.y,this.transform.position.z);
 }
 }
 
 function damage()
 {
 life -= 1;
 if (life == 0) //only if my life points = 0 will i destroy myself
 {
 Destroy(gameObject);
 }
 }
 function OnCollisionEnter (other : Collision) 
 {
         if (other.gameObject.CompareTag("player")) //send message to the player telling it to destroy itself
         other.gameObject.SendMessage("ApplyDamage");
         Destroy(gameObject);// destroy yourself on impact
 }

i dont know what is going on

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
0

Answer by DanFrias · Jul 19, 2013 at 03:52 AM

The last line of the OnCollisionEnter function effectively destroys the boss when the raycast hits it, as you describe. What is the purpose of that line?

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 7Why · Jul 19, 2013 at 05:46 AM 0
Share

that part i noticed after posting i got rid of it however it still dissapears its they aren't destroyed on collision with the player they are moved off screen i have it set up as a layered level and when they detect the player they move to the upper level any idea how to fix sorry for the mistake

avatar image DanFrias · Jul 19, 2013 at 08:37 PM 0
Share

I'm unclear as to what you're saying. The boss moves to another level when it detects the player? What are these layers you are describing? Layers in Unity are used for rendering scenes with multiple cameras, so I'm not sure how that fits into this. Please try to better describe what it is you're seeing or what is occurring, and if the code has changed at all from that which you pasted above.

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

16 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

Related Questions

Multiple Cars not working 1 Answer

Remove object with specific object 1 Answer

Joystick Raycast Problem? 2 Answers

How do I detect if there's an object at the same position and destroy ONLY ONE of them? 1 Answer

how do i access an int var in a javascript and -= it from another javascript 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