How to move two object at time [Solved]

Hello Members,
I have added two object (.obj) file.Both object are moving on navMesh. On right click 1st object is moving. on Left click 2nd object is moving. Click means target position of that object.

I am facing a problem if I click right 1st object is moving between moving the object If I pressed left click 2nd object is moving but 1st object is stop without reaching to target position. Video . jsfiddle

You use one path calculatedPath for both. Once you click or doubleclick the current overwrites the other.

2 Likes

@Fyrestar Ok I make separate for both

From my point of view, it’s better to have two different objects of the same structure, containing a moving object, its path and update() function.

@prisoner849, I did not get you, can u give piece of code

Take a look at the source code of this example. Though it was made with r73, it uses the conception of what I’m talking about. There are three objects, each of them contains several properties, including an object of a sphere and update() function. The code is not so perfect (I rather would say that it’s imperfect), but it does what it was meant to do.

Reflections

@prisoner849 thank sir, its only example, easy to understand if code is there

@prisoner849
controls.update(); not worked

When and where did I say that the object, containing update() function, is an instance of THREE.OrbitControls()?

I refereed ur code

Was it the one single object that contains update()?

spheres.forEach(function (sphereObj) {
  sphereObj.update(delta);  // what's this then?
});

@prisoner849, @donmccurdy , @looeee , @trusktr , @Fyrestar try to help me I am still facing this problem

I’ve tried already.

3 Likes

The problem is you just need to organize your code better: You have global variables, and when you click or right click, you are overriding them. calculatedPath for example is modified when you click or right click. You’re overriding variables that each character needs for the path.

You should organize your code so that each character has it’s own calculated path variable. One way you can do this is to make separate variables for each character like calculatedPath1, calculatedPath2, etc.

Another way to organize is to learn how to make JavaScript classes, and store the information in separate instances of your classes. For example, you might store path information for each character on the instance of the character.

If all your target environment support class {} syntax, learn how to use that. If your target environments don’t all support class syntax, then learn the old traditional “JavaScript constructor patterns” (the same pattern all Three.js classes are made with).

2 Likes

@trusktr, thank you sir, for explaining this… Thank U very much I will try to implement is.

@trusktr

You should organize your code so that each character has it’s own calculated path variable. One way you can do this is to make separate variables for each character like calculatedPath1, calculatedPath2, etc.

this method worked fine,but number of lines increased, How i can used one method for multiple cars

Just out of curiousity, have you read this paragraph?

If you got it working, the best way to continue getting more help is with more examples (post a new pen of what it looks like now)

3 Likes