I am making a small project in Unity, and whenever I walk with the gun and shoot at the same time, the bullets seem to curve and shoot off 2-3 CMs from the center. When I stand still this doesn’t happen.
This is my main Javascript code:
@script RequireComponent(AudioSource);
var projectile : Rigidbody;
var speed = 500;
var ammo = 30;
var fireRate = 0.1;
private var nextFire = 0.0;
function Update() {
if(Input.GetButton ("Fire1") && Time.time > nextFire) {
if(ammo != 0) {
nextFire = Time.time + fireRate;
var clone = Instantiate(projectile, transform.position, transform.root.rotation);
clone.velocity = transform.TransformDirection(Vector3 (0, 0, speed));
ammo = ammo - 1;
audio.Play();
} else {
}
}
}
I assume that these two lines need to be tweaked:
var clone = Instantiate(projectile, transform.position, transform.root.rotation);
clone.velocity = transform.TransformDirection(Vector3 (0, 0, speed));
Thanks in advanced, and please remember that I just started Unity, and I might have a difficult time understanding some things.
By the way, since it is pretty hard to understand, I created a video showing the problem.
Here is the video - The World's Best Creator Platform for Online Workplace Learning
Thanks.
EDIT:
I think, like other people said, raycasting is a good method to use. Can some one rewrite my code, so AFTER each collision the “bullet” gets destroyed, because in my code, the bullet can bounce off trees and go very slowly and stuff, but when I destroy it there is no actual impact, the bullet just gets destroyed right when it hits the cube, while it is supposed to make an impact on cube, and then destroy itself.