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
2
Question by anonymous · Mar 04, 2011 at 01:40 AM · vector3lerpwait

Vector3.lerp and then wait

I'm trying to make the character go to tree, wit a few seconds, go to the camp and then back to the tree ect. but its not working my code is:

using UnityEngine; using System.Collections;

public class ForagerAi : MonoBehaviour {

 public int Health = 6;
 public Transform Tree;
 public Transform Camp;
 public int moveSpeed;
 public int myAcorns;

 private Transform myTransform;

 // Use this for initialization

 void Awake() {
     myTransform = transform;
 }

 void Start () {
     GameObject t = GameObject.FindGameObjectWithTag("ForagerTree");
     Tree = t.transform;

     GameObject c = GameObject.FindGameObjectWithTag("Camp");
     Camp = c.transform;
     myAcorns = 0;
 }

 // Update is called once per frame
 void Update () {
     if(myAcorns == 0) {
         transform.position = Vector3.Lerp(myTransform.position, Tree.position, Time.deltaTime * moveSpeed);
     }
     if(myAcorns == 5) {
         transform.position = Vector3.Lerp(myTransform.position, Camp.position, Time.deltaTime * moveSpeed);
     }

     if(Health == 0) {
         Destroy(gameObject);
     }
 }

 public void WhereAmI() {
     if(myTransform.position == Tree.position) {
         myAcorns = 5;
     }

     if(myTransform.position == Camp.position) {
         myAcorns = 0;
     }
 }

}

How do I make the character wait? Thanks in advance.

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
0

Answer by Berenger · May 03, 2012 at 05:32 AM

WhereAmI doesn't seem to be called. Anyway, the position wouldn't be equal and the character would end up buzzing around a point. Take a look a that code and the comments :

 public float speed, arrivalDistance, wait;
 public Transform tree, camp;
 
 private IEnumerator Start()
 {
     while( Application.IsPlaying )
     {
         // Wait until the character has reached the tree
         yield return StartCoroutine( GoTo(tree.position) );
 
         // Wait "wait" seconds
         yield return new WaitForSeconds( wait );
 
         // Wait until the character has reached the camp
         yield return StartCoroutine( GoTo(camp.position) );
 
         // Wait "wait" seconds
         yield return new WaitForSeconds( wait ); 
     }
 }
 
 // Reach a destination
 private IEnumerator GoTo( Vector dest )
 {
     // Check the sqMag to spare a Sqrt
     while( (dest - transform.position).sqrMagnitude > arrivalDistance )
     {
         transform.position = Vector3.Lerp(transform.position, dest, Time.deltaTime * speed);
         yield return null;
     }
 }
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

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

2 People are following this question.

avatar image avatar image

Related Questions

Using Lerp in a Constant Moving Object 1 Answer

Not Sure How to Get Around this Problem 1 Answer

Vector3.Lerp making my object invisible. 2 Answers

Object movement with smooth start and end? 2 Answers

Do something when lerp reached target. 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