Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Wik0 · Jan 11 at 09:16 PM · playergameenemycombat

Game Combat

Hello, I'm currently a first year student at college studying Computer Science. I have a project/game that I almost managed to complete, but I'm struggling to figure out how to implement combat. I tried searching the browser for answers, however the outcome was unsuccessful. I even looked at a handful of YouTube videos, which did help me with sections of the combat such as making the enemy follow the player, but I couldn't quite get them to interact and take damage from each other. I'm running out of time and ideas of how to solve this problem so I though I would get some advice from here.

  • I need the player to take some health from the enemy once the enemy appears on screen (and the required input has been given). Here's the player code so far:

       if (Input.GetMouseButtonDown(1))
                     {
                         //When enemy is facing player attack
                         var forward = transform.TransformDirection(Vector3.forward);
                         var distance = Vector3.Distance(transform.position, enemy.position);
                         var targetDirection = enemy.position - transform.position;
                         targetDirection.y = 0;
                         var angle = Vector3.Angle(targetDirection, forward);
     
                 if (distance < attackRange && angle < spotAngle)
                 {
                     TakeDamage.Enemy(20); //Attack();
                 }
     
                 Ray ray = cam.ScreenPointToRay(Input.mousePosition);
                 RaycastHit hit;
     
                 if(Physics.Raycast(ray, out hit, 100))
                 {
                     Interactable interactable = hit.collider.GetComponent<Interactable>();
                     if(interactable != null)
                     {
                         SetFocus(interactable);
                     }
                 }
             }
    
    
  • I also need the enemy to take some health from the player once they collide.

Here's the enemy code for the collision: public float initialHealth = 60f; private float currentHealth;

 void Start()
 {
     currentHealth = initialHealth;
 }

 public void TakeDamage(float damage)
 {
     currentHealth -= damage;

     if (currentHealth < 0)
     {
         Destroy(gameObject);
     }
 }

 public override void Interact()
 {
     base.Interact();
 }

 void OnCollisonEnter(Collision collision)
 {
     if (collision.collider.gameObject.Player.CompareTag("Enemy"))
     {
         TakeDamage(20);
     }
 }

I know this is a lot to ask for, but I really don't know what to do at this point.

Comment
Add comment · Show 1
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 CMDR_Garbage_ · Jan 11 at 10:02 PM 0
Share

For better insight into what is exactly broken I recommend also putting in the class names. Regardless does TakeDamage.Enemy(20); //Attack(); actually work? It seems more likely it'd be enemy.TakeDamage(20).

0 Replies

· Add your reply
  • Sort: 

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

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

How to handle multiple colliders for one enemy and bullet. 3 Answers

enemy targeting player in different scene 1 Answer

How old do I have to be to publish a game? 1 Answer

Enemy collider used as range to detect player causes player to not go into jump animation, how should I deal with that? 0 Answers

How to script a enemy so that he shoots the player when he is within a certain radius with respect to player (Unity 3D)? 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