- Home /
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.
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!
thanks for the reply, i have already checked out Brackys tutorial but its in Javascript, as i said im looking for a C# script.
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);
}
}
Your answer
Follow this Question
Related Questions
FPS PISTOL 1 Answer
Shooting script no working correctly 1 Answer
Raycasting to avoid falling (FPS game) 2 Answers