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 ChrisSch · Aug 27, 2013 at 02:45 PM · cameraraycasttagshootturret

Turret With Raycast Camera

So I made a turret that has a sphere as a trigger and shoots at the player when he enters it, which works fine, except it also shoots at the player when hes behind walls, and I don't want that.

I thought about making a cone shaped trigger in front of the turret kind of like a view area but the turret would still shoot at the player behind a wall if the trigger was going through it, and I don't want to have to individually resize the trigger for each scenario.

And my final conclusion was to make a child camera of the turret that would raycast and shoot only if it hits an object with a tag "Player".

So I have a couple questions about that.

  1. Is it a good way, or is it resource expensive?

  2. Is there a better way?

  3. How would I do it? Something like this like in the unity docs?

    function Update () { var hit : RaycastHit; if (Physics.Raycast (transform.position, -Vector3(0,0,1), hit, 100.0)) { var distanceToGround = hit.distance; } }

And then how would I make it react only to an object with tag and execute a function in its parent?

Raycast is really confusing to me even tho I can visualize it I don't understand how the code works, none of the tuts on raycasting helped me, and I could never understand Unity docs. x'D

PS: Sorry for the code not being in code quotes its not working now for some reason, it does usually.

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 robertbu · Aug 27, 2013 at 04:07 PM 0
Share

For an accurate answer, please post your current Turret ai$$anonymous$$g code.

avatar image ChrisSch · Aug 27, 2013 at 05:43 PM 0
Share

Sorry, here. xD Its a code I used in a tower defense game and while it worked there its not good enough for this game.

 #pragma strict
 
 class Turret_Cannon extends Turret_Base
 {
 
 var myProjectile : GameObject;
 var reloadTime : float = 1f;
 var turnSpeed : float = 5f;
 var firePauseTime : float = .25f;
 var muzzleEffect : GameObject;
 var myExplossion : GameObject;
 var errorAmount : float = .001;
 var myTarget : Transform;
 var muzzlePositions : Transform[];
 var turretBall : Transform;
 
 private var nextFireTime : float;
 private var next$$anonymous$$oveTime : float;
 private var desiredRotation : Quaternion;
 private var aimError : float;
 
 function Start ()
 {
 }
 
 function Update ()
 {
 if(myTarget)
 {
 if(Time.time >= next$$anonymous$$oveTime)
 {
 CalculateAimPosition(myTarget.position);
 turretBall.rotation = Quaternion.Lerp(turretBall.rotation, desiredRotation, Time.deltaTime*turnSpeed);
 
 }
 
 if(Time.time >= nextFireTime)
 {
 FireProjectile();
 
 }
 }
 }
 
 /*function OnTriggerEnter(other : Collider)
 {
 if(other.gameObject.tag == "Enemy")
 {
 nextFireTime = Time.time+(reloadTime*.5);
 myTarget = other.gameObject.transform;
 }
 }
 */
 
 function OnTriggerStay(other : Collider)
 {
     if(!myTarget)//if I don't already have a target
     {
         if(other.gameObject.tag == "Player" || other.gameObject.tag == "ShipLoaded")
         {
         nextFireTime = Time.time+(reloadTime*.5);
         myTarget = other.gameObject.transform;
         }
     }
 }
 
 function OnTriggerExit(other : Collider)
 {
 if(other.gameObject.transform == myTarget)
 {
 myTarget = null;
 }
 }
 
 function CalculateAimPosition(targetPos : Vector3)
 {
 var aimPoint = Vector3(targetPos.x+aimError, targetPos.y+aimError, targetPos.z+aimError);
 desiredRotation = Quaternion.LookRotation(aimPoint - turretBall.position);
 
 }
 
 function CalculateAimError()
 {
 aimError= Random.Range(-errorAmount, errorAmount);
 }
 function FireProjectile()
 {
         audio.Play();
         nextFireTime = Time.time+reloadTime;
         next$$anonymous$$oveTime = Time.time+firePauseTime;
         CalculateAimError();
        
         for(the$$anonymous$$uzzlePos in muzzlePositions)
         {
                 Instantiate(myProjectile, the$$anonymous$$uzzlePos.position, the$$anonymous$$uzzlePos.rotation);
                 Instantiate(muzzleEffect, the$$anonymous$$uzzlePos.position, the$$anonymous$$uzzlePos.rotation);
         }
 }
 
 }
 
 function OnCollisionEnter(coll : Collision)
 {
     if (coll.gameObject.tag == "Bullet")
     {
     Instantiate(myExplossion, transform.position, Quaternion.identity);
     Destroy(gameObject);
     }
 }

1 Reply

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

Answer by robertbu · Aug 27, 2013 at 06:17 PM

Having examined you code, I'm not sure what functionality you want. Do you want it to not track the player if the player cannot be "seen" or do you want it so not fire if the player cannot be seen? In either case, I'd recommend a Physics.LineCast():

 function CanSeePlayer() : boolean {
     var hit : RaycastHit;
     
     if (Physics.Linecast(transform.position, myTarget.transform.position, hit)) {
         if (hit.collider.tag == "Player")
             return true;
         // An alternate check that does not depend on the tag
         if (hit.transform == myTarget)
             return true;
     }
     
     return false;
 }


Note this only check to make sure the pivot point of the turret can see the pivot point of the player. If the player is only partly visible, this may return false. A more complete check would linecast against the corners of the either the renderer.bounds or the mesh.bounds (translated into world space).

Comment
Add comment · Show 7 · 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 ChrisSch · Aug 27, 2013 at 08:55 PM 0
Share

Yes I want it to not track or shoot at the player when he can not be "seen". Tracking is fine but shooting not.

Ok so do I attach that to the turret or the camera child of the turret? I don't understand how will the turret "see" without the camera. :S

avatar image robertbu · Aug 28, 2013 at 03:04 AM 1
Share

Start by dropping the CanSeePlayer() method into the Turret_Cannon class above. Then insert as the first line in FireProjectile() method:

 if (!CanSeePlayer()) return;

This may not be how you use it long term. Note Raycasting has nothing to do with cameras. You can construct a ray from a mouse position using a camera, but you can also just Raycast from some position in world space, or between two points using Linecast as I've done here.

avatar image ChrisSch · Aug 28, 2013 at 08:34 AM 0
Share

Wow thank you it works great! :D I had no idea you can raycast without cameras. I thought raycasting is throwing rays from the camera to everything the camera sees. xD

You're the first one that made me understand raycasting code a bit better.

I don't get one thing tho, if you got a $$anonymous$$ to reply one more time. xD

The line

 if (!CanSeePlayer()) return;

It means, if CanSeePlayer is not true right? Cause of the !. Why does it execute the fire code when it sees the player? Or does it mean, if CanSeePlayer not true return and do nothing, else execute the code in brackets?

avatar image robertbu · Aug 28, 2013 at 08:51 AM 1
Share

CanSeePlayer() return true if the raycast hits the player. The '!' inverts the logic. So it means if you cannot see the the player, then return (i.e. don't fire). That prevents the turret from firing if it cannot see the player.

avatar image robertbu · Aug 28, 2013 at 04:55 PM 1
Share

Actually from a "good program$$anonymous$$g" point of view, your way is better. I picked the line I suggested because it was simplest to describe and easiest to get right.

Show more comments

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

15 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

Related Questions

Launch a projectile from one object to another 1 Answer

How to get a rayhit to detect tags? 1 Answer

Start raycast from object 1 Answer

Changing a Variable within the same script 0 Answers

get the point where the raycast hits the world groud? 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