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 Flickayy · Apr 20, 2015 at 04:49 PM · c#positioningvector3.lerp

Enemy Lerping between two Vectors

Hi all,

I've got a quick question, hoping someone can enlighten me as to why it's not functioning correctly. Probably doing something wrong knowing me!

I'm trying to slide an enemy left and right on the x-axis. I've watched and read a few things on Vector3.Lerp but I'm finding it difficult to implement it.

Currently the enemy moves into a position, but never actually reaches the value I've set. Please take a look at the script below.

EDIT: The script below now works, credit to JoeStrout for the help. Hopefully someone will find this useful.

 using UnityEngine;
 using System.Collections;
 
 public class EnemyAI : MonoBehaviour {
 
     public float moveSpeed = 0.5f;
 
     private Transform self;
     private Vector3 newPos;
 
     void Awake() {
         self = transform;
         newPos = new Vector3(-2, transform.position.y);
     }
     
     // Update is called once per frame
     void Update () {
         SlideEnemy();
     }
 
     void SlideEnemy() {
         Vector3 posA = new Vector3(-2, transform.position.y);
         Vector3 posB = new Vector3(2, transform.position.y);
         if (transform.position == posA) {
             newPos = posB;
         }
         else if (transform.position == posB) {
             newPos = posA;
         }
         
         gameObject.transform.position = Vector3.MoveTowards(transform.position, newPos, Time.deltaTime * moveSpeed); 
     }
 }
 


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
1
Best Answer

Answer by JoeStrout · Apr 20, 2015 at 05:06 PM

Yep, that's an abuse of Lerp, all right. Lerp takes a starting point, an ending point, and how far you want to be from one to another. It never takes the current position as any of its parameters. So if you find yourself passing in the current position, you need to step back from that ledge.

In this case -- and many others where Lerp is abused -- what you probably want is Vector3.MoveTowards. That takes the current position, where you're trying to go, and how far you're willing to move on this frame. These are the three parameters you're already using; so use the correct function, and you should be all set!

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 Flickayy · Apr 20, 2015 at 05:08 PM 0
Share

Yeah I've only just recently started to learn about the lerp methods, i'll give it a try now. Thanks for the swift answer!

avatar image Flickayy · Apr 20, 2015 at 05:16 PM 0
Share

Thanks, it didn't work at first but that's due to my own logic being flawed. I've updated the OP with the working script for anyone else who might like to know the correct way to do it.

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

20 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

moving the main game window 0 Answers

Placing player relative to object,Placing the player relative to an object 2 Answers

Renderer on object disabled after level reload 1 Answer


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