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 Danteeva · Jan 29, 2014 at 12:50 AM · c#movementtargeting

Character doesn't stop and sits on top of target.

Hello there, I am currently trying to make it so that when you click on an enemy the character walks towards the target and stops at a certain range. The problem is that no matter what I set the distance to in the if statement it always walks right up to the target and sits on top of it. It fine for the player to be able to walk through/past them normally so I don't want a collider on them or the player. Could anyone help please. I use the same code for normal walking and that will stop before if i set the distance to a larger variable.

Also I must double click off of the target to get it to move away normally which is annoying me also if anyone could hep with that.

This is the clickToMove script.

         if(Input.GetKeyDown(KeyCode.Mouse1))
         {  
             enemyTarget = null;
             Targeting targeting = GetComponent<Targeting>();
 
             Plane playerPlane = new Plane(Vector3.forward, transform.position);
             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
             float hitdist = 0.0f;
 
             if (targeting.selectedTarget != null) {
                 enemyTarget = GameObject.FindGameObjectWithTag ("Target").transform;
                 enemyDistance = Vector2.Distance(enemyTarget.position, transform.position);
             }
     
             else if(enemyTarget == null && playerPlane.Raycast (ray, out hitdist)) {
                 clickPosition = ray.GetPoint(hitdist);
             }
             
         }
         if(enemyTarget != null && enemyDistance > 1.3f){ // Prevents code running when it doesn't need to
             transform.position += (enemyTarget.position - transform.position).normalized * moveSpeed * Time.deltaTime;
         }
         
         if(enemyTarget == null && playerDistance > 0.1f){ // Prevents code running when it doesn't need to
             transform.position += (clickPosition - transform.position).normalized * moveSpeed * Time.deltaTime;
         }

This is the Targeting script.

 public class Targeting : MonoBehaviour {
     
     public Transform selectedTarget = null;
     public int targetfound = 0;
     
     void Update(){
 
         if (Input.GetMouseButtonDown(1)){ // when button clicked...
             Vector3 pos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
             //Debug.Log (pos);
             RaycastHit2D hit; // cast a ray from mouse pointer:
             hit = Physics2D.Raycast (pos, Vector3.zero);
 
             if (hit != null && hit.transform != null && hit.transform.CompareTag("Enemy")) {
                 DeselectTarget(); // deselect previous target (if any)...
                 selectedTarget = hit.transform; // set the new one...
                 selectedTarget.tag = "Target";
                 targetfound = 1;
                 Debug.Log ("Enemy Targeted");
             }
             else{
                 DeselectTarget();
             }
         }
     }
 
     private void DeselectTarget(){
         if (selectedTarget != null){ // if any guy selected, deselect it
             selectedTarget.tag = "Enemy";
             selectedTarget = null;
         }
     }
 }
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
1
Best Answer

Answer by Lockstep · Jan 29, 2014 at 01:27 AM

Your infrastructure sure is pretty messy. For instance you have your Targeting class change the tag of your enemy and then have the movement script look for the tag instead of simply taking the selectedTarget directly from Targeting. But I don't think that this will cause the problem. (But may cause other problems at other places).

The main problem is likely to be caused by the fact that both scripts are dependant of each other but manage input independantly. This way you have no controll about which script gets managed first. Out of the blue: I assume the movement script gets handled first, finds that targeting.selectedTarget is null and thus just move as if no target was selected and sets movement type to "no target found". Later that frame the Targeting script gets served, detects the enemy and sets its selectTarget field accordingly. But that was just to late for the mover script.

In such cases always add in debug statements inside of each if option to check out which branch you are running into.

To fix your current problem I'd suggest to change the update method from the targeting script to an arbitrary public method and then call it from the mover script:

Inside of targeting:

 public void FindTarget(){
     //The code which was inside of update but without the if Mouse
 }

Inside of the mover script:

 if(mouse){
     //other code
     targeting.FindTarget();
     //more code
 }
Comment
Add comment · Show 7 · 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 Danteeva · Jan 29, 2014 at 02:49 PM 0
Share

Thanks for the reply.

Yeah I did the Tag thing to see if it would help, I had what you said before. Although this will help with when I add in attack and I only want it to hit one target. I'm sure there is an easier way but I'm new :) I've also added a bit so that if its already tagged as Target it doesn't deselect. Hopefully it doesn't cause any problems as I've had enough of this script now :/

O$$anonymous$$ so I've tried that and there isn't much difference, I've added some debug statements as well.
The first thing that happens when I click on an enemy is it says "Enemy Targeted".

         if(enemyTarget != null && enemyDistance > 1.3f){ 
             transform.position += (enemyTarget.position - transform.position).normalized * moveSpeed * Time.deltaTime;
             Debug.Log("$$anonymous$$oving to Target");
         }

So after it says "Enemy Targeted" it then says over and and over "$$anonymous$$oving to Target" but doesn't actually move, I have to click lots of times, and then it only moves part of the way. Once it gets to a certain range it just moves directly on top of the target and shakes.

avatar image Lockstep · Jan 29, 2014 at 05:07 PM 0
Share

I think I see the problem now. It's this line:

 enemyDistance = Vector2.Distance(enemyTarget.position, transform.position);

Or rather where this line is. You only update your enemyDistance Variable when you click, but of course you want to update this information every frame.

Try moving this line out of if(mouse){}´and in front of if(enemyDistance){}.

avatar image Danteeva · Jan 29, 2014 at 05:40 PM 0
Share

That now giving me a a null reference exception error as it doesn't start with a target so enemyTarget is null. It does when I click on a target though move to them like I was intending, but I can't move around normally now. Its hard to see as well as the null reference comes up a lot as its part of the update.

avatar image Lockstep · Jan 29, 2014 at 05:44 PM 0
Share

Just add if(enemyTarget) the line before updating enemy Distance.

avatar image Danteeva · Jan 29, 2014 at 05:54 PM 0
Share
 if(enemyTarget)

It finally works! Thank you very much :D Its all these little things I need to learn, but I'm getting there. Thanks again!

Show more comments

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

19 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

Related Questions

Multiple Cars not working 1 Answer

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

how can i auto target Gameobject on startup 1 Answer

Making a bubble level (not a game but work tool) 1 Answer

c# how to refresh my targeting list 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