Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 ordaskal · Feb 10, 2014 at 02:06 PM · c#2dmovementobjectcharacter

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

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · 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
0

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.

Comment
Add comment · 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
0

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

Comment
Add comment · Show 3 · 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 DajBuzi · Feb 10, 2014 at 04:56 PM 0
Share
 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

avatar image ordaskal · Feb 10, 2014 at 05:41 PM 0
Share

It doesn't work. no errors were given, and the script does go to both of the ifs, but the object doesnt move backwards.

avatar image DajBuzi · Feb 10, 2014 at 05:52 PM 0
Share

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

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

21 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

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

Non-symmetrical Character walk left animation. 0 Answers


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