Evading obstacles
I am working on a game in which the character (a bird like creature) will automatically evade obstacles. For example, instead of letting one wing go through the wall, the bird will rotate by 90 degrees to evade it. I am using a rigidbody controller, and a box collider on the wing. The obstacle has a box collider as well, and both colliders have isTrigger enabled. This works, kind of... If the left wing evades the obstacle, I can A. move through the obstacle (no matter what, since both are trigger) and B. when I move to the right (further away from the obstacle) the character will start jittering like crazy. I suspect this to be because the collider will no detect anything, thus moving back to it's original rotation (0 degrees) but it rotates back once it collides.
My questions here are:
1. How can I fix the issue with a more smooth collision?
2. How can I prevent the player from moving through the obstacle? (Adding a second box collider with isTrigger set to false, will make the rotation really harsh, and not smooth since I collide with the box too.
Here is my code:
using JetBrains.Annotations;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
Autoevasion will smooth out the collision either of the wings detect. By rotating as if the player turned left or right.
*/
public class autoEvasionLeft : MonoBehaviour
{
public GameObject orbit; //bird creature (player)
public bool mustMoveL = false;
private Vector3 currentPosition;
private Vector3 safeDistancePositionVector;
private Vector3 velocity = Vector3.zero;
private float smoothRotationTime = 5.0f;
private Rigidbody rb;
private void Start()
{
rb = orbit.GetComponent<Rigidbody>();
}
//This will shoot the character away, which is NOT what I want.
private void OnTriggerEnter(Collider other)
{
//Move away from object
//currentPosition = orbit.transform.position;
//safeDistancePositionVector = new Vector3(currentPosition.x + 2, currentPosition.y, currentPosition.z);
//orbit.transform.position = Vector3.SmoothDamp(currentPosition, safeDistancePositionVector, ref velocity, Time.deltaTime * smoothRotationTime);
}
private void OnTriggerStay(Collider other)
{
//Found jitter bug on contanct. The reason for this is that the colliders moves away, and comes in contanct again thus going back and forth
if (other.tag == "noZone") //noZone defines the non reachable areas by the player, i.e. gameobjects colliders collide against
{
//Rotate to evade the object
Quaternion rotationalTargetAngle = Quaternion.Euler(-209.93f, -90, 89.99999f);
orbit.transform.rotation = Quaternion.Slerp(orbit.transform.rotation, rotationalTargetAngle, Time.deltaTime * smoothRotationTime);
}
}
}
I have some ideas, but they are really weird to implement.
1. Once I rotate, disable controls for moving left and right (bad gameplay experience)
2. Once I rotate, shoot a raycast from the back of the bird, and if the raycast detects nothing, the object is gone.
3. Get collider.transform and set an offest, then move the player to that offset.
Is there someone who has a better implementation or a suggestion on which Idea would be fastest, easiest, and best in player experience?