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 BogdanZ · Feb 19, 2017 at 10:48 PM · velocitybullet

Why velocity is added on the global coordinates and not the local ones?

Hello, I am trying to shot a bullet but it seems that I am doing something wrong. I started by adding an empty object as a spawn point for the bullet, object that is a child object of the barrel. All good so far. After that, I added a script to that empty object to instantiate and shot the bullet. I have followed some tutorials, I have checked the answers from other questions, but it seems that in my case it doesn't work. I've uploaded 2 images of the coordinates of the bullet. In the first image you can see the local coordinates of the bullet. In the second one you can see the global coordinates. The code I am trying to use looks like this: using UnityEngine; using System.Collections;

public class BulletFiring : MonoBehaviour {

 public GameObject bullet;
 public Transform bulletSpawn;
 public float bulletSpeed = 200f;

 // Use this for initialization
 void Start () {
 }
 
 // Update is called once per frame
 void Update () {
 }

 void FixedUpdate(){
     if (Input.GetKeyDown ("space")) {
         Fire ();
     }
 }

 void Fire() {
     GameObject bulletObject = Instantiate (bullet, bulletSpawn.position, bulletSpawn.rotation) as GameObject;
     bulletObject.GetComponent<Rigidbody> ().velocity = bulletSpawn.forward * bulletSpeed;
 }

}

Can someone explain me what am I doing wrong? Thanks in advance!

local.jpg (22.8 kB)
global.jpg (23.8 kB)
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

1 Reply

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

Answer by MelvMay · Feb 20, 2017 at 10:16 AM

Rigidbody.velocity is in world-space, not local-space of the Rigidbody. If you want it in local-space then you need to transform your velocity vector into local-space.

A simpler way is to add a force using Rigidbody.AddRelativeForce where you specify the force in local-space of the Rigidbody and the call converts that to the appropriate world-space force. You can use an impulse force here (2nd argument) which directly modifies the velocity if required.

Comment
Add comment · Show 7 · 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 BogdanZ · Feb 20, 2017 at 09:36 PM 0
Share

Thank you, Sir! It worked when I transformed the velocity to a local one.

avatar image MelvMay ♦♦ BogdanZ · Feb 20, 2017 at 09:40 PM 0
Share

You're welcome. Could you accept my answer, thanks!

avatar image RobAnthem MelvMay ♦♦ · Feb 20, 2017 at 09:49 PM 0
Share

I'm confused, why would you even want it in local space?

Show more comments
avatar image GopalSir · Jul 24, 2019 at 05:04 PM 0
Share

I need some help visualizing rigidbody velocity to local space of an object . Like what does it actually mean . I understand when i calculate position of a rock local to a tree, its the position of rock when tree is taken as origin. Recently i was trying to calculate the angle of attack of an airplane (angle between wind and chord line of the wing of airplane) . Someone posted that convert the rb.velocity from world space to local space , and then he did something like this :

 localvelocity = transform.InverseTransformDirection(rb.velocity);
         angleOfattack = $$anonymous$$athf.Atan2(-localvelocity.y, localvelocity.z);

which I don't understand , if I calculate the velocity of an object , relative to itself . Shouldn't the answer be zero ( I know I'm missing something here). How does local velocity look different from rb.velocity

avatar image Owen-Reynolds GopalSir · Jul 24, 2019 at 05:27 PM 1
Share

Try any general-purpose description of Local and Global space. Velocity is just an arrow. The math and the way to understand it is the same as for any other arrow.

But the first step is that your local space is relative to your position AND facing. An arrow pointing 2 units "forward" is always (0,0,2) in your local space.

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiate GameObject with Velocity 1 Answer

How to rotate Bullets alongside Gravity 2D (No RigidBody) 0 Answers

Help with script for Bullet velocity 0 Answers

How to make bullet prefab take color of the character? 1 Answer

The bullet never destroyed!!! 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