i have a character that shoots “lazers” but the only point one direction … so if im facing to the left the lazer will shoot sidways. how can i fix this?
this is my script
var speed : float = 10.0;
var rotateSpeed : float = 3.0;
var bulletPrefab:Transform;
function Update ()
{
var controller : CharacterController = GetComponent(CharacterController);
// Rotate around y - axis
transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
// Move forward / backward
var forward : Vector3 = transform.TransformDirection(Vector3.forward);
var curSpeed : float = speed * Input.GetAxis ("Vertical");
controller.SimpleMove(forward * curSpeed);
if(Input.GetButtonDown("Jump"))
{
var bullet = Instantiate(bulletPrefab,
GameObject.Find("spawnPoint").transform.position,
Quaternion.identity);
bullet.rigidbody.AddForce(transform.forward * 2000);
}
}
@script RequireComponent(CharacterController)
var bullet = Instantiate(bulletPrefab, GameObject.Find(“spawnPoint”).transform.position, Quaternion.identity);
Change the last part to take the rotation of the object you want to keep the rotation of like so.
var bullet = Instantiate(bulletPrefab, GameObject.Find(“spawnPoint”).transform.position, transform.rotation);
---- Sorry if this is a re-post, my other answer didn’t show up >.>; ----
Using a raycast is basically drawing a line in a direction, read more here:
But basically for your lazer you want to use a raycast to shoot and not a rigidbody.
Here’s a basic script that should work:
var shotSound : AudioClip; // Shot sound
var bloodPrefab : GameObject; // Blood for enemy
var sparksPrefab: GameObject; // Sparks for groun
var muzzleFlash : GameObject; //MuzzleFlash game object
var shootEnabled: boolean = true; // allows disabling the shots
var ChangeWep : boolean = true;
var RayCastDist : float = 50; //Max distance of raycast
var Bullets = 9;
var MaxBullets = 9;
var Clip = 9000;
var force = 50;
var Recoil = 10;
var Accuracy = 50;
var shotInterval = 1.0; //Interval between shots
function Start () {
muzzleFlash.renderer.enabled = false;
shootEnabled = true; // allows disabling the shots
ChangeWep = true;
}
function Update () {
var forward : Vector3 = transform.TransformDirection(Vector3.forward) * 10;
if (Input.GetButtonDown("Fire1")){
if (shootEnabled == true){
if (Bullets > 0){
if (Clip > 0){
Shoot();
Bullets -= 1;
}
}
}
}
if (Input.GetButtonUp("Fire1")){
ChangeWep = true;
}
if (Bullets == 0){
if (Clip >= 1){
Clip -= 1;
Bullets = MaxBullets;
}
if (Clip == 0){
Bullets = 0;
}
}
}
function Shoot(){
var cam : Transform = Camera.main.transform;
var ray = new Ray(cam.position, cam.forward);
var hit : RaycastHit;
var trf = transform; // a little optimization
var hitRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
audio.PlayOneShot(shotSound); // play the shot sound...
MuzzleFlashOn();
if(Physics.Raycast (ray, hit, RayCastDist)){
var rot = Quaternion.FromToRotation(Vector3.up, hit.normal);
if(hit.collider.gameObject.tag == "Box"){ //Checks hit collider's tag
Debug.Log ("Hit A Box!");
if (sparksPrefab) Instantiate(sparksPrefab, hit.point, rot);
hit.rigidbody.AddForceAtPosition(force * transform.forward , hit.point);
hit.collider.SendMessageUpwards("ApplyDamage", 5.0);
}
if(hit.collider.gameObject.tag == "Enemy"){
Debug.Log ("Hit A Enemy!");
hit.collider.gameObject.BroadcastMessage("LoseHealth", force * 2 / 4);
if (sparksPrefab) Instantiate(sparksPrefab, hit.point, rot);
} else {
if (sparksPrefab) Instantiate(sparksPrefab, hit.point, rot);
Debug.Log ("Hit Other!");
}
ChangeWep = false;
shootEnabled = false;
yield WaitForSeconds(shotInterval);
shootEnabled = true;
ChangeWep = true;
}
}
function MuzzleFlashOn()
{
var muzzleFlashLength = 0.1;
muzzleFlash.renderer.enabled = true;
yield WaitForSeconds(muzzleFlashLength);
muzzleFlash.renderer.enabled = false;
}