• 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 Voridian · Feb 23, 2014 at 03:05 AM · c#raycastfpsshoot

Where can i find an easy raycast shooting tutorial for C#?

Hey all, i know this question has popped up a lot but is there a good tutorial for how to make a raycast shooting script in C#? not javascript, all the good guides i found were for java. If there is actually no such thing as raycast shooting for C#, are there any other ways asside from the prefab bulletspawn method?

also on another note, if anybody has done animation in blender could you take a look at This Question I asked it yesterday and no answers.

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
Best Answer

Answer by Truger · Feb 23, 2014 at 05:34 AM

Brackys has a shooting tutorial http://youtu.be/mpxim8YbsMk

You should also check out -unity cookie http://cgcookie.com/unity/

-bergzerg arcade http://www.youtube.com/user/BurgZergArcade/videos

for more unity tutorials for different things

 using UnityEngine;
 using System.Collections;
 
 public class RaycastShooting : MonoBehaviour {
 
 
 Transform Effect;
 float TheDammage = 100;
 object particleClone;
 RaycastHit hit;
 
 void Update (){
     
     
     Vector3 fwd = transform.TransformDirection(Vector3.forward); //the direction the player is facing
     
     if (Input.GetMouseButtonDown(0)) //if the left mouse button is pressed do ....
     {
 
             if (Physics.Raycast(transform.position, fwd, 10)) //if a game object is in front of player but within 10 units of it trigger a hit
             {
 
             //particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
             //Destroy(particleClone.gameObject, 2);
             hit.transform.SendMessage("ApplyDammage", TheDammage, SendMessageOptions.DontRequireReceiver); //Call the method Apply Damage in the gameobject that is hit
             }
     }
     
 }
 }

sorry about that I downloaded and converted the script to c# but because i didn't watch the video myself i have no idea what particleclone.gameobject is referring to so i commented that part out otherwise this should at least help you get started!

Comment
Add comment · Show 2 · 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 Voridian · Feb 23, 2014 at 06:41 AM 0
Share

thanks for the reply, i have already checked out Brackys tutorial but its in Javascript, as i said im looking for a C# script.

avatar image Truger · Feb 23, 2014 at 04:21 PM 0
Share

i commented the code to make it easier to understand.

avatar image
0

Answer by sunil1 · Mar 21, 2017 at 06:02 AM

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

//using UnityEditor.Animations;

[RequireComponent(typeof(AudioSource))] public class raycastShooting : MonoBehaviour { public LayerMask layerMask; public float range = 1000; public float force = 500; public GameObject Blood; public GameObject untagged; private float triggerTime = 0.05f; public GameObject mainCamera; public int damage = 50; public Animation Anim; public bool firing = true; public Text bullets; public int bulletshit = 30; public AudioSource au; int gung;

 // Use this for initialization
 void Start()
 {
     bullets.text = " Bullet =" + bulletshit.ToString();
     Anim = GetComponent<Animation>();
 }

 // Update is called once per frame
 public void Update1()
 {
     if (firing)
     {
         if (bulletshit > 0)
         {

             Vector3 direction = mainCamera.transform.TransformDirection(new Vector3(Random.Range(-0.05f, 0.05f) * triggerTime / 3, Random.Range(-0.05f, 0.05f) * triggerTime / 3, 1));
             RaycastHit hit;
             Vector3 position = transform.parent.position;

             if (Physics.Raycast(position, direction, out hit, range, layerMask.value))
             {
                 Anim.Play("Fire");
                 au.Play();
                 Vector3 contact = hit.point;
                 Quaternion rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);

                 if (hit.rigidbody)
                 {

                     hit.rigidbody.AddForceAtPosition(force * direction, hit.point);
                 }

                 if (hit.transform.tag == "Untagged")
                 {

                     GameObject default1 = Instantiate(untagged, contact, rotation) as GameObject;
                     hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
                     default1.transform.parent = hit.transform;
                 }


                 if (hit.transform.tag == "Enemy")
                 {

                     hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);

                     Instantiate(Blood, contact, rotation);
                     if (Physics.Raycast(position, direction, out hit, range, layerMask.value))
                     {
                         if (hit.rigidbody)
                         {
                             hit.rigidbody.AddForceAtPosition(force * direction, hit.point);
                         }
                     }
                 }
             }
             bullets.text = " Bullet =" + bulletshit.ToString();
             bulletshit--;
             
         }

         if (bulletshit == 0)
         {
             firing = false;
         }
     }




  

}

 public void gun()
 {
     print("h");
     bulletshit = 30;
     firing = true;
     Anim.Play("Reload",PlayMode.StopAll);
 }
   

}

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

FPS PISTOL 1 Answer

Raycast Positioning 2 Answers

Raycast stops when false, even in update() 1 Answer

C# Raycast 2D GameObject follow Mouse 1 Answer

Finding Distance between Angles and Points 2 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