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 GregIven · Nov 27, 2015 at 11:16 AM · movementattack

Click to move towards an opponents transform and attack when in range

EDIT: So the problem is that all of my code is contained in IF statements, so when I click the mousebutton down it calls the movetopos function, which moves my player towards the transform. However I have no way of checking within that function when I have reached the range and then attacking. I have tried all types of while/do-while loops but cannot get it done.

All I want is Click left mouse button down -> Retrieve enemies transform -> move towards transform -> when transform is reached to automatically attack.

I've gone over and condensed my code to show only necessary functions.

Thanks

 void Update () {
         InRange();
 
         if (!die)
         {
             //Player clicks left mouse button to move only
             if (Input.GetMouseButton(0))
             {
                 //get position of where player clicked
                 GetPosition();
             }
 
             #region TEST UPDATE 1
             //Player clicks the right mouse button, if enemyhit, player will move to enemy to attack, if not hit, enemy move to pos hit.
             if (Input.GetMouseButtonDown(1))   //This button doubles as movement and attack
             {
                 GetPosition();
                 if (!inRange && enemyHit)
                 {
                     
                     if (inRange)
                     {
                         Attack();
                     }
                 }
                 else if (inRange && enemyHit)
                 {
                     _position = transform.position;
                     
                     Attack();
                 }
             }
            // MoveToPos();
             #endregion
 
         }
         //MoveToPos();
 
         Impact();
 
      
         if (!inRange)
         {
             MoveToPos();
         }
 
         if(inRange)
         {
             Attack();
         }
     }
 
  void GetPosition()
     {
         RaycastHit hit;
 
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
         if (Physics.Raycast(ray, out hit, 1000f))
         {
             if (hit.collider.tag != "Player" && hit.collider.tag != "Enemy")
             {                
                 _position = new Vector3(hit.point.x, 0, hit.point.z);
                 enemyHit = false;
             }
 
             if (hit.collider.tag == "Enemy")
             {
                 enemyHit = true;
                 opponent = opponent.gameObject;
                 if(!inRange)
                 _position = opponent.transform.position;
 
             }
             //Instantiate animatation to represent click pos on the terrain
             //GameObject position = (GameObject)Instantiate(posClick, _position, Quaternion.identity);
         }
     }
 
    void MoveToPos()
     {
         //GameObject is moving
         if (Vector3.Distance(transform.position, _position) >= RADIUS)
         {
             GetMousePos();
 
             Quaternion newRotation = Quaternion.LookRotation(_position - transform.position, Vector3.forward);
 
             newRotation.x = 0f;
             newRotation.z = 0f;
 
             transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 10);
 
             //transform.position = Vector3.MoveTowards(transform.position, opponent.transform.position, step);
 
            
             cc.SimpleMove(transform.forward * speed);
 
             GetComponent<Animation>().CrossFade(run.name);
 
             
             //GetComponent<Animation>().CrossFade(run.name);
         }
 
         //This was to check if play was within radius of attack, if they were it was meant to stop the player at their current position.
         //else if (Vector3.Distance(transform.position, _position) <= RADIUS)
         //{
         //    Attack();
         //}
 
         //Game object finished moving
         else
         {
             GetComponent<Animation>().CrossFade(idle.name);
         }
     }
 
   public void Attack()
     {
         //if (Input.GetMouseButton(1))
         //{
         //    if (!started)
         //    {
         //        GetComponent<Animation>().Play(attackClip.name);
         //        attack = false;
         //        transform.LookAt(lookTarget);
         //    }
         //}
 
         if (!started && enemyHit)
         {
             transform.LookAt(lookTarget);
             GetComponent<Animation>().Play(attackClip.name);
             attack = false;
         }
     }
 
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 Salmjak · Nov 27, 2015 at 03:20 PM 0
Share
 if (!InRange())
                  {
                      $$anonymous$$oveToPos();
  
                      //If player is in range they will attack only, not move
                      if (InRange())
                      {
                          Attack();
                      }
                  }

Your "Inrange" checks are within the same brackets. You first check "!Inrange" and when you know you're not in range you still say "if (inrange)". You also have a check in your $$anonymous$$ovePos-function, which says: if you're close enough, don't move. That means, if you are in range, "if(!Inrange)" will never be true. You will never move, and you will never check if you are in range. So as soon as you're in range the functions will never be called.

Atleast this is my first reaction, not sure if it actually would work anyway (since you do change your position before second check).

You can also try to change your check to: "Vector3.Distance(transform.position, _position) >= RADIUS" So that you make sure that you're actually within the radius and not exactly at the border.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by GregIven · Nov 28, 2015 at 09:27 AM

@salmjak Ya I see what your saying, after looking into it a little more, I think the problem is that it checks upon each click whether I am in range or not. However, what I need it to do is upon each click: Check if in range, if not in range, I need to move to and then attack in the same click. If I am in range, just a simple attack.

I am stumped at the part of getting to move and attack in the same click....

Comment
Add comment · Show 2 · 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 Salmjak · Nov 28, 2015 at 04:00 PM 0
Share

Get$$anonymous$$ouseButton(0) is true when pressing and holding left mouse button :) So you might want just Get$$anonymous$$ouseButtonDown(0)? This will only be true when you click and will prevent the code from running with every update-tick.

avatar image GregIven Salmjak · Nov 28, 2015 at 10:58 PM 0
Share

Hey, so I updated the question in case it was a bit confusing. But I want a few things to happen when I click that button, I'll try what you suggested, but its not exactly my problem. Although it could help to smooth out the whole system.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Move and Attack ( for MOBA style minions) 1 Answer

Help 2D attack 1 Answer

OnTriggerEnter2D only triggers once, until the character or enemy moves again. (With video showcase) 1 Answer

Charecter keeps playing walking animation after punching 0 Answers

Characters moving in mid air after going up hills/mountains and won't come back down for unkown reason(s) 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