Hello, I can see that the Refresh for my bullet isn’t fast enough.
Here is my code on my HotKey Detector:
#pragma strict
var projectile: Rigidbody;
var box : Rigidbody;
var crosshairTexture : Texture2D;
var position : Rect;
function Start()
{
position = Rect( ( Screen.width - crosshairTexture.width ) / 2, ( Screen.height -
crosshairTexture.height ) / 2, crosshairTexture.width, crosshairTexture.height );
}
function OnGUI()
{
GUI.DrawTexture( position, crosshairTexture );
}
function Update () {
if( Input.GetButtonDown( "Fire1" ) )
{
var instantiatedProjectile : Rigidbody = Instantiate(projectile, transform.position, transform.rotation );
Debug.Log("Shot");
}
if(Input.GetKeyDown(KeyCode.Z))
{
var box : Rigidbody = Instantiate(box, transform.position, transform.rotation );
}
if(Input.GetButtonDown("Fire2"))
{
var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit: RaycastHit;
// do a infinite Raycast from the camera
if (Physics.Raycast(ray, hit)) print("Hit something at "+hit.distance+" meters");
// do a Raycast up to 50 meters from the starting point
if (Physics.Raycast(ray, hit, 50)) print("Hit "+hit.transform.name);
}
if(Input.GetKeyDown(KeyCode.LeftAlt))
{
print(Camera.main.fieldOfView);
if(Camera.main.fieldOfView == 10f)
{
Camera.main.fieldOfView = 60f;
}else{
Camera.main.fieldOfView = 10f;
}
}
}
Now, my code for the bullet(The collision Detector):
#pragma strict
var layerMask : LayerMask; //make sure we aren't in this layer
var skinWidth : float = 0.1; //probably doesn't need to be changed
private var minimumExtent : float;
private var partialExtent : float;
private var sqrMinimumExtent : float;
private var previousPosition : Vector3;
private var myRigidbody : Rigidbody;
var hit : boolean = false;
//initialize values
function Awake() {
myRigidbody = rigidbody;
previousPosition = myRigidbody.position;
minimumExtent = Mathf.Min(Mathf.Min(collider.bounds.extents.x, collider.bounds.extents.y), collider.bounds.extents.z);
partialExtent = minimumExtent*(1.0 - skinWidth);
sqrMinimumExtent = minimumExtent*minimumExtent;
}
function FixedUpdate() {
if(!hit){
//have we moved more than our minimum extent?
var movementThisStep : Vector3 = myRigidbody.position - previousPosition;
var movementSqrMagnitude : float = movementThisStep.sqrMagnitude;
if (movementSqrMagnitude > sqrMinimumExtent) {
var movementMagnitude : float = Mathf.Sqrt(movementSqrMagnitude);
var hitInfo : RaycastHit;
//check for obstructions we might have missed
if (Physics.Raycast(previousPosition, movementThisStep, hitInfo, movementMagnitude, layerMask.value))
myRigidbody.position = hitInfo.point - (movementThisStep/movementMagnitude)*partialExtent;
enabled = false;
}
previousPosition = myRigidbody.position;
}
}
And the Bullet Force:
//Add this script to rigidbody GO, which will be your bullet/tracer.
var damage : int = 10;
var bulletSpeed : float = 1000;
var destroyAfter : float;
function Start(){
rigidbody.AddRelativeForce(new Vector3(0,0,bulletSpeed), ForceMode.Impulse);
//If you don't hit, destroy bullet after 3 seconds
//Destroy(gameObject, destroyAfter);
}
function OnCollisionEnter(col : Collision){
Debug.Log("Hit");
col.gameObject.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
Destroy(gameObject, 3);
}
The only problem now is that the collisions are not detected on medium-small objects like thin walls, or boxes, or AIs.
Anyone knows how to fix it?