Search Unity

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

Discussion in 'Scripting' started by JeevanjotSingh, Oct 31, 2014.

  1. JeevanjotSingh

    JeevanjotSingh

    Joined:
    Apr 30, 2014
    Posts:
    251
    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.forward*MoveSpeed*Time.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 .
     
    Last edited: Oct 31, 2014
  2. QSI_Andrew

    QSI_Andrew

    Joined:
    Dec 16, 2013
    Posts:
    4
    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).
     
  3. JeevanjotSingh

    JeevanjotSingh

    Joined:
    Apr 30, 2014
    Posts:
    251
    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.forward*MoveSpeed*Time.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 ?
     
  4. JeevanjotSingh

    JeevanjotSingh

    Joined:
    Apr 30, 2014
    Posts:
    251
  5. JeevanjotSingh

    JeevanjotSingh

    Joined:
    Apr 30, 2014
    Posts:
    251
    guys any answer will be appreciated
     
  6. JeevanjotSingh

    JeevanjotSingh

    Joined:
    Apr 30, 2014
    Posts:
    251
    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#
     
  7. JeevanjotSingh

    JeevanjotSingh

    Joined:
    Apr 30, 2014
    Posts:
    251
    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 .
     
  8. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    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.
     
    JeevanjotSingh likes this.
  9. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    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.
     
  10. JeevanjotSingh

    JeevanjotSingh

    Joined:
    Apr 30, 2014
    Posts:
    251
    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 .
     
  11. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    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.
     
  12. JeevanjotSingh

    JeevanjotSingh

    Joined:
    Apr 30, 2014
    Posts:
    251
    Thanks for answer sir but this is solved .