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 Cobradabest · Jan 05, 2013 at 12:51 AM · c#collisionrigidbodyprojectiles

Why are my projectiles facing the wrong way?

I'm making a weapon for my FPS game which shoots darts, it took me ages find a tutorial for this that worked, but now I have another problem:

My projectiles are facing the wrong way, they're pointing down rather than forward, how do I fix this?

Here's the code:

 using UnityEngine;
 using System.Collections;
 
 public class Launcher : MonoBehaviour {
     
     public Rigidbody projectile; //Variable to hold our projectile object of type Rigidbody.
     public short speed= 999; //Variable 'speed' which determines how fast our projectile will shoot.
     
     void  Start ()
     {
     }
     void  Update (){
         if (Input.GetButtonDown("Fire1")) //If player clicks left mouse button.
         {
             Rigidbody instantiatedProjectile = (Rigidbody)Instantiate(projectile, transform.position, transform.localRotation);
             instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0, speed));
             Physics.IgnoreCollision(instantiatedProjectile.collider, transform.collider);
         }
     }
 }

Also, how do I destroy projectiles after a certain period of time, or if there's too many in the game?

I also want to make it so that once they're dropped, the player can pick them up again, how would I do that?

Comment
Add comment · Show 6
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 fafase · Jan 05, 2013 at 12:57 AM 0
Share

Your object uses transform.localRotation. You should consider the transform.forward of the root object.

avatar image Cobradabest · Jan 05, 2013 at 01:14 AM 0
Share

Isn't forward a vector3 value? How would I put it in a quaternion slot? Or do I do something else entirely?

avatar image fafase · Jan 05, 2013 at 01:23 AM 0
Share

Try Quaternion.Euler(Vector3);

avatar image Cobradabest · Jan 05, 2013 at 01:27 AM 0
Share

They're still pointing down when shooting... Well, thanks anyway.

avatar image AlucardJay · Jan 05, 2013 at 03:11 AM 0
Share

transform is a Vector3, therefore :

 Vector3.forward // gives a world-space value
 transform.forward // gives a value from local-space perspective

Same for the other Vector3 class variables, try it out.

Looks like $$anonymous$$ad$$anonymous$$ike has the answer (upvoted), if your model is not facing forward, put it as a child of an empty gameObject, correct the alignment, then save the empty with the rotated model as a prefab, use that.

Show more comments

1 Reply

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

Answer by madmike6537 · Jan 05, 2013 at 02:52 AM

To my knowledge this has to with the model itself and it's hard to fix. The simple way to fix it is to drop it inside an empty game object and then adjust the rotation of the child object.

Also look in to the destroy command, it has a automatic command to destroy after so many seconds.

Comment
Add comment · Show 8 · 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 Cobradabest · Jan 05, 2013 at 02:58 AM 0
Share

I'm confused. So you drop the prefab into the scene parenting it with an empty?

avatar image madmike6537 · Jan 05, 2013 at 02:58 AM 0
Share

Parent the projectile with an empty and update it as the prefab after you get it so it's ai$$anonymous$$g the right way.

avatar image madmike6537 · Jan 05, 2013 at 03:03 AM 0
Share

Drag a copy of the projectile into scene. Create empty game object. Drag projectile into the empty to child it. Adjust rotation until facing the right way based on the parent empty. Now save as the prefab.

avatar image Cobradabest · Jan 05, 2013 at 03:55 PM 0
Share

That kind of works, but it points in the same direction regardless of where I shoot, even when I shoot the other way!

avatar image madmike6537 · Jan 05, 2013 at 03:58 PM 0
Share

Hmm its got something to do with how you are moving it with velocity. I am not as familiar with that method. Just curious why not just use rigidbody.addforce?

Show more comments

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

11 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

Related Questions

Need help with scripting bug fix. pleease 0 Answers

[Solved] Is it okay if we change transform.position on a kinematic rigid body which also does not use Gravity? 2 Answers

OnCollisionEnter not being called, between a Rigidbody and a Box Collider 1 Answer

Any way to ignore collision between rigidbodies and colliders/character controllers? 1 Answer

C# GameObject is not detecting collision with Character Controller 2 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