0

I am using the code below attached to a cylinder object with ridge body and collision. This object is placed in the middle of collision barriers that form a circle. I am trying to get this object to move smoothly forward a random distance and after it moves, wait a random time and then move again. If it hits the barrier with tag name "wall" then turn around by some degree away from walls so it can continues to move around inside. and keep doing the following code.

However the object doesn't move smoothly, and it barely moves at all.

How can I fix this? Thanks in advance for any links or help to this problem.

public bool hit, m = false;


public float waitTime;
public float movDistance;
public float fRotation;
public float transitionSpeed = 1f;

private float lerpTime = 3f;
private float currentLerpTime = 0f;
private Vector3 startPos, endPos;

void Start()
{
    //rb = GetComponent<Rigidbody>();
    startPos = this.transform.position;
}

private void Update()
{
    if (m == false) {
        if (hit == true) {
            currentLerpTime += Time.deltaTime;
            if (currentLerpTime >= lerpTime) {
                currentLerpTime = lerpTime;
                hit = false;
            }
            float p = currentLerpTime / lerpTime;
            this.transform.position = Vector3.Lerp(startPos, endPos, p);


        }
        if (hit == false) {
            setRanges();
            m = true;
            StartCoroutine(Example());
        }
    }
}

public void setRanges()
{
    waitTime = Random.Range(4f, 10f);
    movDistance = Random.Range(2f, 10f);
    fRotation = Random.Range(1f, 270f);
    endPos = this.transform.position + Vector3.forward * movDistance;
}


IEnumerator Example()
{
    Debug.Log("Wait" + waitTime + "Seconds");
    startPos = this.transform.position;
    yield return new WaitForSeconds(waitTime);
    hit = true;
    m = false;

}
void OnCollisionEnter(Collision col)
{
    if (col.gameObject.tag == "wall")
    {

        Debug.Log(" hit wall");
        this.transform.rotation = Quaternion.Euler(0, 180, 0);
        hit = false;
    }
}
shane
  • 342
  • 1
  • 5
  • 28
  • Don't move RigidBody directly via `transform.position`! Rather use [`rb.MovePosition`](https://docs.unity3d.com/ScriptReference/Rigidbody.MovePosition.html) – derHugo Jan 29 '19 at 06:42

1 Answers1

2

Omit some trivial parts from your code

if (m == false) {
    if (hit == true) {
        //.... Move
        hit = false;
    }
    if (hit == false) {
        //.... Set new target position
        m = true;
        //.... Set m to false and hit to true after waitTime
    }
}

And you will find the problem in your logic.

After the object moves for 1 frame, you set hit to false then it stops move again until waitTime passed.

I guess the right logic should be stop the object after it reach the target position, so:

if(hit){
    //... Move
    if(currentLerpTime == lerpTime)
         hit = false;
}
shingo
  • 18,436
  • 5
  • 23
  • 42
  • Thank you this solved the smooth move problem for the first move, I am not sure about the moves after that, When it collides the object rotates around fully but for some reason it keeps going the same direction on the next move even though it rotated. And on the next move and moves after it seems to just teleport to the next position which allows it to leave the boundary area set up. I edited the code above to show my current code being used. – shane Jan 29 '19 at 14:38
  • I think the problem is in my endPos in setRanges() because it is set to move forward? If that is the problem is there a better way to move other than this? – shane Jan 29 '19 at 14:41
  • 1
    1) You need also change the `endPos` when the collision happens. 2) Teleportation is because you never reset `currentLerpTime`. – shingo Jan 29 '19 at 15:17
  • The currentLerpTime makes sense, but is there a better way than Vector3.forward? My object turns around which is good and it isn't always going to be 180 degree turn in the future. So I want the object to move forward the way it is facing but it keeps trying to go the same way even with changing direction. – shane Jan 29 '19 at 15:51
  • `Vector3.forward` always returns (0,0,1), you need use **Object Position** + **Object's Transform.forward** * **distance** to determine the end position. – shingo Jan 30 '19 at 03:41