How can i Add Sphere AI (Artificial Intelligence) In My Game

Hi,
I created enemy (a ball) that follows my player hit it or touch it and player dead or decreased power.
but i can’t able to script it properly. I use this code in my Sphere(Ball Enemy to gave it AI) But it does not rotate & i didn’t know how to add rotate line or something in my code) can you please help me .
I need real-time rotation when it’s following my player . With this script the enemy does not rotates it follows up my player just track or drag like . it does not rotating like the ball walking . Please help me .

var Player : Transform;
var MoveSpeed = 4;
var MaxDist = 10;
var MinDist = 5;

function Start ()
{

}

function Update ()
{

transform.LookAt(Player);

if(Vector3.Distance(transform.position,Player.position) >= MinDist){

transform.position += transform.forwardMoveSpeedTime.deltaTime;
transform.Rotate(new Vector3(15,30,45)* Time.deltaTime);

var moveHorizontal : float = Input.GetAxis(“Horizontal”);
var moveVertical : float = Input.GetAxis(“Vertical”);

if(Vector3.Distance(transform.position,Player.position) <= MaxDist)
{
//Here Call any function U want Like Shoot at here or something
}

}
}

One more thing can i use free textures of assets store commercially for free and i didn’t found anything license type on there assets page .

You need to get rid of “transform.LookAt” since it’s telling the ball to rotate toward the player. If you need to get the direction to the player use Vector3.Normalize(Player.position - transform.position).

It’s not roration perfectly and it’s AI not works anymore perfect after deleting - transform.LookAt(Player);
and insert - Vector3.Normalize(Player.position - transform.position); instead .

var Player : Transform;
var MoveSpeed = 4;
var MaxDist = 10;
var MinDist = 5;

function Start ()
{

}

function Update ()
{

Vector3.Normalize(Player.position - transform.position);

if(Vector3.Distance(transform.position,Player.position) >= MinDist){

transform.position += transform.forwardMoveSpeedTime.deltaTime;
transform.Rotate(new Vector3(15,30,45)* Time.deltaTime);

var moveHorizontal : float = Input.GetAxis(“Horizontal”);
var moveVertical : float = Input.GetAxis(“Vertical”);

if(Vector3.Distance(transform.position,Player.position) <= MaxDist)
{
//Here Call any function U want Like Shoot at here or something
}

}
}

am i miss something ?

@QSI_Andrew
can you please tell me about

guys any answer will be appreciated

here in c# now i am working on it with same problem

using UnityEngine;
using System.Collections;

public class EnemyAI: MonoBehaviour {

public Transform target;
public int moveSpeed;
public int rotationSpeed;

private Transform myTransform;

void Awake(){
myTransform = transform;
}

// Use this for initialization
void Start () {
GameObject go = GameObject.FindGameObjectWithTag(“Player”);

target = go.transform;
}

// Update is called once per frame
void Update () {
Debug.DrawLine(target.position, myTransform.position, Color.cyan);

//look at target
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);

//move towards target
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
}

please here anyone that can solve my ball problem described in my 1st post on this topic on main post .
But i want result in c#

when i try to erase [/ myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime); ] then after half rotation enemy ball stops stand in one position and starts small jumps .

Please use code tags when pasting your code.

You can do this automatically by picking the icon in the tool bar when writing your post, or by using square brackets [ ] with the words code at the beginning and /code at the end inside these square brackets.

For more information, please see the sticky post in scripting and support on how to use code tags.

1 Like

Personally, you can try to manually determine the movement and rotation of the sphere, but I would simply use forces, and let the ball rotate on it’s own.

Look at the learn tab of the unity website and watch the “roll a ball” project for examples.

That’s the tutorial sir i use it first to make my game .
but i can’t understand of it’s ai motion how i follow my player with spin without dragging .

Just trying to understand exactly what you want.

You want your ball to follow your player?

If it were me, I would take the point where my ball is, and find the point where my player is, which will give you the direction between them, and then I would add force on the ball, pushing it towards the player.

Thanks for answer sir but this is solved .