Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Scarfo76 · Sep 08, 2012 at 02:09 AM · weapons

how to make something shoot the angle i am facing

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)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by farrisarts · Sep 08, 2012 at 03:02 AM

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 >.>; ----

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Scarfo76 · Sep 08, 2012 at 04:49 AM 0
Share

hey thanks that worked! but now i need a better idea on a "lazer" have anything i can try? i just have the prefab as a sphere that i sized down and when i shoot it it flips everywhere

and do you know how i can script so i can hold right click and look around?

avatar image chrisall76 · Sep 08, 2012 at 06:09 AM 0
Share

Probably want to use a raycast and not a rigidbody.

avatar image Scarfo76 · Sep 08, 2012 at 06:19 AM 0
Share

whats raycast?

avatar image
0

Answer by chrisall76 · Sep 08, 2012 at 06:58 AM

Using a raycast is basically drawing a line in a direction, read more here: http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html

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;
 }
 
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Scarfo76 · Sep 08, 2012 at 07:49 AM 0
Share

i tried it an when i shoot they stay in place

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Projectile instantiation positioning is off. 3 Answers

Error in switching weapons (activate/deactivate objects) 1 Answer

Player weapon swap 2 Answers

how to i play another animation when the first one is finished 1 Answer

Weapon / Viewmodel clipping shader methods 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges