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 Frolanta · Sep 22, 2014 at 09:04 PM · aimcrosshairspaceship

spaceship aim

I want to rework the aim system of my game because i'm not satisfied at all with the current one. This is what I want to achieve : https://www.youtube.com/watch?v=pdEXh_Wjlo0 And what I have now : https://dl.dropboxusercontent.com/u/25407702/flyingship/FlyingShip.html (W, A, S, D to move , LeftShip for boost, 1 /2 to switch between machine gun and rail gun)

At first i wanted a 3D crosshair so I put mine on a plane and tried to align it with the bullet impact. But I can't get greats results, at some correct distances it works nicely, but at close or long range, impacts are not in the center of my crosshair. So i want to rework it like on the youtube's video, but can't think of the right way to do it.

I think i need to render my crosshair on the GUI and follow the mouse, but for projectiles going where i'm aiming, I'm stuck.

Projet's github : https://github.com/Frolanta/FlyingShipProject (If you interested)

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

1 Reply

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

Answer by sevensixtytwo · Sep 23, 2014 at 12:27 AM

You can use the GUITexture object for your crosshair and a raycast to find out where the shots will land. Then simply convert the raycast's hit to viewport space and place the crosshair there.

 public var muzzle : Transform;
 public var crosshair : Transform;
 public var viewport : Camera;
 
 public var maxDistance : float = 1000;
 
 function Update () {
 
     var ray : Ray = new Ray(muzzle.position,muzzle.forward);
     var hit : RaycastHit;
     if (Physics.Raycast(ray,hit,maxDistance)) {
         crosshair.position = viewport.WorldToViewportPoint(hit.point);
         }
     else {
         crosshair.position = viewport.WorldToViewportPoint(muzzle.forward * maxDistance);
         }
 
      }

"muzzle" should be an empty gameObject that is aligned to the origin of your shots. "crosshair" should be a GUITexture object. "viewport" will be a camera. "maxDistance" is the length of the ray. if the target is too far, the crosshair will be set at maxDistance.

For mouse aiming, it's a bit different.

 public var ship : Transform;
 public var crosshair : Transform;
 public var viewport : Camera;
 
 public var maxDistance : float = 1000;
 
 function Update () {
 
     var ray : Ray = viewport.ScreenPointToRay(Input.mousePosition);
     var hit : RaycastHit;
     if (Physics.Raycast(ray,hit,maxDistance)) {
         crosshair.position = viewport.WorldToViewportPoint(hit.point);
         ship.LookAt(hit.point);
         }
     else {
         crosshair.position = viewport.WorldToViewportPoint(muzzle.forward * maxDistance);
         ship.LookAt(muzzle.forward * maxDistance);
         }
 
     }

"ship" is the transform of your player. The other variables are the same as before. This does much the same as before, except that the ray originates from the camera towards the mouse position and the ship locks onto the point indicated by the raycast.

Both methods have the added advantage of the crosshair NOT scaling relative to the distance. It will always stay the same size.

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 Frolanta · Sep 23, 2014 at 08:53 PM 0
Share

Thanks, I implemented your first solution and I got something close to why I wanted. I think the second solution with mouse aim can be better for me but it need some tweek for me, I will try this later.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Add an aim to a space ship simulator 0 Answers

2D Platform Shooter Camera Controls C# 0 Answers

crosshair help needed 2 Answers

Deadfrontier type crosshair 1 Answer

Crosshair Aiming Issue, Simple, Yet Confusing. 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