- Home /
How to make my enemy jump over obstacles?
finally i made my first game in unity http://www.youtube.com/watch?v=RBCv3DuBvKE
but now what i want to now is how can i make that when my enemy which is the red one instead pass through the cubes he jump
Answer by aldonaletto · Oct 03, 2011 at 03:50 AM
It's way more complicated: you must create an AI (Artificial Intelligence) script to control your enemy. It could use Rigidbody.SweepTest with a short distance (1, for instance) to see if the enemy will collide with something ahead, and make it jump if there's an obstacle. An alternative could be to use a trigger childed to your your enemy and positioned a little ahead, so when something hit it you could make the enemy jump.
EDITED: Well, this is basically your original script with some optimization and the code to jump automatically included - it will jump when colliding to some lateral object (it collides with the ground all the time, so collisions with normals above 30 degrees (sin = 0.5) are ignored. I added also a "jetpack" feature to pass over the abyss - when there is no ground down to 20m below it, the jetpack is turned on until it finds ground. Your enemy must be a CharacterController: if it's not, add it using the menu option Component/Physics/Character Controller.
using UnityEngine; using System.Collections;
public class Enemy : MonoBehaviour {
public float speed = 4f;
public float jetPackSpeed = 0.3f;
public float jumpSpeed = 8f;
public float gravity = 10;
private Transform _Player;
private CharacterController character;
private Transform tr;
private float vSpeed = 0f;
private bool jump = false;
void Start ()
{
_Player = GameObject.FindGameObjectWithTag("Player").transform;
character = GetComponent<CharacterController>();
tr = transform;
}
void Update ()
{ // find the vector enemy -> player
Vector3 chaseDir = _Player.position - tr.position;
chaseDir.y = 0; // let only the horizontal direction
float distance = chaseDir.magnitude; // get the distance
if (distance <= 2)
Debug.Log("Attacking Player");
else
{ // find the player direction
Quaternion rot = Quaternion.LookRotation(chaseDir);
// rotate to his direction
tr.rotation = Quaternion.Slerp(tr.rotation, rot, Time.deltaTime * 4);
if (character.isGrounded){ // if is grounded...
vSpeed = 0; // vertical speed is zero
if (jump){ // if should jump...
vSpeed = jumpSpeed; // aplly jump speed
jump = false; // only jump once!
}
}
else // but if lost ground, check if it's an abyss
if (!Physics.Raycast(tr.position, -tr.up, 20f)){ // if no ground below
vSpeed = jetPackSpeed; // use jetpack
}
vSpeed -= gravity * Time.deltaTime; // apply gravity
// calculate horizontal velocity vector
chaseDir = chaseDir.normalized * speed;
chaseDir.y += vSpeed; // include vertical speed
// and move the enemy
character.Move(chaseDir * Time.deltaTime);
}
}
// if collided with some wall or block, jump
void OnControllerColliderHit(ControllerColliderHit hit){
// only check lateral collisions
if (Mathf.Abs(hit.normal.y) < 0.5){
jump = true; // jump if collided laterally
}
}
}
i like you because you always answer my questions but can you gime me a example because im not good coding
What is your enemy? A CharacterController? Post the current script you're using to move the enemy.
using UnityEngine; using System.Collections;
public class Enemy : $$anonymous$$onoBehaviour {
public float speed = .05f;
private float _x$$anonymous$$ove; private float _y$$anonymous$$ove; private RaycastHit _h; private Transform _Player; private float _distanceFromPlayer;
void Start ()
{
_Player = GameObject.FindGameObjectWithTag("Target").transform;
}
void Update ()
{
_distanceFromPlayer = Vector3.Distance(transform.position, _Player.position);
if(_distanceFromPlayer <= 1)
Debug.Log("Attacking Player");
else
{
transform.rotation = Quaternion.Slerp(transform.rotation,Quaternion.LookRotation( _Player.position - transform.position ), Time.deltaTime * 4);
transform.Translate(0,0,.05f);
}
}
}
Oh, this script! But that's not what the enemy is doing right now: it just goes from left to right at a fixed speed. Ok, I'll think a little about this.
Answer by ocularcash · Oct 03, 2011 at 03:30 AM
you could try something like this:
var range = 2;
if(Vector3.Distance(transform.position, target.position) > range
thankZzz but i got this error
Assets/Robot Artwork/Enemy.cs(19,29): error CS0411: The type arguments for method `UnityEngine.Component.GetComponent()' cannot be inferred from the usage. Try specifying the type arguments explicitly
@mangohack, UA ate the CharacterController type (it thought <CharacterController> was a tag). I fixed the answer, and now it must work.
thankyouuuuuuuu thankyouuuuuuuu it workkkkkkkkkkkkkkkkkkkk im so happyyyyyyyyyyyyyyy thankyouuuuuuuu i love you you the BESTTTT everrrr lol
Your answer
Follow this Question
Related Questions
How to make an object jump? 0 Answers
How do I make an enemy AI jump? 0 Answers
AI Script attached to Enemy and is Rotating around player 0 Answers
Disable a target after trigger exit? 1 Answer