x


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.

more ▼

asked Mar 04 '11 at 01:40 AM

anonymous gravatar image

anonymous
35 3 4 5

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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;
    }
}
more ▼

answered May 03 '12 at 05:32 AM

Berenger gravatar image

Berenger
11k 12 19 53

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x577
x263
x58

asked: Mar 04 '11 at 01:40 AM

Seen: 1357 times

Last Updated: May 03 '12 at 05:32 AM