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 surgesus · Mar 08, 2013 at 05:50 PM · gunshootturret

Gun Turret will not Shoot.

So not sure where to start. My problem is the Turret will not shoot at targets, He will aim at the target and that is it. I need help making him shoot at the target. Here is my script:

 var myProjectile : GameObject;
 var reloadTime : float = 1f;
 var turnSpeed : float = 5f;
 var firePauseTime : float = .25f;
 var muzzleEffect : GameObject;
 var errorAmount : float = .001;
 var myTarget : Transform;
 var muzzlePositions : Transform[];
 var turretBall : Transform;
 
 private var nextFireTime : float;
 private var nextMoveTime : float;
 private var desiredRotation : Quaternion;
 private var aimError : float;
 
 
 function Start ()
 {
 
 
 }
 function Update ()
 {
     if(myTarget)
 {
     if(Time.time >= nextMoveTime)
 {
 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 OnTriggerExit(other : Collider)
 {
 if(other.gameObject.transform == myTarget)
 {
 myTarget = null;
 }
 }
 
 function CalculateAimPosition(targetPos : Vector3)
 {
 var aimPoint = Vector3(targetPos.x-transform.position.x+aimError, targetPos.y-transform.position.y+aimError, targetPos.z-transform.position.z+aimError);
 desiredRotation = Quaternion.LookRotation(aimPoint);
 }
 
 function CalculateAimError()
 {
 aimError= Random.Range(-errorAmount, errorAmount);
 }
 function FireProjectile(){
 
     audio.Play();
     nextFireTime = Time.time+reloadTime;
     nextMoveTime = Time.time+firePauseTime;
     CalculateAimError();
 
 for(theMuzzlePos in muzzlePositions)
 {
 Instantiate(myProjectile, theMuzzlePos.position, theMuzzlePos.rotation);
 Instantiate(muzzleEffect, theMuzzlePos.position, theMuzzlePos.rotation);
 }
 }


Here is the image of how I have set up. Please help me. Thank you for your time. I am still really new so please dumb it down the best you can, Thank you. alt text

gunturret.jpg (315.7 kB)
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
Best Answer

Answer by robertbu · Mar 08, 2013 at 06:06 PM

You don't add any force to your projectile. Assuming it is aimed the correct way, you would do something like:

 var go  = Instantiate(myProjectile, theMuzzlePos.position, theMuzzlePos.rotation);
 go.rigidbody.AddRelativeForce(Vector3.forward * 2000);

or

 go.rigidbody.AddForce(Transform.forward * 2000);
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 surgesus · Mar 08, 2013 at 06:31 PM 0
Share

@robertbu

Not sure where to add that, I added it to FireProjectile function and gave me some errors, Just saying $$anonymous$$ identifier the$$anonymous$$uzzlePos, so I fix it to my Variable up top as muzzlePositions and this is the code:

 function FireProjectile(){
 
       var go = Instantiate(myProjectile, muzzlePositions.position, muzzlePositions.rotation);
     go.rigidbody.AddRelativeForce(Vector3.forward * 2000);
 
     audio.Play();
     nextFireTime = Time.time+reloadTime;
     next$$anonymous$$oveTime = Time.time+firePauseTime;
     CalculateAimError();

But still didn’t worked, I add that code in the for (the$$anonymous$$uzzlePos in muzzlePositions), because that’s what it look like it was written for, code as follows:

 for(the$$anonymous$$uzzlePos in muzzlePositions)
 {
 
     var go = Instantiate(myProjectile, the$$anonymous$$uzzlePos.position, the$$anonymous$$uzzlePos.rotation);
     go.rigidbody.AddRelativeForce(Vector3.forward * 2000);
     
 Instantiate(myProjectile, the$$anonymous$$uzzlePos.position, the$$anonymous$$uzzlePos.rotation);
 Instantiate(muzzleEffect, the$$anonymous$$uzzlePos.position, the$$anonymous$$uzzlePos.rotation);
 }
 }

Still nothing... . .

avatar image robertbu · Mar 08, 2013 at 06:34 PM 0
Share

The two lines would replace line 7:

 Instantiate(myProjectile, the$$anonymous$$uzzlePos.position, the$$anonymous$$uzzlePos.rotation);

$$anonymous$$ake sure your spawn point is in front of your muzzle. There may be other problems with your script. Are you getting any Console errors.

avatar image surgesus · Mar 08, 2013 at 06:51 PM 0
Share

@robertbu Ok, We got bullets !!!!! Thank you so much it worked like a charm. Now I am going to work on the damage script. It does seem not to readjust its aim after it hits an objects and it moves it (for example my dumpster that is the enemy). But works fine on my FPS. Thank you for the fast response.

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

10 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

Related Questions

firing a shell from a tank turret 1 Answer

How do I make a gun project a particle? 1 Answer

Using the arrow keys to shoot using c# 2 Answers

not losing ammo when shooting 1 Answer

Want to Fire Machine Gun 4 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