• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Trying to get a smooth gridmove 0 Answers

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

Greater than or equal to Vector3 1 Answer

Having trouble with grid movement 1 Answer

Executing multiple actions one after another Unity 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges