• 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 chrisPOD · Mar 09, 2011 at 03:07 AM · javascriptraycastraycastingturret

Turret that shots a raycast to detect my character then shots at him.

Hello everyone im New to UnityAnswers and im glad i found this place because i need some help with a few Javascripts. I'm pretty new to Scripting so i will explain the best i can.

WHAT I WANT TO HAPPEN: I created a Turret that i want to use a raycast to look for my Character. When it spots my Character i want the Turret to shot me EVEN IF i walk around it. I also want the Turret to stop shooting at me when i leave its raycast or walk behind a wall.

WHAT IS HAPPENING: When i enter the Turrets raycast it does see me, and it looks at me. But it doesnt shot at me and it still detects me through walls.

Heres my script im trying to use PLEASE HELP

var area:int =100; var target: Transform; var damp = 6.0; var bullitPrefab : Transform; var savedTime=0; var AttackSpeed=1000; function Update (){ var layermask= 1 << 8;

var left = (transform.TransformDirection (-Vector3.right)); var right = (transform.TransformDirection (Vector3.right)); var up = (transform.TransformDirection (Vector3.forward)); var down = (transform.TransformDirection (-Vector3.forward));

if (Physics.Raycast(transform.position,left,area,layermask)){ transform.LookAt( target ); Debug.DrawLine(transform.position,target.position, Color.red); return; }

if (Physics.Raycast(transform.position,right,area,layermask)){ transform.LookAt( target ); Debug.DrawLine(transform.position,target.position, Color.green); return; }

if (Physics.Raycast(transform.position,up,area,layermask)){ transform.LookAt( target ); Debug.DrawLine(transform.position,target.position, Color.blue); return; }

if (Physics.Raycast(transform.position,down,area,layermask)){ transform.LookAt( target ); Debug.DrawLine(transform.position,target.position, Color.yellow); return; } }

if(target) {

var seconds : int = Time.time; var oddeven = (seconds % 2);

if(oddeven) { shoot(seconds); }

}

function shoot(seconds) { if(seconds!=savedTime) {

var bullit = Instantiate(bullitPrefab ,transform.Find("spawnPointA").transform.position , Quaternion.identity);

bullit.gameObject.tag = "enemyProjectile"; bullit.rigidbody.AddForce(transform.forward * AttackSpeed);

savedTime=seconds;

}

}

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 AVividLight · Mar 09, 2011 at 04:50 AM

You don't have a function, to start with... And, just a personal thing, its a little unorganized... But thats not important. If I were you I would add "function Update () {" to the top, and see what it does.


-Hope I helped

-Gibson

Comment
Add comment · Show 4 · 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 chrisPOD · Mar 09, 2011 at 12:23 PM 0
Share

I do have a Function Update look at the top. it says var area:int =100; var target: Transform; var damp = 6.0; var bullitPrefab : Transform; var savedTime=0; var AttackSpeed=1000; function Update (){ var layermask= 1 << 8;

Its just that the script copied over really weird and some the script is gray and not in the little script box thing.

avatar image AVividLight · Mar 09, 2011 at 04:34 PM 0
Share

Oh, okay... Sorry... I'll see what else I can do.

avatar image AVividLight · Mar 09, 2011 at 04:35 PM 0
Share

You can highlight your code and then click the code button (again) and it will put it all in the correct places

avatar image chrisPOD · Mar 09, 2011 at 08:17 PM 0
Share

Oh okay thank you

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

ray cast not working ? 2 Answers

Raycast help..... 2 Answers

Raycasting problems 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 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