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 /
avatar image
0
Question by Dongseon_SHIN · Sep 20, 2018 at 02:17 PM · movementstorecompareactiongridmove

I want to make some event when object change its direction!

Hello!

I make a path finding games.

But I'm not good at unity and C# and so confuse. Please help!

my object has grid movement with this code.

int action = Mathf.FloorToInt(vectorAction[0]);

     if (action == 0)
     {
         rBody.MovePosition(this.transform.position + new Vector3(0, 0, 0.1f));
     }

     if (action == 1)
     {
         rBody.MovePosition(this.transform.position + new Vector3(-0.1f, 0, 0));
     }

     if (action == 2)
     {
         rBody.MovePosition(this.transform.position + new Vector3(0, 0, -0.1f));
     }

     if (action == 3)
     {
         rBody.MovePosition(this.transform.position + new Vector3(0.1f, 0, 0));
     }

     if (action == 4)
     {
         rBody.MovePosition(this.transform.position + new Vector3(0, 0.1f, 0));
     }

     else if (action == 5)
     {
         rBody.MovePosition(this.transform.position + new Vector3(0, -0.1f, 0));
     }

and I want to make a event when object change its dircetion like this.

EX)

(1)up (2)up (3)up [No event]

(1)up (2)right (3) up [Event occured (2) and (3)]

How can I do that?

thx!

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
Best Answer

Answer by Zarenityx · Sep 20, 2018 at 03:48 PM

What you would have to do is store the previous direction you moved in. I'm not quite sure how your action numbers correspond to the vectors they represent, so I'm gonna use your example at the bottom.

You will need a previousDirection variable. If every action corresponds to a direction, this can just be an int for the last action you took. Then, when you move again, you check if it is a different move from the last direction you took. To use your example from before, and extend it with this:

Current: | (1)Up | (2)Up | (3)Up
Previous:| (1)---- | (2)Up | (3)Up
Changed:| (1)No | (2)No | (3)No [No Events]

Current: | (1)Up | (2)Right | (3)Up
Previous:| (1)---- | (2) Up | (3)Right
Changed:| (1)No | (2)YES | (3)YES [Events on 2 and 3]


Doing this in your existing code might be cumbersome, since you have so many if statements. If you're learning C#, I would make a huge suggestion and say to avoid long if-else chains as much as possible. In your case, it's not required, and can be greatly simplified with an array like this (your old code, no events):

 //Up at the top
 Vector3[] directions = {
      new Vector3(0f,0f,1f),
      new Vector3(-1f,0f,0f),
      new Vector3(0f,0f,-1f),
      new Vector3(1f,0f,0f),
      new Vector3(0f,1f,0f),
      new Vector3(0f,-1f,0f)
 };
 
 //Where your big chain of if-else statements was:
 rBody.MovePosition(transform.position+directions[action]*0.1f);

A few things to note here are that I changed this.transform.position to just transform.position, since the this is unnecessary. (If it is necessary and you named a variable 'transform', rename that variable. It will just make your code confusing down the line.) I also made the array contain vectors of length 1 instead of 0.1, then multiplied by 0.1 later. This makes it easier to change that scale, say if you wanted to change the movement speed. Finally, and most obviously, the array never needs changing and every action corresponds to an entry in the array. this means that any changes to the actual movement code only need to be done once rather than 6 times, reducing the potential for error.

Now, to combine the two fixes:
This is the simplified code above, but with an event that is called when the direction changes:

 //In your usings, if you want to use a UnityEvent
 using UnityEngine.Events;
 
 //Up at the top
 Vector3[] directions = {
      new Vector3(0f,0f,1f),
      new Vector3(-1f,0f,0f),
      new Vector3(0f,0f,-1f),
      new Vector3(1f,0f,0f),
      new Vector3(0f,1f,0f),
      new Vector3(0f,-1f,0f)
 };
 
 public UnityEvent OnChangeDirection; //I like UnityEvents, but this might be different if you are doing it some other way.

 private int previousAction = -1; //The previous action variable discussed above





 //Where your big chain of if-else statements was:
 
 if(action != previousAction && previousAction != -1){
      //The -1 part is to prevent it from firing the event when it first
      // starts moving. If you want it to fire then too, just remove the
      // previousAction != -1 part.
 
      OnChangeDirection.Invoke(); //Actually calls the event
 }
 rBody.MovePosition(transform.position+directions[action]*0.1f);
 previousAction = action; //After the action is performed, make it the previous action
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 Dongseon_SHIN · Sep 21, 2018 at 01:21 AM 0
Share

Wow Thanks for kindness answer agian! and It is working!

You are my hero for real!

THAN$$anonymous$$ YOU:)

avatar image Zarenityx Dongseon_SHIN · Sep 24, 2018 at 04:04 PM 0
Share

No problem. If this has worked for you, could you accept this answer as correct?

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

143 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

Related Questions

Grid-based movement with auto-move 1 Answer

C# Movement reversed with GridMove script? 0 Answers

how to make objects move in rows and columns? 0 Answers

Third person controller (fast paced, action RPG, rigidbody) 1 Answer

New Input System 'Started' and 'Performed' actions fire at the same time? 1 Answer


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