- Home /
 
making an object move to a certain point
I got a character and I want it to move backwards (like a knockback effect) when it hits an object. So I got the collision working but my problem is the moving part. I'm using this funcion to make it move backwards but the problem is that he keeps going, how can I make him stop at a certain point? or maybe you can suggest a better way:
 transform.Translate(Vector3.left * moveSpeed * Time.deltaTime);
 
               This is all in 2D, with C#.
(Just need him to move backwards from a certain X to another).
Answer by DajBuzi · Feb 10, 2014 at 02:20 PM
In my opinion You shouod usse mathf.lerp: http://docs.unity3d.com/Documentation/ScriptReference/Mathf.Lerp.html
It will "send" your character from point a to point b within the duration of time that you've declared.
For more informations read this : http://answers.unity3d.com/questions/237294/how-the-heck-does-mathflerp-work.html
Answer by BlackPanda · Feb 10, 2014 at 03:12 PM
If you are using a rigidbody, I think adding forces is better than translation. Try with the different force modes.
Answer by ordaskal · Feb 10, 2014 at 04:43 PM
I get errors, here is my script:
         private float curPosX;
         private float newPosX;
 
     void OnTriggerEnter2D(Collider2D other) {
         if (other.tag == "TrapLevel1")
         {
             transform.position.x = Vector2.Lerp(curPosX, newPosX, Time.deltaTime);
          }
 
               }
     // Update is called once per frame
     void Update () {
         curPosX = tranform.position.x;
         newPosX = tranform.position.x - 10;
     }
 
               I want that the object will move backwards by 10 in X position when it collides with "TrapLevel1"
error: Assets/Sample Assets/2D/Scripts/PlayerHitTrap.cs(25,56): error CS1502: The best overloaded method match for `UnityEngine.Vector2.Lerp(UnityEngine.Vector2, UnityEngine.Vector2, float)' has some invalid arguments
 private float curPosX;
 private float newPosX;
 private float speed;
 private bool wasHitten;
 void OnTriggerEnter2D(Collider2D col){
     if(col.tag == "TrapLevel1"){
         wasHitten = true;
         curPosX = transform.position.x;
         newPosX = curPosX - 10.0f;
     }
 }
 void Update(){
     if(wasHitten && curPosX != newPosX){
         passed += speed * Time.deltaTime;
         transform.position = new Vector3($$anonymous$$athf.Lerp(curPosX, newPosX, passed), transform.position.y, transform.position.z);
     } else passed = 0.0f;
 
                  PS. I could've missed something but i wrote this from mobile device and it's hard to write anything on this :p
It doesn't work. no errors were given, and the script does go to both of the ifs, but the object doesnt move backwards.
I 5hink that i've missed something but i'll be able to check it the day after tomorrow... Thats what happens when i write code on mobile device :/
Your answer
 
             Follow this Question
Related Questions
Enemy not moving towards Player 0 Answers
Making a bubble level (not a game but work tool) 1 Answer
Stop Player movement when bool changes 2D 3 Answers
Sprite is not shown moving 1 Answer