I am trying to find a solution for 2D A.I. movement, while following the normals of obstacles.
Moreover, I am seeking 1) to learn how to make the A.I. move left or right and negotiate these obstacles.
2) how to influence the orientation of the A.I., from obstacles normals.
Please see attached example image.
Well, what I do is fire 5 rays. 1 in the middle. 2 on the side of the character going forward. 2 on the side of the character going outwards. The front ray in the middle is slightly longer. The ray hits obviously returns the normals which are passed onto the locomotive engine as the force to apply to the steer. Works well for me.
Hope thats not too vague, it took a while to actually take the concept and get it working Unity.
Thanks for the quick reply!
I’m using the following script on my A.I. object, but am not having good results.
How would you recommend steering the A.I., while keeping it attached to the ground and walls?
Thanks,
Greg
private var yRotationOffset : float = 0;
function Update()
{
var hit : RaycastHit;
var castPos =Vector3(transform.position.x-.009,transform.position.y-.009,transform.position.z-.009);
if (Physics.Raycast (castPos, -transform.up, hit, 200)) {
transform.up = hit.normal;
transform.Rotate (0, yRotationOffset, 0);
}}
Sorry I only do C#. I’m not sure what your doing in your code snippet.
Also I know you are doing 2d so somethings may be different.
I use a Freeze rotation on the rigidbody for x and z to stop falling over.
I apply a gravity constant and check whether its colliding with the terrain OR I use ground clamping to align the entity with the normal of the terrain.
I apply a force in the forward direction to the rigidbody to move the entity.
Thanks again for your reply, CrazySi! I’m now using the below code to steer my AI toward the target, but
am having an issue. The AI is getting hung up on obstacles in it’s path, rather than climbing over them.
Any ideas how to solve this?
#pragma strict
var target : Transform; //the enemy's target
var moveSpeed = 3; //move speed
var rotationSpeed = 3; //speed of turning
var myTransform : Transform; //current transform data of this enemy
function Awake()
{
myTransform = transform; //cache transform data for easy access/preformance
}
function Start()
{
target = GameObject.FindWithTag("Player").transform; //target the player
}
function Update () {
//rotate to look at the player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
//move towards the player
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
Perhaps I misinterpreted the image. What I saw was a 2D map top down, and you want the green blocks to avoid the grey blocks whilst followng something like a player.
When you say move “over” and “on top” this doesnt quite make sense given the above scenario.
Therefore I think what you mean is you want obstacles to always wall/obstacle hug, either moving in the clockwise or anticlock wise motion depending on which way round would be closest to the player. Is this correct?
Yes, exactly. This is a side view simulation, with gravity. The gray blocks are the obstacles and the green is the AI.
I have the orientation figured out with:
function OnCollisionEnter(collision : Collision) {
var contact = collision.contacts[0];
var rot = Quaternion.FromToRotation(Vector3.up, contact.normal);
transform.rotation = rot;
}
The steering is problematic as it does not climb over the obstacles as it moves toward its target.
Any idea how pathfinding works for a 2D side view like this?