- Home /
Error CS1501, Projectiles spawning.
My bullet projectiles are being fired from the turret, and ending up spawning ontop of another, furthermore they don't move. I've tried adding Physics.IgnoreCollision(bullet.collider);
However that generates the error; error CS1501: No overload for method IgnoreCollision' takes1' arguments
void Update(){
TargetEnemy(); // update the selected target and look at it
if (selectedTarget){ // if there's any target in the range...
transform.LookAt(selectedTarget); // aim at it
if (Time.time >= shootTime){ // if it's time to shoot...
Rigidbody bullet = (Rigidbody)Instantiate(bulletPrefab, bulletSpawn.position, bulletSpawn.rotation);
}
Answer by Chronos-L · Mar 22, 2013 at 01:45 AM
Physics.IgnoreCollision()
takes 2 or 3 arguments.
Physics.IgnoreCollision( colliderA, colliderB )
: colliderA and colliderB will not collidePhysics.IgnoreCollision( colliderA, colliderB, false )
: colliderA and colliderB will collide
Physics.IgnoreCollision(bullet.collider)
is incorrect, you need to define what collider the bullet will ignore.
I see, so something like this?
Physics.IgnoreCollision( bullet.collider, collider );
that doesn't give me the error anymore, but the bullets are moving just spawning ontop of one another. I take it that the above code is wrong.
You need to apply force to your bullet, after you instantiate it.
hmmm i am, i'm down with a first person camera, and i can see the bullets following the position of the targets, but they're not moving at all. Used Rigidbody.AddForce() ins$$anonymous$$d of bullet.AddForce but to no avail.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class TurretScript : $$anonymous$$onoBehaviour {
public float shotInterval = 0.2f; // interval between shots
public Rigidbody bulletPrefab; // drag the bullet prefab here
private float shootTime = 5.0f;
private List<Transform> targets;
private Transform selectedTarget;
private Transform myTransform;
private Transform bulletSpawn;
private int speed = 10;
void Start(){
targets = new List<Transform>();
selectedTarget = null;
myTransform = transform;
bulletSpawn = transform.Find ("bulletSpawn"); // only works if bulletSpawn is a turret child!
}
void OnTriggerEnter(Collider other){
if (other.tag == "Enemy"){ // only enemies are added to the target list!
targets.Add(other.transform);
}
}
void OnTriggerExit(Collider other){
if (other.tag == "Enemy"){
targets.Remove(other.transform);
}
}
void TargetEnemy(){
if (selectedTarget == null){ // if target destroyed or not selected yet...
SortTargetsByDistance(); // select the closest one
if (targets.Count > 0) selectedTarget = targets[0];
}
}
void SortTargetsByDistance(){
targets.Sort(delegate(Transform t1, Transform t2){
return Vector3.Distance(t1.position, myTransform.position).CompareTo(Vector3.Distance(t2.position, myTransform.position));
});
}
void Update(){
TargetEnemy(); // update the selected target and look at it
if (selectedTarget){ // if there's any target in the range...
transform.LookAt(selectedTarget); // aim at it
if (Time.time >= shootTime){ // if it's time to shoot...
Rigidbody bullet = (Rigidbody)Instantiate(bulletPrefab, bulletSpawn.position, bulletSpawn.rotation);
Physics.IgnoreCollision( bullet.collider, collider );
bullet.AddForce(transform.forward*speed); // shoot in the target direction
shootTime = Time.time + shotInterval; // set time for next shot
}
}
}
}
Like a actual bullet, you need to propel it with a huge amount of force in a short amount of time, like a controlled explosion in a actual gun.
You controlled the type of force applied to the bullet by using Force$$anonymous$$ode. By default, the AddForce()
use Force$$anonymous$$ode.Force
, that is good for a continuous force, not a sudden force ( or impulse force ).
Try: bullet.AddForce( bullet.forward * impluseForce, Force$$anonymous$$ode.Impulse );
You need to set the impulseForce
to a reasonable amount, just play with the value.
I am not sure it bullet.forward
if equivalent to transform.forward
, but you can use whatever that works.
Disregard the above problem. I have messed about with the code some more and it works! =) can't thankyou enough.
I'm pretty sure that also leads me onto a whole new problem =p enemies using colliders (which i can't take of due to the movement system i'm using) messing up the bullet destroy code.
But i'll have a think of that myself, thanks again mate
Your answer
Follow this Question
Related Questions
Collision detection with overlap sphere 1 Answer
Distribute terrain in zones 3 Answers
Why are my projectiles facing the wrong way? 1 Answer
Multiple Cars not working 1 Answer
Produce Collision Though Code 0 Answers