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 /
avatar image
0
Question by Techn0man · Nov 22, 2017 at 05:47 PM · rotationweaponsship

Rotating weapon output Vector3[]

Hi, I have a ship that shoots out a lazer via Raycast and I set the position with Vector3[] so that I can add more or remove some locations later on, I also have a missile script thats similar but it creates an object that travels forwards and explodes. but the problem is that the output doesn't follow the ships rotation. heres my current script for the missile and lazers:

 //missile script
     public float maxReloadTime = 25f;
     public string input = "Fire2";
     public GameObject missile;
     public Vector3[] weaponOutput;
 
     Vector3[] globalPoints;
 
     bool isReloading = false;
 
     private void Start()
     {
         globalPoints = new Vector3[weaponOutput.Length];
         for (int i = 0; i < weaponOutput.Length; i++)
         {
             globalPoints[i] = weaponOutput[i] + transform.position;
         }
     }
 
     private void Update()
     {
         if (Input.GetButton(input) && isReloading == false)
         {
             StartCoroutine(ShootMissile());
         }
     }
 
     IEnumerator ShootMissile()
     {
         if (weaponOutput != null)
         {
             for (int i = 0; i < weaponOutput.Length; i++)
             {
                 Instantiate(missile, );
                 isReloading = true;
 
                 yield return new WaitForSeconds(maxReloadTime);
                 isReloading = false;
             }
         }
     }
 
     private void OnDrawGizmosSelected()
     {
         float size = 0.1f;
 
         if (weaponOutput != null)
         {
             for (int i = 0; i < weaponOutput.Length; i++)
             {
                 Gizmos.color = Color.red;
                 Vector3 globalWaypointPos = weaponOutput[i] + transform.position;
                 Gizmos.DrawLine(globalWaypointPos - Vector3.up * size, globalWaypointPos + Vector3.up * size);
                 Gizmos.DrawLine(globalWaypointPos - Vector3.left * size, globalWaypointPos + Vector3.left * size);
                 Gizmos.DrawLine(globalWaypointPos - Vector3.forward * size, globalWaypointPos + Vector3.forward * size);
             }
         }
     }
 
 //Lazer script
     public float damage = 10f;
     public float force = 30f;
     public float range = 100f;
     public float fireRate = 15f;
 
     public string input = "Fire1";
 
     public ParticleSystem muzzleFlash;
     public GameObject impactEffect;
 
     public Vector3[] weaponOutput;
 
     Vector3[] globalPoints;
     float nTTF = 0f;
 
     private void Start()
     {
         globalPoints = new Vector3[weaponOutput.Length];
         for (int i = 0; i < weaponOutput.Length; i++)
         {
             globalPoints[i] = weaponOutput[i] + transform.position;
         }
     }
 
     private void Update()
     {
         if (Input.GetButton(input) && Time.time >= nTTF)
         {
             nTTF = Time.time + 1f / fireRate;
             Shoot();
         }
     }
 
     void Shoot()
     {
         if (muzzleFlash != null)
         {
             muzzleFlash.Play();
         }
 
         if (weaponOutput != null)
         {
             for (int i = 0; i < weaponOutput.Length; i++)
             {
                 RaycastHit hit;
                 if (Physics.Raycast(transform.position, transform.forward, out hit, range))
                 {
                     HP hp = hit.transform.GetComponent<HP>();
                     if (hp != null)
                     {
                         hp.TakeDamage(damage);
                         Debug.Log(hit.transform.name + hp.Health);
                     }
 
                     if (hit.rigidbody != null)
                     {
                         hit.rigidbody.AddForce(-hit.normal * force);
                     }
 
                     if (impactEffect != null)
                     {
                         GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
                         Destroy(impactGO, 2f);
                     }
                 }
             }
         }
     }
 
     private void OnDrawGizmosSelected()
     {
         float size = 0.1f;
 
         if (weaponOutput != null)
         {
             for (int i = 0; i < weaponOutput.Length; i++)
             {
                 Gizmos.color = Color.red;
                 Vector3 globalWaypointPos = weaponOutput[i] + transform.position;
                 Gizmos.DrawLine(globalWaypointPos - Vector3.up * size, globalWaypointPos + Vector3.up * size);
                 Gizmos.DrawLine(globalWaypointPos - Vector3.left * size, globalWaypointPos + Vector3.left * size);
                 Gizmos.DrawLine(globalWaypointPos - Vector3.forward * size, globalWaypointPos + Vector3.forward * size);
             }
         }
     }
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

0 Replies

· Add your reply
  • Sort: 

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

100 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 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 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 do i keep an object from rotating? 2 Answers

How to turn ship smoothly to mouse position when mouse click? 0 Answers

Rotation Speed 1 Answer

Rotate a ship so cannons can face the objective (another ship) 2 Answers

Set rotation based on where player is facing 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