• 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 BlackWingsCorp · Feb 27, 2013 at 07:43 PM · javascriptfpsraycasthit

Shooting system not working

Hey guys how's it going? I am trying to make a script for shooting using RayCast I wrote it so that when the RayCast hits an object tagged "enemy" and the player presses fire1 enemy will take damage but no damage is done:

 var bullet : Rigidbody;
 var speed : float = 20;
 
 function Start () {
 attackTimer = 0;
 }
 
 function Update () {
 var hit : RaycastHit;
 if(Physics.Raycast(transform.position, transform.forward, hit, 3)){
 if(hit.collider.gameObject.tag == "Enemy"){
 if(Input.GetButtonUp("Fire1")){
 Shoot();
 }
 }
 }
 }
 
 
 private function Shoot (){
 var newBullet : Rigidbody = Instantiate(bullet, transform.position, transform.rotation);
 newBullet.velocity = transform.forward * speed;
 var eh : EnemyHealth = GetComponent("EnemyHealth");
 eh.AdjustHealth(-10);
 }

Any ideas/fps tutorials?

Comment
Add comment · Show 2
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 robertbu · Feb 27, 2013 at 08:13 PM 0
Share

I'm not sure what you want to do here. GetButtonUp() will only return true for a single frame when the button is lifted. GetButtonDown() might be what you are looking for. Put a debug statement inside the raycast to make sure the raycast is working and hitting the enemy (ins$$anonymous$$d of some other collider).

 Debug.Log("Name = "+hit.collider.name); 
avatar image BlackWingsCorp · Feb 27, 2013 at 08:27 PM 0
Share

the script is attach to the pistol which is attached to the player I'm trying to cast a ray from the gun, checks if the ray hits the enemy (hit.gameObject.tag == "Enemy) then if the player hits fire1 the enemy's health drops (eh.AdjustHealth (-5)) I put a Debug.Log function but it didn't work and the console doesn't show errors. Do you think I have a better chance trying the OnCollisionEnter(col : Collision) and place it on the bullet prefab?

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Wiki

Answer by BlackWingsCorp · Feb 27, 2013 at 11:35 PM

Well the ray is casting but only 1 enemy is taking damage here's the script:

 var force : float = 10;
 var range : float = 200;
 var enemies : GameObject[];
 var enemy : GameObject;
 function Start () {
 enemies = GameObject.FindGameObjectsWithTag("Enemy");
 enemy = enemies[0];
 }
 
 function Update () {
 RayShoot();
 }
 
 
 function RayShoot (){
 var hit : RaycastHit;
 var directionRay = transform.TransformDirection(Vector3.forward);
 Debug.DrawRay(transform.position, directionRay * range, Color.blue);
 if(Physics.Raycast(transform.position, directionRay, hit, range)){
 if(hit.collider.tag == "Enemy" && Input.GetButtonUp("Fire1")){
 enemy = enemies
 var eh : EnemyHealth = enemy.GetComponent("EnemyHealth");
 eh.AdjustHealth(-10);
 }
 }
 }

I want enemy to change depending on where the player is aiming. Any idea guys?

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

9 People are following this question.

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

Related Questions

FPS controller scripts disable in debug mode [Unanswered] 0 Answers

"NullReferenceException: Object reference not set to an instance of an object" When Raycast hits and tries select a designated object 2 Answers

Modern Warfare Styled Zoom? (Javascript issue) 0 Answers

CS0116 "A namespace can only contain type and namespace declarations" 1 Answer

Whenever I shoot a bullet from my gun, the bullet disapears. 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges