- Home /
rigidbody.MovePosition & translation
Hi,
I have a npc-monster which has a "road".
The part of the code :
dir = new Vector3(10,0,10);
for (i=0;i<100;i++){
rigidbody.MovePosition(this.transform.position + dir * 0.01f);
and then he makes the inverse "road". But there is like some micro lags sometimes and the npc goes too far or too short so he is translated which is a problem. I tried to change all properties of rigidbody in vain.
I made a video-example : https://www.youtube.com/watch?v=QrvFO0gdDPY&feature=youtu.be
Thanks.
answer was this bug happens only when playing the game in unity editor. when the game is built (build + run) it dont happen.
Answer by M-G-Production · Dec 01, 2014 at 06:23 AM
Hi ayks!
You should read about Vector3.MoveTowards and transform.LookAt...
Actually, this is the first time I see a moving system using rigidbody.MovePosition... When you are blocked, you should try another way.
What would I do? There is a lot of ways of doing it...
Create two empty objects named: Target1 and Target2 and put them where you want your monster to go;
Put this in monster script:
public GameObject target1, target2; public float spd; private GameObject myTarget; void Start() { myTarget = target1; } // Update is called once per frame void Update () { if (transform.position == target1.transform.position) {myTarget = target2;} if (transform.position == target2.transform.position) {myTarget = target1;} transform.position = Vector3.MoveTowards (transform.position, myTarget.transform.position, spd * Time.deltaTime); transform.LookAt (myTarget.transform); }
Drag and drop the 2 targets.
What is cool with this, you can control the speed and the destinations. If you want more than two target, read about Arrays. Don't forget that with the RigidBody component, you can add constraint with position and rotation.
This should fix these bizarre behaviors!
Math
Hello $$anonymous$$.G Production. Wow, a big clean answer.
I knew about transform.LookAt (basic for rpg-like :)), recently learned about Vector3.$$anonymous$$oveTowards (3-4 days ago).
I use rigidbody.$$anonymous$$ovePosition because it has to collide with player at a moment which seems logic to me although I'm not a reference of logic.
It looks cool indeed, apparently I did it without target. I think I don't like checkpoint-target, without considering efficiency. I'll keep your method for next movements things.
Thanks for the answer.
Ok, I have another solution co$$anonymous$$g up for you :D
There is no more problem (with your first answer) I'm ok on this point personnally ! :y thanks :-
Your answer
Follow this Question
Related Questions
How Do I Reduce Lag in my Networked Game? 5 Answers
Does collider size affect performance? 1 Answer
Multiplayer Rigidbody Vibration 2 Answers
Why does this simple follow script make my game lag? 2 Answers
MovePosition not as precise as Translate with Kinematic rigidbodies (example unitypackage included) 1 Answer