- Home /
raycast spawn problem
Hello, I'm working on a game project in school with a couple of classmates. I'm making a gun script for our FPS shooter and I have encountered a problem. The problem is when I'm shooting my raycast casting points get's further and further down for every shot and I don't know why. The raypoint don't move (The object where I start my raycast from). Any one that has any idea what the problem may be? I have pasted my code in the post.
Thanks in advance.
@HideInInspector
var cameraObject : GameObject;
@HideInInspector
var targetXRotation : float;
@HideInInspector
var targetYRotation : float;
@HideInInspector
var targetXRotationV : float;
@HideInInspector
var targetYRotationV : float;
var rotateSpeed : float = 0.3;
var holdHeight : float = -0.5;
var holdSide : float = 0.5;
var racioHipHold : float = 1;
var hipToAimSpeed : float = 0.1;
@HideInInspector
var racioHipHoldV : float;
var aimRacio : float = 0.2;
var zoomAngle : float = 40;
var fireSpeed : float = 1;
@HideInInspector
var waitTilNextFire : float = 0;
var recoilAmount : float = 0.2;
var recoilRecoverTime : float = 0.2;
@HideInInspector
var currentRecoilZPos : float;
@HideInInspector
var currentRecoilZPosV : float;
var floatInFrontOfWall : float = 000.1;
var gunbobAmountX : float = 0.5;
var gunbobAmountY : float = 0.5;
var currentGunbobX : float;
var currentGunbobY : float;
static var AMMO : int = 50;
var clickSound : AudioClip;
var gunSound : AudioClip;
var decalHitWall : GameObject;
var muzzleFlash : GameObject;
var raySpawn : GameObject;
var range : float = 300;
function Start() {
ammo = 50;
}
function LateUpdate () {
currentGunbobX = Mathf.Sin(cameraObject.GetComponent(MouseLookScript).headbobStepCounter) * gunbobAmountX * racioHipHold;
currentGunbobY = Mathf.Cos(cameraObject.GetComponent(MouseLookScript).headbobStepCounter * 2) * gunbobAmountY * -1 * racioHipHold;
if(AMMO > 0) {
if (Input.GetKeyDown(KeyCode.Mouse0)) {
if (waitTilNextFire <= 0) {
RayShoot();
PlayShootAudio();
var clone : GameObject;
clone = Instantiate(muzzleFlash, raySpawn.transform.position, transform.rotation);
currentRecoilZPos -= recoilAmount;
waitTilNextFire = 1;
}
}
}
if (AMMO <=0 ) {
if (Input.GetKey(KeyCode.Mouse0)) {
if (waitTilNextFire <= 0) {
PlayClickSound();
waitTilNextFire = 1;
}
}
}
waitTilNextFire -= Time.deltaTime * fireSpeed;
currentRecoilZPos = Mathf.SmoothDamp( currentRecoilZPos, 0, currentRecoilZPosV, recoilRecoverTime);
cameraObject.GetComponent(MouseLookScript).currentTargetCameraAngle = zoomAngle;
if (Input.GetKey(KeyCode.Mouse1)) {
cameraObject.GetComponent(MouseLookScript).currentAimRacio = aimRacio;
racioHipHold = Mathf.SmoothDamp(racioHipHold, 0, racioHipHoldV, hipToAimSpeed);
}
if (Input.GetKey(KeyCode.Mouse1) == false) {
cameraObject.GetComponent(MouseLookScript).currentAimRacio = 1;
racioHipHold = Mathf.SmoothDamp(racioHipHold, 1, racioHipHoldV, hipToAimSpeed);
}
transform.position = cameraObject.transform.position + (Quaternion.Euler(0,targetYRotation,0) * Vector3(holdSide * racioHipHold + currentGunbobX, holdHeight * racioHipHold + currentGunbobY, 0) + Quaternion.Euler(targetXRotation, targetYRotation, 0) * Vector3(0,0,currentRecoilZPos));
targetXRotation = Mathf.SmoothDamp( targetXRotation, cameraObject.GetComponent(MouseLookScript).xRotation, targetXRotationV, rotateSpeed);
targetYRotation = Mathf.SmoothDamp( targetYRotation, cameraObject.GetComponent(MouseLookScript).yRotation, targetYRotationV, rotateSpeed);
transform.rotation = Quaternion.Euler(targetXRotation, targetYRotation, 0);
}
function RayShoot() {
AMMO -= 1;
var hit:RaycastHit;
var directionRay = raySpawn.transform.TransformDirection(Vector3.forward);
Debug.DrawRay(raySpawn.transform.position, directionRay * range, Color.blue);
if(Physics.Raycast(raySpawn.transform.position, directionRay, hit, range)) {
if(decalHitWall && hit.transform.tag == "Level Parts") {
Instantiate(decalHitWall, hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal));
}
}
}
function PlayShootAudio() {
audio.PlayOneShot(gunSound);
}
function PlayClickSound() {
audio.PlayOneShot(clickSound);
}
Comment
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Raycasr in my fps? 1 Answer
Straight Raycast Shooting 1 Answer
One animation for two objects 0 Answers