简体   繁体   中英

Transform.position not moving object to the given Vector3 position

I wrote a code to update positions of objects, and it was working well until I added a new section to move other batches of objects of the same kind.

The objects did not move to the given Vector3 position, so I checked the vector I was giving them; it was the position they were supposed to be moved to. I even checked the distance between the designated position and current position; they never reached 0.

I checked if they were stuck. They don't have Rigidbody and they are not a child to any other objects, so I didn't think this was the case, and I was right. Even when forcibly changed their location to the right one in inspector, they kept moving back to the wrong position.

I have no Idea how this could be happening. Here is the code section I wrote.

public void UpdateCardPosition(int offsetDirection)
{
    TimeBehaviour centralTime = FindObjectOfType<TimeBehaviour>();
    float AnchorDistance = centralTime.GetBoardSize() * 4.5f;
    float ManifestRepulsion = 0f;
    int playerIndex = centralTime.PlayerList.IndexOf(this);
    
    if (FocalEntity != null) ManifestRepulsion = AnchorDistance * 5;
    
    foreach (EntityBehaviour entity in FindObjectsOfType<EntityBehaviour>())
    {
        float em_count = entity.entityManifests.Count;
        for(int i=0; i<em_count; i++)
        {
            CardBehaviour curCard = entity.entityManifests[i];
            if (curCard != centralTime.FocalEvent)
            {
                float theta = Mathf.PI / 2 * (((float)i + 1) / (em_count + 1) - .5f);
                Vector3 newLoc = new Vector3(AnchorDistance * (.5f - playerIndex) / .5f * Mathf.Cos(theta), AnchorDistance*5, AnchorDistance * (.5f - playerIndex) / .5f * Mathf.Sin(theta));
                if (entity == FocalEntity) newLoc = new Vector3(AnchorDistance * (.5f - playerIndex) / .5f * Mathf.Cos(theta), 0f, AnchorDistance * (.5f - playerIndex) / .5f * Mathf.Sin(theta));
                curCard.transform.position = Vector3.Lerp(curCard.transform.position, newLoc, 0.1f);
                // Debug.Log((curCard.transform.position-newLoc).magnitude);
                curCard.transform.rotation = Quaternion.Euler(new Vector3(0, (90f * (.5f - playerIndex) / .5f - theta * 180 / Mathf.PI), 0));
            }
            else
            {
                curCard.transform.position = Vector3.Lerp(curCard.transform.position, centralTime.transform.position, 0.1f);
                curCard.transform.rotation = Quaternion.Slerp(curCard.transform.rotation, centralTime.transform.rotation, 0.1f);
            }
        }
    }
    
    // These Sections Under this Comment works properly, and they are written in the same way as above.
    float pre_count = playerPresent.Count;
    for (int i=0; i<pre_count; i++)
    {
        float AnchorDistance_Pre = AnchorDistance + 1.5f* AnchorDistance * 0.5f * Mathf.Max(0, Mathf.Abs(offsetDirection));
        CardBehaviour curCard = playerPresent[i];
        if(curCard != centralTime.FocalEvent)
        {
            float theta = Mathf.PI / 2 * (((float)i + 1) / (pre_count + 1) - .5f) + offsetDirection * Mathf.PI / 2;
            Vector3 newLoc = new Vector3(AnchorDistance_Pre * (.5f - playerIndex) / .5f * Mathf.Cos(theta), ManifestRepulsion, AnchorDistance_Pre * (.5f - playerIndex) / .5f * Mathf.Sin(theta));
            curCard.transform.position = Vector3.Lerp(curCard.transform.position, newLoc, 0.1f);
            curCard.transform.rotation = Quaternion.Euler(new Vector3(0, (90f * (.5f - playerIndex) / .5f - theta * 180 / Mathf.PI), 0));
        }
        else
        {
            curCard.transform.position = Vector3.Lerp(curCard.transform.position, centralTime.transform.position, 0.1f);
            curCard.transform.rotation = Quaternion.Slerp(curCard.transform.rotation, centralTime.transform.rotation, 0.1f);
        }
    }
    
    float fut_count = playerFuture.Count;
    for (int i = 0; i < fut_count; i++)
    {
        float AnchorDistance_Fut = AnchorDistance + 1.5f* AnchorDistance * (Mathf.Min(-0.5f, offsetDirection) + 1f);
        CardBehaviour curCard = playerFuture[i];
        float theta = Mathf.PI / 2 * (((float)i + 1) / (fut_count + 1) - .5f) + Mathf.PI/2 + offsetDirection * Mathf.PI/2;
        Vector3 newLoc = new Vector3(AnchorDistance_Fut * (.5f - playerIndex) / .5f * Mathf.Cos(theta), ManifestRepulsion, AnchorDistance_Fut * (.5f - playerIndex) / .5f * Mathf.Sin(theta));
        curCard.transform.position = Vector3.Lerp(curCard.transform.position, newLoc, 0.1f);
        curCard.transform.rotation = Quaternion.Euler(new Vector3(0, (90f * (.5f - playerIndex) / .5f - theta * 180 / Mathf.PI), 0));
    }
    
    float pas_count = playerPast.Count;
    for (int i = 0; i < pas_count; i++)
    {
        float AnchorDistance_Pas = AnchorDistance + 1.5f* AnchorDistance * Mathf.Abs(Mathf.Max(0.5f, offsetDirection)-1f);
        CardBehaviour curCard = playerPast[i];
        float theta = Mathf.PI / 2 * (((float)i + 1) / (pas_count + 1) - .5f) - Mathf.PI / 2 + offsetDirection * Mathf.PI/2;
        Vector3 newLoc = new Vector3(AnchorDistance_Pas * (.5f - playerIndex) / .5f * Mathf.Cos(theta), ManifestRepulsion, AnchorDistance_Pas * (.5f - playerIndex) / .5f * Mathf.Sin(theta));
        curCard.transform.position = Vector3.Lerp(curCard.transform.position, newLoc, 0.1f);
        curCard.transform.rotation = Quaternion.Euler(new Vector3(0, (90f * (.5f - playerIndex) / .5f - theta * 180 / Mathf.PI), 0));
    }   
}

You want to move the game object to the target location, in this case, if you use Vector3.Lerp. I assume it doesn't change the position instantly but moves with a predefined speed. If this is the case your script that causes the object to move is under the condition of if(curCard.= centralTime.FocalEvent) and this condition is under another condition foreach (EntityBehaviour entity in FindObjectsOfType<EntityBehaviour>()) . My guess is that since the transformation takes time when any condition is satisfied, the code that causes the object to move will no longer be executed. Even though if(curCard.= centralTime.FocalEvent) is still correct, if the foreach condition is finished, the code under it will no longer be executed. If you said the second section is running properly, I guess the difference between the one with problems and the one that works is the foreach ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM