• 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
1
Question by roboaedin · Oct 20, 2014 at 08:54 AM · rotationinstantiate prefab

Rigidbody Velocity Not Added Properly

I have a problem with adding velocity via JavaScript. This is that an object which is instantiated with the correct rotation and position is only given velocity as if the character were facing straight(level with ground).

I am doing this for a class in video game developement, and this assignment is already late, so it is 'imperative' for me to get it finished soon.

My Coding:

 function Shoot(){
     if(CanBeShot==true&&Reloading==false){
         //Insantiate a bullet at the tip of the gun
         var firedBullet:Rigidbody=Instantiate(Bullet,gunPos.position,gunPos.rotation);
         //Gun position(gunPos) is set to the tip of the gun, which has no collider
         firedBullet.velocity=transform.TransformDirection(Vector3(0,0,BulletSpeed));
         //Instantiate a flash effect
         Instantiate(MuzzleFlash,gunPos.position,gunPos.rotation);
         Physics.IgnoreCollision(firedBullet.collider,transform.root.collider);
         //Subtract from remaining ammo
         AmmoRemaining--;
         //Irrelevant to the problem:
         CanBeShot=false;
         yield WaitForSeconds(0.5);
         CockingSound.audio.Play();
         yield WaitForSeconds(0.5);
         CanBeShot=true;
     }
 }

Code in tutorial:

 var instantiatedProjectile:Rigidbody=Instantiate(projectile,transform.position,Vector3(0,-1,0),transform.rotation);
 instantiatedProjectile.velocity = transform.TransformDirection(Vector3(0,0,speed));
 Physics.IgnoreCollision(instantiatedProjectile.collider,transform.root.collider);
Comment
Add comment · Show 1
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 Wisearn · Oct 20, 2014 at 09:05 AM 0
Share

And what is the problem?

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Kiwasi · Oct 22, 2014 at 02:36 AM

Why are you using two different transforms to set the rotation and velocity? Currently there is no relationship in your script between the bullet rotation and the bullet velocity.

You need to either set the velocity based on the firedBullet.transfrom.forward or gunPos.forward. Change line 6 to the following.

 firedBullet.velocity = firedBullet.transform.forward * BulletSpeed;

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 roboaedin · Oct 22, 2014 at 05:53 AM 0
Share

First, I fixed it-- I was sure I had it on the camera, but the script was actually on the player. Second, is there any way to revoke a question asked? Third, I hadn't uploaded the whole script. That was just a snipped which I added comments to-- the part relevant to the former problem.

avatar image
1

Answer by Owen-Reynolds · Oct 20, 2014 at 05:25 PM

Which direction is +Z on the gun? (the blue arrow, when the translate tool is set to local.) Is this script on the gun, or some other object?

o The line transform.TransformDirection(Vector3(0,0,BulletSpeed)); says to push the bullet along local +Z. Because standard Unity assumes that +Z is always forwards. Lots of people have models that aim +y or -z, ... . Several well-known ways to fix that.

This is so common, that transform.forward is a shortcut for tran.transDir(V3(0,0,1));. Both mean "my local forward." So most people would just write: velocity=transform.forward*bulletSpeed;.

o In transform.transDir... the transform refers to you -- the thing with the script on it. So if this script is on the player, it will fire the bullet whichever way the player is facing. It looks like the player "knows about" the gun through gunPos. So use gunPos.transDir... or just gunPos.forward.

Comment
Add comment · Show 4 · 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 roboaedin · Oct 20, 2014 at 05:40 PM 0
Share

Wouldn't gunPos.forward move the gun tip forward A bit more info: Z is the correct axis. It is moving along that axis properly. The problem is that the bullet is ONLY being moved on that axis, with the exception of gravity. Despite the local Z axis of the bullet being pointed upward, it will only move on the local(to the character) Z axis. The actual rotation of the bullet is normal, right at the tip of the gun model I'm using. I can make a video as an example if necessary.

avatar image roboaedin · Oct 20, 2014 at 05:45 PM 0
Share

The gun and script are attached to the player's camera.

avatar image Owen-Reynolds · Oct 21, 2014 at 12:05 AM 0
Share

Wait. If the bullet has +Z facing up, and you say in your Q that the bullet has the correct rotation, that means the gun is facing up?

Is it a first person camera, so the gun always faces the way the camera does? So the camera is also facing up? Because the current code uses the camera's blue arrow as the firing direction.

One trick is just to simplfy. Fixed overhead camera. Try to fire a bullet from a "cannon" stuck to the ground. $$anonymous$$ake sure the cannon is spun and tilted, to be sure the direction is working. Can split screen Scene mode to look from any angle.

avatar image roboaedin · Oct 22, 2014 at 02:12 AM 0
Share

Yes, the gun is facing up. Yes, it's a first person camera. It shoots forward-- ONLY forward. If I aim upwards, it will still only go in that one direction. It is instantiated properly, with the right rotation, but the velocity does not seem to take that into account. This needs to be first-person.

I can get a video of the project uploaded if you need more information.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiated Projectiles doesn't go in correct direction! 1 Answer

I need to summon projectiles 2 Answers

Instantiate rotation is not correct, why? 1 Answer

Flip over an object (smooth transition) 3 Answers

Trying to implement a cone shot in a 2D Game. 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