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 jacques 1 · Apr 27, 2011 at 12:11 AM · raycasthitdamagefiring

making a turet shoot

so i downloaded the fps add on put a turret down and it wouldnt fire at me it only looked at me and the muzzle flash was still there so since im not the best scripter around i thought id use the machine gun script to edit it and made this:

var attackRange = 30.0; var shootAngleDistance = 10.0; var target : Transform; var damage = 5.0; var fireRate = 0.05; var force = 10.0; private var hitParticles : ParticleEmitter;

function Start () { if (target == null && GameObject.FindWithTag("Player")) target = GameObject.FindWithTag("Player").transform; hitParticles = GetComponentInChildren(ParticleEmitter);

// We don't want to emit particles all the time, only when we hit something.
if (hitParticles)
    hitParticles.emit = false;

}

function Update () { if (target == null) return;

if (!CanSeeTarget ()) return;

// Rotate towards target
var targetPoint = target.position; var targetRotation = Quaternion.LookRotation (targetPoint - transform.position, Vector3.up); transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0);

// If we are almost rotated towards target - fire one clip of ammo var forward = transform.TransformDirection(Vector3.forward); var targetDir = target.position - transform.position; if (Vector3.Angle(forward, targetDir) < shootAngleDistance) SendMessage("Fire");

}

function CanSeeTarget () : boolean { if (Vector3.Distance(transform.position, target.position) > attackRange) return false;

var hit : RaycastHit; if (Physics.Linecast (transform.position, target.position, hit)) return hit.transform == target;

return false;

}

function FireOneShot () { var direction = transform.TransformDirection(Vector3.forward); var hit : RaycastHit ;

// Did we hit anything? if (Physics.RaycastHit (transform.position, direction, hit, range)) { // Apply a force to the rigidbody we hit if (hit.rigidbody) hit.rigidbody.AddForceAtPosition(force * direction, hit.point);

 // Place the particle system for spawing out of place where we hit the surface!
 // And spawn a couple of particles
 if (hitParticles) {
     hitParticles.transform.position = hit.point;
     hitParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
     hitParticles.Emit();
 }
 }
 }

but it said 'RayCastHit' is not a member of 'UnityEngine.Physics' would that script work fine if the RayCastHit was changed if not how could i edit it to make it fire at me snd do damage? thanks!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Richard J. Hansen · Apr 27, 2011 at 01:15 AM

This gave me trouble the first time I dealt with Raycasts as well.

If you scroll down here: http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html?from=RaycastHit you will see an example of the proper way to use RaycastHit.

You basically create a RaycastHit (you would call it hit, in your code). This does not perform the raycast, but creates a container to store information about what the raycast hits. If you use Physics.Raycast (transform.position, -Vector3.up, hit) it will store the information in the RaycastHit specified by hit.

There might be other things wrong with your code but that is the thing that is immediately obvious to me.

Comment
Add comment · Show 1 · 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 Richard J. Hansen · Apr 27, 2011 at 05:24 AM 0
Share

Looking at your code some more... Not only should you change Physics.RaycastHit to Physics.Raycast, but you do not seem to define range (at the top of your script you call it attackRange. I'm also not sure you can call direction like that (I always call that type of information as part of the transform).

One thing that may help you once you are no longer getting error messages is to throw in a debug draw line in there.

avatar image
0

Answer by Meater6 · Apr 27, 2011 at 01:21 AM

Best way to see if it works is to test it. So to solve the problem you say, I would just change the Physics.RaycastHit to Physics.Raycast, for it seems like that is the problem. That should work, but as I said earlier,you should test it. Of course, I see no function or line of code that deals damage to a target. For that, you need to subtract the target's health (Which is defined in a player status script) when hit. Try looking at this question: Question

Good luck, 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 jacques 1 · Apr 27, 2011 at 02:03 AM 0
Share

i also tried taking off the hit part but it just said it was unidentified

avatar image jacques 1 · Apr 27, 2011 at 02:05 AM 0
Share

takin oof hit just turns it unidentified, and i dont want to make a seperate damage script for sentry damage because if anything else hits me it wont add them to gether like it should

avatar image Meater6 · Apr 27, 2011 at 03:04 AM 0
Share

You don't need a second script on the sentry, you have some of it in the script you just showed me, and the other half in the character. I can't really see the entire script due to your formatting problem, and I can't edit it to due to not enough rep. But I'm sure that raycasthit isn't supposed to be there, because RaycastHit is not a part of Physics.

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

No one has followed this question yet.

Related Questions

I'm trying to get a Raycast Laser weapon to tell whatever it hits to do damage, and its not working. im kindof a noob... 0 Answers

Target a single enemy 1 Answer

How could I apply damage to the player when the player is hit with a raycast? 0 Answers

AIs with guns and sentrys that fire 1 Answer

Instantiate a projectile on key press "F" key / OnMouseDown! Help!!! 2 Answers


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