Rigidbody and velocity

I set the velocity of a rigidbody that I instantiate to some value. Is the velocity going to drop during some time and will the rigidbody fall to the ground or will the rigidbody continue to travel ?

Hello!

Setting the velocity should not drop as long as the rigidbody does not have any drag and does not collide with anything.rigidbody.drag = 0;To prevent gravity from making the rigidbody fall, useGravity should be set to false.rigidbody.useGravity = false;Remember that there is an element of randomness to physics, so how the rigidbody reacts to collisions will be different every time. Eg. The velocity and angle the rigidbody bounces off a wall cannot be predetermined.

Thank you, it seems to work.

I would like to test it also with kinematic rigidbody. But than I cant use velocity. So is it possible to move a kinematic rigidbody with constant speed just by using transform of the object ?
I use Instantiate() so I have no control of the object. The speed should be set just after it is instantiated.

If you have isKinematic == true then you can move the object using its transform like anything else.

public Transform somePrefab;
private Transform thingToMove;

void Start() {
  thingToMove = Instantiate(somePrefab) as Transform;
}

void Update() {
  thingToMove.position += Vector3.right * Time.deltaTime;
}

Hmm, yea but I instantiate my objects in Update() so after this function ends I don’t have any control of the objects. So I cant use thingToMove.position.

Actually I instantiate bullets from a car that try to hit the target.

Here is some example code to get you started. I haven’t actually tested this!

using UnityEngine;

public class Gun : MonoBehaviour {
  public Bullet bulletPrefab;

  void Update() {
    if (Input.GetMouseButton(0)) {
      Bullet newBullet = Instantiate(bulletPrefab, transform.position, Quaternion.identity) as Bullet;
      newBullet.Init(transform.forward);
    }
  }
}
using UnityEngine;

public class Bullet : MonoBehaviour {
  private Vector3 bulletDirection;
  private float bulletSpeed = 25;

  public void Init(Vector3 direction) {
    bulletDirection = direction;
  }

  public void Update() {
    transform.position += bulletDirection.normalized * bulletSpeed * Time.deltaTime;
  }
}

Yea, that way it should work by putting another script in Bullet. Thx.