Shooting in a sraight line script, errors etc. Help.

Hello. I have a scripting problem that shows alot of errors. I followed the tornado twins tutorial on youtube, the one thats about how to make a turret shoot etc. Well, i took out the lookat/follow function so it would just shoot in a straigh line at the target. I am making a platformer touch game where the two cubes are suppose to shoot at eachother in a straight line. The problem is the errors i get from the scripting.

Errors: Assets/NewBehaviourScript 2.js(6,17): BCE0043: Unexpected token: =.

Assets/NewBehaviourScript 2.js(6,20): BCE0043: Unexpected token: ).

Assets/NewBehaviourScript 2.js(12,1): BCE0043: Unexpected token: var.

Assets/NewBehaviourScript 2.js(12,4): UCE0001: ';' expected. Insert a semicolon at the end.

Assets/NewBehaviourScript 2.js(12,13): BCE0043: Unexpected token: :.

Assets/NewBehaviourScript 2.js(12,14): UCE0001: ';' expected. Insert a semicolon at the end.

Assets/NewBehaviourScript 2.js(23,1): BCE0044: expecting EOF, found '}'.

I have followed the things that the errors says i have to correct, but its still not working, for example, i changed the "unexpected token =." at line 6,17. Not working, still gives me the same error.

Please help, I dont know how to fix this, and if there is a better script for shooting an object in a straighline untill it hits something, please share.

Code below:

var bullitPrefab:Transform; var savedTime; function Update () { if(LookAtTarget)= 0) {

var seconds : int = Time.time; var oddeven = (seconds % 2); if(oddeven) { Shoot(seconds); }

}

}

function Shoot(seconds)

{ if(seconds !=savedTime) {

var bullit = Instantiate(bullitPrefab, transform.Find("Spawn").transform.position, Quaternion.identity,);

bullit.rigidbody.AddForce(transform.forward * 1000);

savedTime = seconds; } }

If I were you I would use InvokeRepeating

var rateOfFire = 1.25;
var Bullet : Transform;

function Update ()
{

InvokeRepeating("Shoot", .001, rateOfFire);

//CancelInvoke("Shoot"); If you ever want to cancle the invoke.
}

function Shoot ()

{

var Bullet = Instantiate(Bullet,transform.Find("Spawn").transform.position,
             Quaternion.identity);

}

Just drag the bullet it has to instantiate in the slot in the inspector.

This is untested so if you get any errors please tell me :)

var bullitPrefab:Transform;
var savedTime;
function Update ()
{
if((LookAtTarget) == 0) <-use “==”, do not use “=”
{
var seconds : int = Time.time;
var oddeven = (seconds % 2);
if(oddeven)
{
Shoot(seconds);
}
}
}

function Shoot(seconds)
{
	if(seconds !=savedTime)
	{
		var bullit = Instantiate(bullitPrefab, transform.Find("Spawn").transform.position, Quaternion.identity);
		
		bullit.rigidbody.AddForce(transform.forward * 1000);
		
		savedTime = seconds;
	}
}

Or you can use this script

var bullitPrefab : GameObject; 
var firerate = 0.5;
var lastshot = 0.0;
var Spawn : Transform;

function Update ()
{
   if (Input.GetButton("Fire1"))
   {
   		if (Time.time > firerate + lastshot)
   		{
        	Fire();
        }
   }
}

function Fire()
{
    var bullit = Instantiate(bullitPrefab, Spawn.position, Quaternion.identity);

    bullit.rigidbody.AddForce(transform.forward * 1000);
    
    lastshot = Time.time;
}

Here try my script :

public enum FireType

{

	RAYCAST,

	PHYSIC_PROJECTILE,

}



public enum FireMode

{

	SEMI_AUTO,

	FULL_AUTO,

	BURST

}



public var FireTypeFireShoot = FireType.PHYSIC_PROJECTILE || FireType.RAYCAST;



public var FireModeFireShoot = FireMode.BURST || FireMode.FULL_AUTO || FireMode.SEMI_AUTO;





	public var gunName : String;

	public var bulletMark : GameObject;

	public var projectilePrefab : GameObject;

	//public var soldierCamera : SoldierCamera;

	public var cam : Camera;

	public var weaponTransformReference : Transform;

	

	public var hitLayer : LayerMask;

	

	public var woodParticle : GameObject;

	public var metalParticle : GameObject;

	public var concreteParticle : GameObject;

	public var sandParticle : GameObject;

	public var waterParticle : GameObject;



	//How many shots the gun can take in one second

	public var fireRate : float;

	public var useGravity : boolean;

	public var fireType : FireType;

	public var fireMode : FireMode;

	

	//Number of shoots to fire when on burst mode

	public var burstRate : int;

	

	//Range of fire in meters

	public var fireRange : float;

	

	//Speed of the projectile in m/s

	public var projectileSpeed : float;

	

	public var clipSize : int;

	public var totalClips : int;

	

	//Time to reload the weapon in seconds

	public var reloadTime : float;

	public var autoReload : boolean;

	public var currentRounds : int;

	

	public var shootVolume : float = 0.4;

	public var shootSound : AudioClip;

	private var shootSoundSource : AudioSource;

	

	public var reloadSound : AudioClip;

	private var reloadSoundSource : AudioSource;

	

	public var outOfAmmoSound : AudioClip;

	private var outOfAmmoSoundSource : AudioSource;

	

	private var reloadTimer : float;

	public var other : Camera;



	public var freeToShoot : boolean;

	



	public var reloading : boolean;

	private var lastShootTime : float;

	private var shootDelay : float;

	private var cBurst : int;

	



	public var fire : boolean;

	public var hitParticles : GameObject;

	

//	public var shotingEmitter : GunParticles;

	private var shottingParticles : Transform;

	

	public var capsuleEmitter : ParticleEmitter[];

	

	//public var shotLight : ShotLight;

	

	public var unlimited : boolean = true;

	

	private var timerToCreateDecal : float;

	

	public var pushPower : float = 3.0;

	



	

	function OnDisable()

	{

		//if(shotingEmitter != null)

		//{

			//shotingEmitter.ChangeState(false);

		//}

		

		//if(capsuleEmitter != null)

		//{//

			//for(var i : int = 0; i < capsuleEmitter.Length; i++)

			//{

				//if (capsuleEmitter *!= null)*

// capsuleEmitter*.emit = false;*

* //}*

* //}*

* //if(shotLight != null)*

* //{*

* //shotLight.enabled = false;*

* //}*

* }*

* function OnEnable()*

* {*

* cam = other.camera;*

* reloadTimer = 0.0;*

* reloading = false;*

* freeToShoot = true;*

* shootDelay = 1.0 / fireRate;*

* cBurst = burstRate;*

* totalClips–;*

* currentRounds = clipSize;*

* if(projectilePrefab != null)*

* {*

* fireType = FireType.PHYSIC_PROJECTILE;*

* }*

* //if(shotLight != null)*

* //{*

// shotLight.enabled = false;

* //}*

* //shottingParticles = null;*

* //if(shotingEmitter != null)*

* //{*

* //for(var i : int = 0; i < shotingEmitter.transform.childCount; i++)*

* //{*

* //if(shotingEmitter.transform.GetChild(i).name == “bullet_trace”)*

* //{*

* //shottingParticles = shotingEmitter.transform.GetChild(i);*

* //break;*

* //}*

* //}*

* }*

* function ShotTheTarget()*

* {*

* if(fire && !reloading)*

* {*

* if(currentRounds > 0)*

* {*

* if(Time.time > lastShootTime && freeToShoot && cBurst > 0)*

* {*

* lastShootTime = Time.time + shootDelay;*

* switch(fireMode)*

* {*

* case FireMode.SEMI_AUTO:*

* freeToShoot = false;*

* break;*

* case FireMode.BURST:*

* cBurst–;*

* break;*

* }*

* if(capsuleEmitter != null)*

* {*

* for(var i : int = 0; i < capsuleEmitter.Length; i++)*

* {*

_ capsuleEmitter*.Emit();*_

* }*

* }*

* PlayShootSound();*

* //if(shotingEmitter != null)*

* //{*

* // shotingEmitter.ChangeState(true);*

* //}*

* //if(shotLight != null)*

* //{*

* //// shotLight.enabled = true;*

* //}//*

* switch(fireType)*

* {*

* case FireType.RAYCAST:*

* //TrainingStatistics.shootsFired++;*

* //CheckRaycastHit();*

* break;*

* case FireType.PHYSIC_PROJECTILE:*

* //TrainingStatistics.grenadeFired++;*

* LaunchProjectile();*

* break;*

* }*

* currentRounds–;*

* if(currentRounds <= 0)*

* {*

* Reload();*

* }*

* }*

* }*

* else if(autoReload && freeToShoot)*

* {*

* //if(shotingEmitter != null)*

* //{*

* // shotingEmitter.ChangeState(false);*

* //}*

* //if(shotLight != null)*

* //{*

* //shotLight.enabled = false;*

* //}*

* if(!reloading)*

* {*

* Reload();*

* }*

* }*

* }*

* else*

* {*

* //if(shotingEmitter != null)*

* //{*

* // shotingEmitter.ChangeState(false);*

* //}*

* //if(shotLight != null)*

* //{*

* // shotLight.enabled = false;*

* //}/*

* }*

* }*

* function LaunchProjectile()*

* {*

* //Get the launch position (weapon related)*

_ var camRay : Ray = cam.ScreenPointToRay(new Vector3(Screen.width * 0.5, Screen.height * 0.6, 0));_

* var startPosition : Vector3;*

* if(weaponTransformReference != null)*

* {*

* startPosition = weaponTransformReference.position;*

* }*

* else*

* {*

_ startPosition = cam.ScreenToWorldPoint(new Vector3 (Screen.width * 0.5 , Screen.height * 0.5 , 0));_

* }*

* var projectile : GameObject = Instantiate(projectilePrefab, startPosition, Quaternion.identity);*

projectilePrefab.rigidbody.AddForce( camRay.direction * 3000000000 * Time.deltaTime);

//var grenadeObj : Grenade = projectile.GetComponent(“Grenade”) as Grenade;

//grenadeObj.soldierCamera = soldierCamera;

//projectile.transform.rotation = Quaternion.LookRotation(camRay.direction);

//var BulletObj : Bullet = projectile.GetComponent(“Bullet”) as Bullet;

//BulletObj.soldierCamera = soldierCamera;

//projectile.transform.rotation = Quaternion.LookRotation(camRay.direction);

//var projectileRigidbody : Rigidbody = projectile.rigidbody;

* var projectileRigidbody : Rigidbody = projectile.rigidbody;*

* if(projectile.rigidbody == null)*

* {*

* projectileRigidbody = projectile.AddComponent(“Rigidbody”); *

* }*

* projectileRigidbody.useGravity = useGravity;*

* var hit : RaycastHit;*

_ var camRay2 : Ray = cam.ScreenPointToRay(new Vector3(Screen.width * 0.5, Screen.height * 0.55, 0));_

* if(Physics.Raycast(camRay2.origin, camRay2.direction, hit, fireRange, hitLayer))*

* {*

_ projectileRigidbody.velocity = (hit.point + weaponTransformReference.position).normalized * projectileSpeed;_

* }*

* else*

* {*

_ projectileRigidbody.velocity = (cam.ScreenToWorldPoint(new Vector3(Screen.width * 0.5, Screen.height * 0.55, 40)) - weaponTransformReference.position).normalized * projectileSpeed;_

* }*

* }*

_ /*_

* function CheckRaycastHit()*

* {*

* var hit : RaycastHit;*

* var glassHit : RaycastHit;*

* var camRay : Ray;*

* var origin : Vector3;*

* var glassOrigin : Vector3;*

* var dir : Vector3;*

* var glassDir : Vector3;*

* if(weaponTransformReference == null)*

* {*

_ camRay = cam.ScreenPointToRay(new Vector3(Screen.width * 0.5, Screen.height * 0.5, 0));_

* origin = camRay.origin;*

* dir = camRay.direction;*

_ origin += dir * 0.1;_

* }*

* else*

* {*

_ camRay = cam.ScreenPointToRay(new Vector3(Screen.width * 0.5, Screen.height * 0.5, 0));_

_ origin = weaponTransformReference.position + (weaponTransformReference.right * 0.2);_

_ if(Physics.Raycast(camRay.origin + camRay.direction * 0.1, camRay.direction, hit, fireRange, hitLayer))_

* {*

* dir = (hit.point - origin).normalized;*

* if(hit.collider.tag == “glass”)*

* {*

_ glassOrigin = hit.point + dir * 0.05;_

* if(Physics.Raycast(glassOrigin, camRay.direction, glassHit, fireRange - hit.distance, hitLayer))*

* {*

* glassDir = glassHit.point - glassOrigin;*

* }*

* }*

* }*

* else*

* {*

* dir = weaponTransformReference.forward;*

* }*

* }*

* if(shottingParticles != null)*

* {*

_ shottingParticles.rotation = Quaternion.FromToRotation(Vector3.forward, (cam.ScreenToWorldPoint(new Vector3(Screen.width * 0.5, Screen.height * 0.5, cam.farClipPlane)) - weaponTransformReference.position).normalized);_

* }*

* if(Physics.Raycast(origin, dir, hit, fireRange, hitLayer))*

* {*

* hit.collider.gameObject.SendMessage(“Hit”, hit, SendMessageOptions.DontRequireReceiver);*

* GenerateGraphicStuff(hit);*

* if(hit.collider.tag == “glass”)*

* {*

* if(Physics.Raycast(glassOrigin, glassDir, glassHit, fireRange - hit.distance, hitLayer))*

* {*

* glassHit.collider.gameObject.SendMessage(“Hit”, glassHit, SendMessageOptions.DontRequireReceiver);*

* GenerateGraphicStuff(glassHit);*

* }*

* }*

* }*

* }*

_ */_

_ /*_

* function GenerateGraphicStuff(hit : RaycastHit)*

* {*

* // var hitType : HitType;*

* var body : Rigidbody = hit.collider.rigidbody;*

* if(body == null)*

* {*

* if(hit.collider.transform.parent != null)*

* {*

* body = hit.collider.transform.parent.rigidbody;*

* }*

* }*

* if(body != null)*

* {*

* if(body.gameObject.layer != 10 && !body.gameObject.name.ToLower().Contains(“door”))*

* {*

* body.isKinematic = false;*

* }*

* if(!body.isKinematic)*

* {*

* var direction : Vector3 = hit.collider.transform.position - weaponTransformReference.position;*

_ body.AddForceAtPosition(direction.normalized * pushPower, hit.point, ForceMode.Impulse);_

* }*

* }*

* var go : GameObject;*

* var delta : float = -0.02;*

* var hitUpDir : Vector3 = hit.normal;*

_ var hitPoint : Vector3 = hit.point + hit.normal * delta;_

* switch(hit.collider.tag)*

* {*

* case “wood”:*

* hitType = HitType.WOOD;*

* go = GameObject.Instantiate(woodParticle, hitPoint, Quaternion.FromToRotation(Vector3.up, hitUpDir)) as GameObject;*

* break;*

* case “metal”:*

* hitType = HitType.METAL;*

* go = GameObject.Instantiate(metalParticle, hitPoint, Quaternion.FromToRotation(Vector3.up, hitUpDir)) as GameObject;*

* break;*

* case “car”:*

* hitType = HitType.METAL;*

* go = GameObject.Instantiate(metalParticle, hitPoint, Quaternion.FromToRotation(Vector3.up, hitUpDir)) as GameObject;*

* break;*

* case “concrete”:*

* hitType = HitType.CONCRETE;*

* go = GameObject.Instantiate(concreteParticle, hitPoint, Quaternion.FromToRotation(Vector3.up, hitUpDir)) as GameObject;*

* break;*

* case “dirt”:*

* hitType = HitType.CONCRETE;*

* go = GameObject.Instantiate(sandParticle, hitPoint, Quaternion.FromToRotation(Vector3.up, hitUpDir)) as GameObject;*

* break;*

* case “sand”:*

* hitType = HitType.CONCRETE;*

* go = GameObject.Instantiate(sandParticle, hitPoint, Quaternion.FromToRotation(Vector3.up, hitUpDir)) as GameObject;*

* break;*

* case “water”:*

* go = GameObject.Instantiate(waterParticle, hitPoint, Quaternion.FromToRotation(Vector3.up, hitUpDir)) as GameObject;*

* break;*

* default:*

* return;*

* }*

* go.layer = hit.collider.gameObject.layer;*

* if(hit.collider.renderer == null) return;*

* if(timerToCreateDecal < 0.0 && hit.collider.tag != “water”)*

* {*

* go = GameObject.Instantiate(bulletMark, hit.point, Quaternion.FromToRotation(Vector3.forward, -hit.normal));*

* // var bm : BulletMarks = go.GetComponent(“BulletMarks”);*

* bm.GenerateDecal(hitType, hit.collider.gameObject);*

* timerToCreateDecal = 0.02;*

* }*

* }*

_ */_

* function Update()*

* {*

* timerToCreateDecal -= Time.deltaTime;*

* if(Input.GetButtonDown(“Fire1”) && currentRounds == 0 && !reloading && freeToShoot)*

* {*

* LaunchProjectile();*

* PlayOutOfAmmoSound();*

* }*

* if(Input.GetButtonUp(“Fire1”))*

* {*

* LaunchProjectile();*

* freeToShoot = true;*

* cBurst = burstRate;*

* }*

* HandleReloading();*

* }*

* function HandleReloading()*

* {*

* if(Input.GetKeyDown(KeyCode.R) && !reloading)*

* {*

* Reload();*

* }*

* if(reloading)*

* {*

* reloadTimer -= Time.deltaTime;*

* if(reloadTimer <= 0.0)*

* {*

* reloading = false;*

* if(!unlimited)*

* {*

* totalClips–;*

* }*

* currentRounds = clipSize;*

* }*

* }*

* }*

* function Reload()*

* {*

* if(totalClips > 0 && currentRounds < clipSize)*

* {*

* PlayReloadSound();*

* reloading = true;*

* reloadTimer = reloadTime;*

* }*

* }*

* //---------------AUDIO METHODS--------*

* function PlayOutOfAmmoSound()*

* {*

* audio.PlayOneShot(outOfAmmoSound, 1.5);*

* }*

* function PlayReloadSound()*

* {*

* audio.PlayOneShot(reloadSound, 1.5);*

* }*

* function PlayShootSound()*

* {*

* audio.PlayOneShot(shootSound);*

* }*
basically its shoots a projectile from the centre of the main camera :slight_smile: if you get confused with what you need to attach i will tell you know:
when you attach the script to the gun or what ever you want to attach it to you will see 3 main variables
Projectile Prefab
Cam
Weapon transform
Now attach what i tell you to the following 3 variables:
on the CAM attach you main camera
on the Weapon transform drag and drop the bullet spawn point
and on the projectile prefab drag the bullet it will shoot out
and that should shoot the bullet in a straight line for you i hope that i have helped you please let me know. also a reminder its a work in progress you may need to adjust the variables which contain number :slight_smile:
if you come across any errors inform me and finally the other variables dont bother with because at the moment they nothing special :slight_smile:
Hummad Nazir (MC HALO)