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 Caelorn · Aug 02, 2013 at 08:18 PM · combat

Problem with combat.

So I have this specific problem. I'm following BurgZergArcade tutorials, but I wanted to implement them to something of my own. (Think 2.5D platformer) So the problem is as follows: When attacking from the left everything is fine. I hit the target when facing it and missing when facing away, but when hitting from the right side I can hit it no matter if I'm facing the enemy or not.

 using UnityEngine;
 using System.Collections;
 
 public class PlayerAttack : MonoBehaviour {
     public GameObject target;
     public float attackTimer;
     public float cooldown;
 
     // Use this for initialization
     void Start () {
         attackTimer = 0;
         cooldown = 1.0f;
     }
     
     // Update is called once per frame
     void Update () 
     {        
         if(attackTimer > 0)
         {
             attackTimer -= Time.deltaTime;
         }
         
         if(attackTimer < 0)
         {
             attackTimer = 0;
         }
         
         if (Input.GetKeyUp(KeyCode.F))
         {
             if(attackTimer == 0)
             {
                 Attack();
                 attackTimer = cooldown;
             }
         }
     
     }
     
     private void Attack()
     {
         float distance = Vector3.Distance(target.transform.position, transform.position);
         
         Vector3 dir = (target.transform.position - transform.position).normalized;
         
         float direction = Vector3.Dot(dir, transform.forward);
         
         Debug.Log(direction);
         Debug.Log(distance);
         
         if(distance < 3)
         {
             if(direction >= 0)
             {
                 EnemyHealth eh = (EnemyHealth)target.GetComponent("EnemyHealth");
                 eh.AdjustCurrentHealth(-10);
             }
         }
     }
 }
 

problem seems to be that "direction" is exactly 0 when attacking from left or right, if player is looking right. Any advice?

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 EHogger · Aug 02, 2013 at 09:39 PM

If direction is 0 then dir and transform.forward are perpendicular. Your model is probably rotated wrong. Check that the blue axis is actually pointing forward.

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 Caelorn · Aug 03, 2013 at 07:55 AM 0
Share

Thank you so much for the answer! Now i'm feeling kinda stupid for not figuring that out, but at least it's working :P

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

15 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to import the object from server to unity 2 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Material doesn't have a color property '_Color' 4 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