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 WeeGee9000 · Jul 28, 2013 at 12:38 AM · c#projectileshootflight

I shoot a projectile, but its rotation is wrong (c#)

Hello again.

I made a few scripts in my flight sim so my aircraft shoots projectiles, but i can't figure out a way to make it instantiate in the angle that i want.

Projectile Script:

 using UnityEngine;
 using System.Collections;
 
 public class Projectile : MonoBehaviour {
     
     public float speed = 60.0f;
 
     // Use this for initialization
     void Start () 
     {
     
     }
     
     // Update is called once per frame
     void Update () 
     {
 
         transform.Translate(new Vector3(0.0f, 0.0f ,speed*Time.deltaTime ));
     }
 }





Script that spawns the Projectile:

 using UnityEngine;
 using System.Collections;
 
 public class shipshoot : MonoBehaviour {
     public GameObject playerProjectile;
     public Transform Source;
     public float TimeBetweenShots = 0.2f;
     
     private float _NextSHot = 0.0f;
     
     // Use this for initialization
     void Start () {
         
         Source = transform;
     
     }
     
     // Update is called once per frame
     void Update () {
         
         if (Input.GetMouseButton(0) && Time.time > _NextSHot)
         {
             _NextSHot = Time.time + TimeBetweenShots;
             Instantiate(playerProjectile, Source.position, Source.rotation);
         
         }
 }
 }

The result of everything above is this: alt text

But i want the projectile to come out like this: alt text

Any ideas?

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 cdrandin · Jul 28, 2013 at 01:22 AM 0
Share

Place the object in the scene. Place how you would want it to be spawned then from there make it a prefab and then instantiate the prefab. It should work. ALso double check rotation is Quaternion.Identity.

avatar image WeeGee9000 · Jul 28, 2013 at 01:35 AM 0
Share

Your idea didn't work. The projectile still spawns as a regular capsule would.

Also when i use Quaternion.Identity, the projectile spawns at the RIGHT side of the ship ins$$anonymous$$d of the front.

1 Reply

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

Answer by robertbu · Jul 28, 2013 at 01:45 AM

The easiest way to fix this problem is to create an empty game object, make the capsule a child of the empty game object and then apply a rotation to the visible object. Scripts go on the empty game object.

You can also calculate the rotation. You said 'capsule' so I'm assuming you want the 'y' of the capsule to point forward. And I'm assuming you are firing out the front (side facing positive 'z' when the ship has no rotaiton). Put these three lines in place of line 24:

 GameObject go = Instantiate(playerProjectile, Source.position, Quaternion.identity) as GameObject;
 Quaternion q = Quaternion.FromToRotation(Vector3.up, transform.forward);
 go.transform.rotation = q * go.transform.rotation; 
Comment
Add comment · Show 5 · 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 WeeGee9000 · Jul 28, 2013 at 02:12 AM 0
Share

I get error CS0266 : Cannot implicitly convert type UnityEngine.Object' to UnityEngine.GameObject'. An explicit conversion exists (are you missing a cast?)

avatar image WeeGee9000 · Jul 28, 2013 at 02:17 AM 0
Share

your first suggestion didn't help either. Ins$$anonymous$$d of getting the projectile correctly, i got nothing

avatar image robertbu · Jul 28, 2013 at 02:53 AM 0
Share

I fixed the script above by adding 'as GameObject'. For the first one to work, you need the position of the child (visible) object to be at (0,0,0) relative to the parent. You probably have an offset.

avatar image WeeGee9000 · Jul 28, 2013 at 02:54 AM 0
Share

Thank you.

avatar image kacyesp · Sep 04, 2014 at 04:31 AM 0
Share

@robertbu Hey, robert, is there any simple intuition behind multiplying the Quaternions to get the rotation I want. It works, I just don't get why.

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

15 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

My projectile wont fire in the right direction 2 Answers

Boomerang projectile doesn't return to player if the player has moved 1 Answer

Photon Unity Networking - Projectile Syncing problem 0 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