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 behnker · Jul 29, 2010 at 06:22 PM · raycastshootingprojectileshootcrosshair

Air Strike shoot aiming at cross hairs in middle of the screen

I am looking to create an airstrike as a third weapon of my game. I can get it to fire a rocket and it fires in the direction that im facing but the issue that im running into is having it hit where my cross hairs are facing. It would also be a possibility of creating new cross hairs while in the airstrike mode that are on the ground in front of you. Here is my current code. Any thoughts?

//code current

var projectile : Rigidbody;

var initialSpeed = 20.0;

var reloadTime = 0.5;

var ammoCount = 20;

private var lastShot = -10.0;

function Fire () {

 // Did the time exceed the reload time?
 if (Time.time > reloadTime + lastShot && ammoCount > 0) {
     // create a new projectile, use the same position and rotation as the Launcher.
     var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation);

     // Give it an initial forward velocity. The direction is along the z-axis of the missile launcher's transform.
     instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0, 0, initialSpeed));

     // Ignore collisions between the missile and the character controller
     Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);

     lastShot = Time.time;
     ammoCount--;
 }

}

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 qJake · Jul 29, 2010 at 06:47 PM 0
Share

Format your code by selecting it and hitting the Code Format button.

avatar image behnker · Jul 29, 2010 at 07:28 PM 0
Share

Thanks. This is my first post and I couldnt figure that out.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Mike 3 · Jul 29, 2010 at 07:47 PM

By the looks of it, just change:

instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0, 0, initialSpeed));

to

instantiatedProjectile.velocity = Camera.main.ScreenPointToRay(Input.mousePosition).direction * initialSpeed;
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 behnker · Jul 29, 2010 at 08:04 PM 0
Share

It didnt work. I think its because there is not mousePosition. I trying to add this to the FPS tutorial for testing purposes. So you have your center crosshairs and no mouse. The airstrike spawns way above the character. It shoots in the direction of the cross hairs just not in the center of the screen that I am looking at. I think I have to switch this to some form of RayCast just not certain on how that works fully.

avatar image Mike 3 · Jul 29, 2010 at 08:28 PM 0
Share

It may be better to explain how you're doing the crosshairs then, I assumed it was where the mouse was.

avatar image behnker · Aug 03, 2010 at 07:16 PM 0
Share

Thanks for the help $$anonymous$$ike. It sent me in the right direction of figuring out the answer.

avatar image
0

Answer by behnker · Aug 03, 2010 at 07:15 PM

Ok I figured it out. Below is the code that works for me. If anyone is going to set this up in their game I would say go a step further and add a cross hair that runs accross the ground and put the game object with this script attached to the ground cross hair that way when ever you do the air strike it shoots down onto the object.

var projectile : Rigidbody; var initialSpeed = 20.0; var reloadTime = 0.5; var ammoCount = 20; private var lastShot = -10.0;

function Fire () { // Did the time exceed the reload time? if (Time.time > reloadTime + lastShot && ammoCount > 0) { // create a new projectile, use the same position and rotation as the Launcher. var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation);

     // Give it an initial forward velocity. The direction is along the z-axis of the missile launcher's transform.
     //instantiatedProjectile.velocity = Camera.main.ScreenPointToRay(Input.mousePosition).direction * initialSpeed;;

     //Sets up the RayCast to the direction of the cross hairs
     var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     var hit : RaycastHit;
     Physics.Raycast(ray, hit);

     //gives position of the RayCast
     instantiatedProjectile.MovePosition(hit.point);

     //gives the initial forward velocity
     instantiatedProjectile.velocity = ray.direction * initialSpeed;

     // Ignore collisions between the missile and the character controller
     Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);

     lastShot = Time.time;
     ammoCount--;
 }

}

Comment
Add comment · Show 2 · 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 HolBol · Aug 03, 2010 at 09:16 PM 0
Share

Anyway to change this so the projectile doesn't hit instantly?

avatar image Ullukai · Mar 27, 2011 at 08:21 PM 0
Share

@Fishman92 -maybe adjust the initialspeed variable

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

Projectile Hit Detection with Bullet Drop 1 Answer

Random Raycast inside crosshairs 1 Answer

Problem in Shooting Accuracy 0 Answers

How to stop enemies from shooting each other 1 Answer

Aiming to the crosshair 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