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!
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.
Thank you, Sir! It worked when I transformed the velocity to a local one.
You're welcome. Could you accept my answer, thanks!
I'm confused, why would you even want it in local space?
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
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
![](https://koobas.hobune.stream/wayback/20220612111331im_/https://answers.unity.com/themes/thub/images/avi.jpg)