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 poupas · Apr 15, 2014 at 01:19 PM · c#variablevaluemove an object

Variable value not changing (bug or i'm just an idiot?)

Hey everyone. First of all, this is actually my first time sharing a problem with you guys (if im asking you for help is because i really need your master knowledgement).

The problem is simple, but complex for explainning it to you, but ill try my best.

Im making an RTS game like age of empires and so i have workers, which can build, harvest resources, fight and move. Ive made a class for each action (Move, Harvest, Build, Fight).

The problem is in the class Harvest. In here, the worker needs to move to the resource object and then gather a fixed amount of resource. The problem is that the worker cant actually start moving to the object (i use the Move Class to help me do the movement).

When i started making some Debug.Log(), i noticed that the variable (boolean) moving doesnt go from False to True (it actually goes to True, but in the method performAction() it stays False).

Here you have the code for the Move Class:

 public class Move : Action {
     
     private Unit origin;
 
     private Vector3 destinationPos;
     private Quaternion destinationRot;
 
     private bool rotating, moving;
 
     private float moveSpeed, rotateSpeed;
 
     public Move(Unit origin, WorldObject destination) {
         this.origin = origin;
         this.destinationPos = destination.transform.position;
         StartMove(destinationPos);
     }
 
     public Move(Unit origin, Vector3 destinationPos) {
         this.origin = origin;
         this.destinationPos = destinationPos;
         StartMove(destinationPos);
     }
 
     public override void PerformAction() {
         if(rotating) {
             TurnToTarget();
         }
         else if(moving) {
             MoveToTarget();
         }
     }
 
     protected void MoveToTarget() {
         origin.transform.position = Vector3.MoveTowards(origin.transform.position, destinationPos, moveSpeed);
         if(origin.transform.position == destinationPos || origin.CollidedObject) {
             moving = false;
             origin.transform.position -= new Vector3(0f,0f,-2f);
             origin.CollidedObject = null;
         }
     }
 
     private void TurnToTarget() {
         origin.transform.rotation = Quaternion.RotateTowards(origin.transform.rotation, destinationRot, rotateSpeed);
         //Faz o caminho mais rapido na rotacao e retira o problema de o unit ficar virado ao contrario
         Quaternion inverseTargetRotation = new Quaternion(-destinationRot.x, -destinationRot.y, -destinationRot.z, -destinationRot.w);
         if(origin.transform.rotation == destinationRot || origin.transform.rotation == inverseTargetRotation) {
             rotating = false;
             moving = true;
         }
     }
     
     private void StartMove(Vector3 destination) {
         moveSpeed = origin.moveSpeed;
         rotateSpeed = origin.rotateSpeed;
         //Calcula o movimento (rotacao e tranlaccao) entre o destino e a origem
         destinationRot = Quaternion.LookRotation(destination - origin.transform.position);
         rotating = true;
         moving = false;
     }
 }

I really hope theres no need for me to write the harvest class in here (its a bit big).

In GENERAL, the question is: Why the value of moving changes to True in one method (TurnToTarget()) but not in the other (PerformAction())??

Thanks for everything

Pedro Gomes

PS: The Class Move works fine when its used in the Unit, but NOT in the Harvest.

Comment
Add comment · Show 3
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 KiraSensei · Apr 15, 2014 at 02:29 PM 0
Share

Do you enter in the statement :

 if(origin.transform.position == destinationPos || origin.CollidedObject) {

of the $$anonymous$$oveToTarget function ? Put a breakpoint or a log to be sure you go in there.

avatar image ArkaneX · Apr 15, 2014 at 03:01 PM 0
Share

Do you call PerformAction once? If yes, then TurnToTarget method is called (assu$$anonymous$$g rotating is true). Inside that method, moving is set to true, and then method returns. But

 else if(moving)

statement won't be checked, because it is a part of a previous if statement, which was already tested and succeeded. If you want to check for moving then, depending on what you want to achieve, you either have to call PerformAction again or use if(moving) ins$$anonymous$$d of else if(moving).

avatar image poupas · Apr 15, 2014 at 05:23 PM 0
Share

$$anonymous$$iraSensei, i did check that, but it wont go to that statement.

AkaneX you are actually right... If i dont have that statement (if(moving)), the unit cant move... But that doesnt seem right.. the PerformAction() from the Harvest (where it has the PerformAction() from the $$anonymous$$ove) is called in the Update(), so that means that the method is always being called.

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

22 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

Related Questions

C# How To Save PlayerPrefs for Unity GUI 2 Answers

C# Custom Class - Declare Variable's Value 1 Answer

Decreasing a value as another value increases through script 4 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 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