Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Mansmart10 · Oct 27, 2016 at 10:15 PM · raycastphysics2draycasthit2drange

Racasthit2d range not working

When I shoot my gun from a far distance the raycast doesn't detect the other player. But when the player is touching the other player it works. here is my script.

using UnityEngine; using UnityEngine.Networking; using UnityEngine.UI;

public class ShootingScript1 : NetworkBehaviour { //public ParticleSystem _muzzleFlash; //public AudioSource _gunAudio; public GameObject _impactPrefab; public Transform ray;

 ParticleSystem _impactEffect;

 public int gunDamage = 25;
 public float range;
 public float fireRate;

 public LayerMask whatToHit;

 void Start()
 {
     _impactEffect = Instantiate(_impactPrefab).GetComponent<ParticleSystem>();
 }

 void Update()
 {
     if (Input.GetButtonDown("Fire1"))
     {
         //_muzzleFlash.Stop();
         //_muzzleFlash.Play();
         //_gunAudio.Stop();
         //_gunAudio.Play();

         RaycastHit2D hit = Physics2D.Raycast(ray.position, Vector2.zero, range, whatToHit);

         if (hit.transform.gameObject.tag == "Player")
         {
             //Health health = hit.transform.GetComponent<Health>();
             _impactEffect.transform.position = hit.point;
             _impactEffect.Stop();
             _impactEffect.Play();
             Debug.Log("Hit");
             //Destroy(gameObject);
             CmdHitPlayer(hit.transform.gameObject);
         }
     }
 }

 [Command]
 void CmdHitPlayer(GameObject hit)
 {
     hit.GetComponent<NetworkedPlayerScript>().RpcResolveHit();
 }

}

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 TreyH · Oct 31, 2016 at 01:50 AM 1
Share

Have you tried drawing gizmos to see where that raycast is actually going? Also, that's a Physics call running in Update(). For more reliable results, those sorts of things typically go in FixedUpdate().

avatar image Mansmart10 TreyH · Oct 31, 2016 at 01:54 AM 0
Share

No how would I do that.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by TreyH · Nov 01, 2016 at 04:39 PM

Also, your raycast direction is set to Vector2.zero. Your ray will follow this path:

Start --> Start + Direction * magnitude.

In your case, the direction is zero, so your ray will only hit things that come within your object itself -- this might explain your issue. For an example, attach this to something and make sure Gizmos are enabled in your editor. Rotate it around in the inspector to see what happens:

 using UnityEngine;
 using System.Collections;
 
 public class GizmoTest : MonoBehaviour {
 
     // Draw rays, primatives, and whatnot to test your orientations.
     void OnDrawGizmos () {
 
         // Create the ray you want to check
         float rayLength = 10f;
 
         // Then draw it
         Gizmos.DrawRay(this.transform.position, this.transform.up * rayLength );
 
     }
 }
 
Comment
Add comment · Show 1 · 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 Mansmart10 · Nov 01, 2016 at 08:59 PM 0
Share

ok i will try it out.

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

77 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 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 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 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

Is it possible to add Angle to Raycast2D? 1 Answer

Raycast2D for top-down game not working as expected 1 Answer

2D How to reflect a raycast with a line renderer 0 Answers

Checking for Raycast distances not working as expected. 0 Answers

How can I detect which game object was clicked? 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