- Home /
making a turet shoot
so i downloaded the fps add on put a turret down and it wouldnt fire at me it only looked at me and the muzzle flash was still there so since im not the best scripter around i thought id use the machine gun script to edit it and made this:
var attackRange = 30.0; var shootAngleDistance = 10.0; var target : Transform; var damage = 5.0; var fireRate = 0.05; var force = 10.0; private var hitParticles : ParticleEmitter;
function Start () { if (target == null && GameObject.FindWithTag("Player")) target = GameObject.FindWithTag("Player").transform; hitParticles = GetComponentInChildren(ParticleEmitter);
// We don't want to emit particles all the time, only when we hit something.
if (hitParticles)
hitParticles.emit = false;
}
function Update () { if (target == null) return;
if (!CanSeeTarget ()) return;
// Rotate towards target
var targetPoint = target.position; var targetRotation = Quaternion.LookRotation (targetPoint - transform.position, Vector3.up); transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0);
// If we are almost rotated towards target - fire one clip of ammo var forward = transform.TransformDirection(Vector3.forward); var targetDir = target.position - transform.position; if (Vector3.Angle(forward, targetDir) < shootAngleDistance) SendMessage("Fire");
}
function CanSeeTarget () : boolean { if (Vector3.Distance(transform.position, target.position) > attackRange) return false;
var hit : RaycastHit; if (Physics.Linecast (transform.position, target.position, hit)) return hit.transform == target;
return false;
}
function FireOneShot () { var direction = transform.TransformDirection(Vector3.forward); var hit : RaycastHit ;
// Did we hit anything? if (Physics.RaycastHit (transform.position, direction, hit, range)) { // Apply a force to the rigidbody we hit if (hit.rigidbody) hit.rigidbody.AddForceAtPosition(force * direction, hit.point);
// Place the particle system for spawing out of place where we hit the surface!
// And spawn a couple of particles
if (hitParticles) {
hitParticles.transform.position = hit.point;
hitParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
hitParticles.Emit();
}
}
}
but it said 'RayCastHit' is not a member of 'UnityEngine.Physics' would that script work fine if the RayCastHit was changed if not how could i edit it to make it fire at me snd do damage? thanks!
Answer by Richard J. Hansen · Apr 27, 2011 at 01:15 AM
This gave me trouble the first time I dealt with Raycasts as well.
If you scroll down here: http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html?from=RaycastHit you will see an example of the proper way to use RaycastHit.
You basically create a RaycastHit (you would call it hit, in your code). This does not perform the raycast, but creates a container to store information about what the raycast hits. If you use Physics.Raycast (transform.position, -Vector3.up, hit)
it will store the information in the RaycastHit specified by hit.
There might be other things wrong with your code but that is the thing that is immediately obvious to me.
Looking at your code some more... Not only should you change Physics.RaycastHit to Physics.Raycast, but you do not seem to define range (at the top of your script you call it attackRange. I'm also not sure you can call direction like that (I always call that type of information as part of the transform).
One thing that may help you once you are no longer getting error messages is to throw in a debug draw line in there.
Answer by Meater6 · Apr 27, 2011 at 01:21 AM
Best way to see if it works is to test it. So to solve the problem you say, I would just change the Physics.RaycastHit to Physics.Raycast, for it seems like that is the problem. That should work, but as I said earlier,you should test it. Of course, I see no function or line of code that deals damage to a target. For that, you need to subtract the target's health (Which is defined in a player status script) when hit. Try looking at this question: Question
Good luck, I hope this helps.
i also tried taking off the hit part but it just said it was unidentified
takin oof hit just turns it unidentified, and i dont want to make a seperate damage script for sentry damage because if anything else hits me it wont add them to gether like it should
You don't need a second script on the sentry, you have some of it in the script you just showed me, and the other half in the character. I can't really see the entire script due to your formatting problem, and I can't edit it to due to not enough rep. But I'm sure that raycasthit isn't supposed to be there, because RaycastHit is not a part of Physics.