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 mukheion · Dec 27, 2013 at 09:47 PM · collisionraycastsniper

raycast hit point is wrong

raycast hit wrong point . this script give to sniper weapon. raycast does not hit object(add animation) please help

 #pragma strict
 
 function Start () {
  
 }
 
 
 function Update () {
     var cam : Transform = Camera.main.transform;
     var ray = new Ray(cam.position, cam.forward);
     var hit : RaycastHit;
     if(Physics.Raycast (ray, hit, 500)) {
 
       if (Input.GetMouseButtonDown(0) && hit.collider.gameObject.tag == "Enemy") {
    
           print ("enemy!");
           Destroy(hit.collider.gameObject);
        
       }
     }
 }
 
Comment
Add comment · Show 5
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 TowerOfBricks · Dec 27, 2013 at 09:48 PM 0
Share

Try to give a bit more information. That raycast should hit whatever collider that is in its way. $$anonymous$$ake sure that there is actually a collider there for it to hit. Try debugging the ray using Debug.DrawRay ( ray.origin, ray.direction*500 );

avatar image nastasache · Dec 28, 2013 at 10:52 AM 0
Share

I have a similar problem (always) with models using Character Controller collider; just I can't shoot enemies on the head because raycast looks founding collider only for 70-80% of character height (up to the neck) - it's what Debug.DrawLine show. If your problem is the same, I think looks like a Unity bug. The only solution I found was to attach a separated Capsule Collider to the head. Having a snipper weapon, good think is attaching that second collider to the head you can count headshots.

avatar image Benproductions1 · Dec 28, 2013 at 12:14 PM 1
Share

@nastasache: If you have a similar problem, post another question!. Don't get comment on one that is similar!

The likelihood of it being a "Unity Bug" is extremely low and you're probably just doing something wrong.

avatar image mukheion · Dec 28, 2013 at 11:28 PM 0
Share

thanks. i tryed Debug.DrawRay and attach a separated Capsule Collider. but I cant fix this problem this work like this → http://youtu.be/9Csr7azUeRA and i changed code like this ↓

 #pragma strict
  
 function Start () {
  
 }
  
  
 function Update () {
 var cam : Transform = Camera.main.transform;
 var ray : Ray = camera.ScreenPointToRay (new Vector3(Screen.width / 2, Screen.height / 2, 0));
 var hit : RaycastHit; 
 Debug.DrawRay ( ray.origin, ray.direction*500 , Color.red);
 if(Physics.Raycast (ray, hit, 500)) {
  
 if (Input.Get$$anonymous$$ouseButtonDown(1) && hit.collider.gameObject.tag == "ball") {
  
 print ("enemy!");
 Destroy(hit.collider.gameObject);
  
 }
 }
 }

i cant resolve this problem....

avatar image robertbu · Dec 29, 2013 at 08:26 PM 0
Share

Did you put the debug line in I indicated in my answer to see what your Raycast is hitting?

 Debug.Log(hit.collider.name + ", " + hit.collider.tag);

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by robertbu · Dec 28, 2013 at 11:28 PM

I see nothing wrong with your code that would account for the described behavior. About 50% of the time this issue comes across the list either 1) the name/tag of the game object is not what is expected, or 2) the ray is hitting an unexpected game object. In addition, it is a bit more efficient to place the GetMouseButtonDown() outside of the Raycast(). That way, you don't do a Raycast() each frame. Try this code. I've added a Debug.Log() for the name and the tag.

 #pragma strict
 
 function Update () {
     var cam : Transform = Camera.main.transform;
     var ray = new Ray(cam.position, cam.forward);
     var hit : RaycastHit;
 
     if (Input.GetMouseButtonDown(0)) {
         if(Physics.Raycast (ray, hit, 500)) {
             Debug.Log(hit.collider.name + ", " + hit.collider.tag);
             if (hit.collider.tag == "Enemy") {
                   print ("enemy!");
                   Destroy(hit.collider.gameObject);
             }
         }
     }

}

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

Answer by nastasache · Dec 29, 2013 at 04:03 PM

Try getting a list of all colliders on front of the camera. Then exclude some unwanted colliders, if any (like your gun collider, for example).

Use different tags for player and head.

The code (in C#, sorry for non-conversion to JS) may look like this:

Raycast.cs:

 using UnityEngine;
 using System.Collections;
 
 public class Raycast : MonoBehaviour {
 
     void Start () {
     
     }
     
     void Update () {
         
         if( Input.GetButtonDown( "Fire1" )) { 
             
             Vector3 start = Camera.main.transform.position;
             Vector3 direction = Camera.main.transform.forward; 
             
             RaycastHit[] hits = Physics.RaycastAll (start, direction, float.PositiveInfinity);
             
             Debug.DrawRay ( start, direction * 10 , Color.red, 10);
             
             foreach(RaycastHit hit in hits){
                 
                 Debug.Log("hit.collider="+hit.collider+" hit.point="+hit.point+" tag="+hit.collider.tag);
                 
                 if(hit.collider.tag == "Player" || hit.collider.tag == "PlayerHead") { 
                     
                     Debug.Log ("Shoot "+hit.collider.tag);
             
                 } 
                 
             }
                 
         } 
     }
     
 }

I did a small test project with this issue, you can try it from here . Look for "HeadCollider" object attached to the head of default Unity 3rd Person Controller.

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

Answer by mukheion · Dec 29, 2013 at 07:49 PM

thankyou. result of trying your code and debug. There are problems with object(add animation)probably. I changed different object.  It worked i resolve problem. thanks

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.

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

raycast hit point is wrong 1 Answer

making ray ignor certain collider 1 Answer

Obstacle avoidance 1 Answer

Destroy Turret with machine Gun 0 Answers

RayCast Collision between 2 game objects (of the same prefab) 1 Answer


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