- Home /
My bullets wont slow down
When my bullet is fired from the turret all i can see if the flash of the bullet then it is gone. It hits the target perfectly.
i also try to add my own var called speed and that didnt do anything it just wont slow down. Here is my code.
var LookAtTarget:Transform; var damp = 1.0f; var bullitPrefab:Transform; var savedTime; private var range : float = 1000f; private var nextFire : float = 0.1f;
function Update ()
{ if(LookAtTarget) {
var distance = Vector3.Distance(LookAtTarget.transform.position, transform.position);
var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
if (distance <= range) { transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp); }
var seconds : int = Time.time; var oddeven = (seconds & 9999); //shots per second if(oddeven) { Shoot(seconds); //goes to shoot funcation } } }
function Shoot(seconds) { var distance = Vector3.Distance(LookAtTarget.transform.position, transform.position); if (distance <= range)
if(seconds!=savedTime && Time.time > nextFire) {
var bullit = Instantiate(bullitPrefab, transform.Find("Spawnpoint").transform.position ,Quaternion.identity);
bullit.rigidbody.AddForce(transform.forward * 1000); //how fast bullet travels
savedTime=seconds; } }
Answer by fafase · Dec 02, 2012 at 01:46 PM
Aaaah, If Unity would get a penny every time someone using the Tornado Twins tutorial comes to ask a question, Unity pro would be free...
Well, there is no reason not to learn a "better" way:
var LookAtTarget:Transform;
var _transform :Transform;
var damp = 1.0f;
var bullitPrefab:Transform;
private var rangeSqr : float = 10000f; //The range is squared
private var spawn:Transform; //The spawn is cached
var wait:float;
var seconds:float;
function Start(){
_transform = GetComponent(Transform); //Cache the transform of the turret
spawn = _transform.Find("Spawnpoint").transform;
if(!LookAtTarget)LookAtTarget = GameObject.Find("Player").GetComponent(Transform);
}
function Update (){
if ((LookAtTarget.position - _transform.position).sqrMagnitude <= rangeSqr){
var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
_transform.rotation = Quaternion.Slerp(_transform.rotation, rotate, Time.deltaTime * damp);
seconds += Time.deltaTime;
if(seconds > wait) {
Shoot(); //goes to shoot function
seconds = 0;
}
}
}
function Shoot() {
var bullit = Instantiate(bullitPrefab, spawn.position ,Quaternion.identity);
bullit.rigidbody.AddForce(_transform.forward * 1000);
}
First it looks shorter. Also, the Find function is now done only once. Avoid a Find function in the Update when you can. Here you could. My shoot function has two lines only. The whole timer system is way simple now. No more modulus calculation (yes your & should be a % in (seconds & 9999)<- this is bitwise manipulation) and parameters being passed.
Also, avoid Distance function when you can. Distance uses sqr root which is to be left on the side. Use the squared magnitude instead which is way cheaper on resources.
Let us know if I made any mistakes there...
Thanks for getting back to me but it says that line 15 Cannot convert 'UnityEngine.GameObject' to 'UnityEngine.Transform'
And also line 19 says '<=' cannot be used with a left hand side of type 'float' and a right hand side of type 'function(int):
I fixed the first mistake, the second range should be rangeSqr.
hi thanks for the help so far, i have no error messages now but it the bullet still wont slow down, it appears for a second then disappears, i tried putting the AddForce to a low number and even high but still wont slow down
I swear that tutorial causes more questions than it answers. (plus - bullit? really? REALLY?)
Is there any code on the bullet itself? Is it hitting its source with low force and then destroying itself? Is it a solid object (non-trigger collider) being created inside another solid object?
Comment out the
bullit.rigidbody.AddForce(_transform.forward * 1000);
If your "bullit" still moves, you have something somewhere else.
Your answer
Follow this Question
Related Questions
How to detect an object that comes in the "Crosshair" field of view? 1 Answer
Turret control Problem 4 Answers
Can I randomize LookRotation? 1 Answer
Fire in Unity3D - Fire particles 1 Answer