Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 UdevMike · Jun 24, 2011 at 09:30 PM · collisionraycastprefabaiturret

Wall turret, need help making him a proper prefab that could be scattered around the map. Turret to player, raycast help too.

Hey fellas, I am working on a doom-esque fps, and I have a turret enemy that instanciates prefab bullets and each of those is being propelled via vector.forward.

The prefab turret also has two If Statements', one to control distance and another to only fire at the player when he is not behind a collider. Also the turret uses a Quaternian Slerp to slowly rotate towards the player while firing at the same time.

The prefab itself is structured as such:

 --wallBot=Empty GameObject
 ----botModel= Model of the turret
 ----botLight= Spotlight
 ----botCollider= To test collision with bullets

Here be code:

     using UnityEngine;
     using System.Collections;
 
     public class BotWall : MonoBehaviour {
 
     public Transform trget;
     public Transform meTransform;
     public int maxSeeDistance;
     public GameObject alarmLight;
     public float meTransY;
     public int rotationSpeed;
     public Transform botBullet;
     public GameObject botSpitfire;
     public Transform bulletModel;
     public int botBulletSpeed;
     public int botBulletInterval;
     public int wallBotHealth;
     
     private int bulletTime;
         
     void Awake () {
         meTransform = transform;
     }
     
 
     void Start () {
         GameObject go = GameObject.FindGameObjectWithTag("Player");
         trget = go.transform;
         meTransY = meTransform.rotation.y;
         GameObject.Find("wallBot_spot").light.intensity = 0;
     }
     
     void OnTriggerEnter (Collider coll)
     {
     if(coll.tag == "Bullets") {
         wallBotHealth --;
         if (wallBotHealth <= 0)
         {
             Destroy(gameObject);
         }
     }
     
 }
     
 
     void Update () {
         if ((Vector3.Distance(trget.position, meTransform.position)) < maxSeeDistance)
         {
             if (Physics.Linecast(meTransform.position, trget.position))
             {
                 //Debug.Log("Clear line of sight");
                 meTransform.rotation = Quaternion.Slerp(meTransform.rotation, Quaternion.LookRotation(trget.position - meTransform.position), rotationSpeed * Time.deltaTime);
                 GameObject.Find("wallBot_spot").light.intensity = 7;
                 Debug.DrawLine(trget.position, transform.position);
                 
                 bulletTime ++;
                 //bullet counter, to create a delay between each bullet
                 if (bulletTime > botBulletInterval)
                 {
                     GameObject.Find("wallBot_spot").light.intensity = 8;
                     Transform botBullet=(Transform)Instantiate(bulletModel, (transform.position - new Vector3(0.7f, 0f, 0f)), Quaternion.identity);
                     botBullet.rigidbody.AddForce(transform.forward * botBulletSpeed);
                     
                     //reset counter
                     bulletTime = 0;
                 }
             }
         }
 
         else
         {
             Debug.Log("Out of reach");
             meTransY = 180;
             GameObject.Find("wallBot_spot").light.intensity = 0;
         }    
     }
 }

I bet the keen eyed folks here can tell that since this is structed as such I cannot scatter this enemy around and have it work.

Basically I would like to know how to create this efficiently so I could scatter it around without having it be half broken(light goes on one bot but not another etc). And how to make the enemy wall bot not shoot at the player when he is behind a collider, since right now it doesnt seem to work...

Comment
Add comment · Show 2
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 Dreamblur · Jun 24, 2011 at 09:45 PM 0
Share

Save some bytes and make a few of those variables static.

How exactly is the linecast not working? Does it keep firing or does not it fire at all? Well, whichever it is, using a layermask will help you better achieve getting the intended behaviour.

avatar image UdevMike · Jun 24, 2011 at 10:09 PM 0
Share

Currently it just keeps firing even though I am behind a collider.

Layer mask hmm, I'll look into it, thanks.

1 Reply

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

Answer by Heratitan · Jun 24, 2011 at 11:43 PM

In the scripting reference it says that the linecast will return true if there is a collider between the two points, and in your script, it looks like the turret is shooting when the linecast returns true, or when there is a collider between you and the turret. Try: if (!Physics.Linecast(meTransform.position, trget.position)){

...and see if that works.

I hope this helps!

Comment
Add comment · Show 3 · 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 UdevMike · Jun 25, 2011 at 04:50 PM 0
Share

Thanks for the tip man! I gave it a try and it doesnt start the shooting routine no matter how close or how far I am, it appears as though its hitting the model or something, so I added the entire model to the Ignore Raycast layer. Still nothing.

Any other ideas?

avatar image Heratitan · Jun 25, 2011 at 08:01 PM 0
Share

Did you add both the turret and the player to the ignore ray cast layer? If not, could it be hitting the other one?

Edit: I tested out your turret script with a few cubes and, at first it didn't work, but when I removed the colliders from both the player cube and the turret cube, it worked. I also tested the scene with both the player and the turret having colliders and both being on the ignore ray cast layer, and that worked too.

So, it should shoot if you make sure both the player and the turret are on the Ignore Raycast Layer.

avatar image UdevMike · Jun 25, 2011 at 10:25 PM 0
Share

Fantastic! Works like a charm, thanks very much man!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Determining rotation for collision avoidance? 3 Answers

Destroying a prefab with a prefab? 2 Answers

AI - Raycast collision and follow player 1 Answer

RayCast Collision between 2 game objects (of the same prefab) 1 Answer

Collision detection with raycast 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