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 Crumpet · Mar 17, 2014 at 07:25 AM · shootingraycasting

Raycasting issues raycast angled and not hitting

I created a script with the help of the users on this post http://answers.unity3d.com/questions/663862/c-weapon-shooting-script-using-raycasting-help.html I thought everything was working but when I moved the object a bit it would not hit. I decided to make the raycast show up in scene view and it seems to be firing on a 45 degree angle to the right of the gun. As shown in the image below. I also set the layer on the gun to "Ignore Raycast". alt text

 using UnityEngine;
 using System.Collections;
 
 public class NewBehaviourScript : MonoBehaviour
 {
     public bool outofammo;
     public int bullets = 7;
     public GameObject deagle; //this is my gun
     public float time = 1;
     public float firerate = 1;
     public bool fired = false;
     public bool Cannotfire = false;
     public bool timedown;
     Transform Effect;
     public float TakeDamage = 100;
     RaycastHit hit;
     private Vector3 lineTransform;
     private Vector3 startTransform;
     public int Damage = 25;
     public float Distance;
     public float MaxDistance = 2;
     
     void Start()
     {
         lineTransform = transform.position;
         startTransform = transform.position;
     }
     
     void Update ()
     {
         if (Input.GetKeyDown(KeyCode.R) && bullets < 7)
         {
             deagle.animation.Play ("Reload");
             bullets = 7;
         }
         
         if (Input.GetButtonDown("Fire1"))
         {
             timedown = true;
             
             if (bullets == 0)
             {
                 bullets = 7;
                 outofammo = true;
                 timedown = false;
             }
             else 
                 outofammo = false;
             
             if (outofammo == true)
             {
                 deagle.animation.Play ("Reload");
             }
             
         }
         
         if (timedown == true)
         {
             time = (time - (firerate *Time.deltaTime));
             if (time <= 0)
             {
                 bullets = bullets -1;
                 fired = true;
                 {
                     RaycastHit hit;
                     if(Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), out hit))
                     {
                         Distance = hit.distance;
                         if (Distance < MaxDistance)
                             hit.transform.SendMessage ("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);
                         lineTransform = hit.point;
                         Debug.DrawRay (startTransform, lineTransform, Color.red);
                     }
                     timedown = false;
                     time = 1f;
                     fired = false;
                 }
             }
             
             if (fired == true)
                 time = 1;
         }
     }
 }


Not only that but I don't think the raycast is actually registering the hit either because I have this script attached to the object that it is hitting and nothing seems to be happening.

 using UnityEngine;
 using System.Collections;
 
 public class TakeDamage2 : MonoBehaviour 
 {
     public float health = 100;
     public GameObject Enemy;
 
     void ApplyDamage(int damage)
     {
         health -= damage;
     }
 }


Sorry for having to post about the same issue I thought it was all working. :/

raycastissue.png (11.9 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by robertbu · Mar 17, 2014 at 07:30 AM

The issue with the Debug.DrawRay() is that you are passing it two points. Debug.DrawRay() takes a point and a direction (like you use in your Raycast()). So you can do one of two things. You can change your DrawRay() to a DrawLine():

  Debug.DrawLine(transform.position, hit.point);

Or you can rework your DrawRay():

  Debug.DrawRay (transform.position, transform.forward * 10.0f, Color.red);

Comment
Add comment · Show 4 · 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 Crumpet · Mar 17, 2014 at 07:40 AM 0
Share

Ah I see why now, the gun was originally facing downwards which I had to fix by rotating it. So my "Vector3.forward" is actually downwards so it was shooting the ground. Thank you so much this confused me so much, I checked and it actually does register the hit now. The line was just wrong. Thank you so much this confused me so much. To think it was as simple as a rotation.

avatar image Crumpet · Mar 17, 2014 at 07:42 AM 0
Share

Uhm I don't see how I can even mark this as solved?

avatar image robertbu · Mar 17, 2014 at 08:10 AM 0
Share

I've converted it to an answer. You can click on the checkmark to mark it solved. I originally did not put it in as an answer because it did not address this part of the problem:

but when I moved the object a bit it would not hit.

avatar image Crumpet · Mar 17, 2014 at 08:13 AM 0
Share

Yeah okay, thanks anyway.

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

20 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

Related Questions

Raycasting Collider Problem 0 Answers

How to Fix a Guns Firing Script so it Doesn't Constantly Fire 3 Answers

How do I make bullet tracers show when I shoot in the air? 1 Answer

Casting ray forward from transform position 1 Answer

Shooting and damage with raycast doesnt work. 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