Need help with physics base enemy AI movement
I'm learning Unity, and i'm making a game with just simple cubes as the player and the enemies. And since it is a physics based game it uses force to move the player around in only two axis(x and y). this part of the game has been completed. And now i need to create an enemy AI script which will enable the enemy cube to chase the player using force rather than changing the position and is able to jump to floating floors. I already have script for the enemy AI but the problem is that it uses the position to chase the player rather than using the force (physics) and the enemy cube is not able to climb over obstacle nor jump to floating floors. I want to do it but i dunno how. Thank you
Here's the code i'm using now:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Chase : MonoBehaviour
{
public Transform Player;
int MoveSpeed = 1;
int MaxDist = 10;
int MinDist = 1;
void Start()
{
}
void Update()
{
transform.LookAt(Player);
if (Vector3.Distance(transform.position, Player.position) >= MinDist)
{
transform.position += transform.forward * MoveSpeed * Time.deltaTime;
if (Vector3.Distance(transform.position, Player.position) <= MaxDist)
{
//Here Call any function U want Like Shoot at here or something
}
}
}
}
Answer by goutham12 · Jul 24, 2019 at 06:18 AM
don't change the position directly since you don't have plane surface. try to move your enemy with velocity.
like
body.velocity = transform.forward*someSpeed
it worked like a charm!!! thank you!!! I used the addforce ins$$anonymous$$d..
GetComponent<Rigidbody>().AddForce(transform.forward * strength );
But theres another problem.. Since its a cube and because the gravity is on its not moving ones it lands. However it moves when there is some rotation. How can i add rotation in the forward axis.. is there some code for adding a bit of rotation in the forward axis equivalent to "transform.forward".
public Transform Player;
int $$anonymous$$oveSpeed = 1;
int $$anonymous$$axDist = 10;
int $$anonymous$$inDist = 1;
public float strength = 1;
void Start()
{
}
void Update()
{
Launch();
}
void Launch()
{
transform.LookAt(Player);
if (Vector3.Distance(transform.position, Player.position) >= $$anonymous$$inDist)
{
//transform.position += transform.forward * $$anonymous$$oveSpeed * Time.deltaTime; // Original
GetComponent<Rigidbody>().AddForce(transform.forward * strength ); // $$anonymous$$y Code - Add Force
//GetComponent<Rigidbody>().velocity = transform.forward * strength; // $$anonymous$$yCode - Velocity
if (Vector3.Distance(transform.position, Player.position) <= $$anonymous$$axDist)
{
//Here Call any function U want Like Shoot at here or something
}
}
}
If I get You in Right Way you planning to rotate your character on the direction it is moving. Then
transform.localRotation = Quaternion.LookRotation(body.velocity );
and also don't use getComponent in Update take reference in the start method. like
Rigidbody body;
void Start(){
body = GetComponent();
}
Your answer
Follow this Question
Related Questions
How To Stop Enemy Movement During Its Attack Animation 1 Answer
Raycast Enemy AI shooting script 1 Answer
Interesting Enemy AI issue 2 Answers
Making smart enemy AI using GOAP 0 Answers
AI Problem. 0 Answers