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
1
Question by StudentGameDev · Oct 16, 2012 at 02:03 PM · raycastlayerslayermask

Raycast not hitting on target?

Hello guys,

I'm writing part of the player's ability to do combat, melee. But I don't think the Raycast hits, nor does the AttackSequence enter the Hit function.

 using System;
 using System.Collections;
 using System.Linq;
 using System.Text;
 using UnityEngine;
 
 class PlayerAttack : MonoBehaviour
 {
     private float lastAttack;
     public float attackCooldown = 1.5f;
     private PlayerController controller;
     public LayerMask hitLayers = -1;
 
     public void Awake()
     {
         controller = GetComponent<PlayerController>();
     }
 
     public void Update()
     {
         if (Input.GetButtonDown("Fire1"))
         {
             if (Time.time > lastAttack + attackCooldown)
             {
                 lastAttack = Time.time;
 
                 StartCoroutine(AttackSequence());
             }
         }
     }
 
     private IEnumerator AttackSequence()
     {
         animation.Play("attack");
         float animationDone = Time.time + animation["attack"].length;
 
         //controller.isControllable = false;
 
         float next = Time.time + 0.25f;
         while (Time.time < next) yield return null;
 
         Hit();
 
         while (Time.time < animationDone) yield return null;
 
         //controller.isControllable = true;
         animation.Play("idle");
     }
 
     private void Hit()
     {
         RaycastHit hit;
         
         if (Physics.Raycast(transform.position + Vector3.up, transform.forward, out hit, 3, hitLayers.value))
         {
             Hitable target = hit.collider.GetComponent<Hitable>();
             if (target)
             {
                 target.Hit(20);
                 Debug.Log("Player has attacked target.");
             }
         }
     }
 }
 
 public class Hitable : MonoBehaviour
 {
     float hitpoints = 100;
     public void Hit(float damage)
     {
         hitpoints -= damage;
         if (hitpoints <= 0)
             Debug.Log("Enemy dead.");
     }
 
 }

Thanks in advance,

Tolga.

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

Answer by aldonaletto · Oct 16, 2012 at 02:53 PM

The code seems ok, but many things may go wrong:

1- You're casting a forward ray from transform.position + Vector3.up; if the character is 2 units tall, the raycast will start from to top of its head, possibly passing over the enemy (unless the enemy is a big guy). Try to start the ray at transform.position instead.

2- Since you're using a mask, be sure that it includes the layer where the enemy is. This mask is initialized to -1 in your code (all layers), but may have been changed in the Inspector, and this bastard has the memory of an elephant! I would simply remove the mask parameter until everything is working 100%.

3- The script Hitable may not be attached to the enemy, or may be attached to a children or parent of the hit collider - in any of these cases, GetComponent(Hitable) would return false and the damage would not be applied. Place a debug line in the if:

   if (target){
       target.Hit(20); // enemy hit
       Debug.Log("Player has attacked target.");
   } else {
       Debug.Log("No Hitable"); // no Hitable script found
   }
 
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 StudentGameDev · Oct 16, 2012 at 03:19 PM 0
Share

Thanks for the advice, and help. :)

I tried checking it with the debug logs, and then tried changing the line just above if (target).

I figured out why it didn't register any hits on the object I was testing it on. I adjusted this line:

Hitable target = hit.collider.GetComponent();

to this:

Hitable target = hit.collider.gameObject.transform.GetComponent();

Now all hits and damages are working!

Regards,

Tolga.

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

10 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

Related Questions

How do I use layermasks? 9 Answers

Layer mask doesn't work... 1 Answer

Why is Raycast ignoring the layer mask? 1 Answer

Detecting that I'm clicking a unit even though I'm not? 0 Answers

Layer Mask Detection 2 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