Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by jgratrix · Dec 29, 2015 at 10:48 PM · bulletspacespace shooterspaceflight

Missle/Bullet script. C#

Okie so im wanting to have a targeting system on my cargo space ship, in a way that i can click on a object in game and then this will show up as a target and when i press space all 8 turrets on my ship will turn slowly to face the target, then once in a 100 radius i can press the space bar and the turrets will fire a round of 5 large bullets which will only travel in a straight line. ive tried to make a target click on object system using:

  void OnMouseDown()
  {
      enemy = gameObject.transform;
  }

however when i click on the object it does not set it as the target.Think ive done that Completely wrong. Would anyone have some pieces of code i can mash together with mine or so?.

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 A_Li_N · Dec 30, 2015 at 04:40 AM

In your example, a gameObject with that script attached would set itself as a target; 'gameObject.transform' is the 'current' gameObject's transform. You would want to attempt to find the object at the mouse's position and set it in the onclick. I do not know how to do that yet :( (all of 2 days of exploration)

It all depends on the organization of your scripts, but this is what I have done (so far):

     public void RotateTowards(Vector3 target)
     {
         //Get the difference between the target location and the object location
         Vector3 dir = target - transform.position;
 
         //Normalize it
         dir.Normalize();
 
         //Find the angle between the current facing and the target, flip it 90 degrees so it is on the Z axis
         float zAngle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg - 90;
         
         //Get the rotation that the object would be at when pointing at the target
         Quaternion desiredRot = Quaternion.Euler(0, 0, zAngle);
 
         //Get what the rotation would be if we applied it
         Quaternion rotationAtEndOfThisFrame = Quaternion.RotateTowards(transform.rotation, desiredRot, RotationSpeed * Time.deltaTime);
 
         //If we are not attached to something or if MaxRotation is 0 or the end difference between the current rotation and rotationAtEndOfThisFrame is less than MaxRotation
         if (transform.parent == null || MaxRotation == 0 || Quaternion.Angle(rotationAtEndOfThisFrame, transform.parent.rotation) <= MaxRotation)
         {
             //Set the rotation
             transform.rotation = rotationAtEndOfThisFrame;
         }
     }


'target' can be any vector3, meaning another transform's position or the mouse's position.

Comment
Add comment · 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
0

Answer by jgratrix · Dec 30, 2015 at 01:21 PM

@A_Li_N

Hey thanks for that, yes i found out a slightly easier way to find the target using raycast:

  void Update()
     {
         if (Input.GetMouseButtonDown(0))
         {
             Debug.Log("Mouse is down");
 
             RaycastHit hitInfo = new RaycastHit();
             bool hit = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);
             if (hit)
             {
                 Debug.Log("Hit " + hitInfo.transform.gameObject.name);
                 if (hitInfo.transform.gameObject.tag == "Enemy")
                 {
                     Debug.Log("It's working!");
                 }
                 else {
                     Debug.Log("nopz");
                 }
             }
             else {
                 Debug.Log("No hit");
             }
         }
     }

So all i would need to do is set HitInfo equal to target then wallah, make a simple missile script and use this target and it should work. As for the turret i should be able to use Tranform.LookAt(HitInfo); and it should look at the target.

Comment
Add comment · 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

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

35 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to stop this Behaivour after its done with its its job. 0 Answers

Can you point me in the direction of how to get space physics like the one in this video? 1 Answer

Detect if Game Object has been clicked on in 3Dspace 1 Answer

Particle rotation problems in Unity 2017.1.0f3 2 Answers

Screen Space - Camera makes bullets fly in wrong directions 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