- Home /
ai jump over object
right now i have enemies going towards each ay point. Im making a tower defence game and am planning on making a special turret where the enemies will have to jump over it. There will only be the one turret where i will make this happen so i have simplified my code to only take into account of this special turret. My problem is that when the enemy jumps it continually jumps to the other waypoints. It looks good but what i want is it just to jump over the collided object then land on the ground.
Code: using UnityEngine; using System.Collections;
public class EnemyWayPoints : MonoBehaviour {
public bool Turrethit = false; public bool isGrounded = true;
public Transform[] waypoint;
private int currentWayPoint;
public float jumpspeed = 8.0f; public float gravity = 20.0f;
public Vector3 target; public Vector3 moveDirection; public Vector3 velocity; public Vector3 jump;
public float speed = 20.0f;
// Use this for initialization void Awake() { waypoint[0] = GameObject.Find("WayPointMain").transform; waypoint[1] = GameObject.Find("WayPoint1").transform; waypoint[2] = GameObject.Find("WayPoint2").transform; waypoint[3] = GameObject.Find("WayPoint3").transform; waypoint[4] = GameObject.Find("WayPoint4").transform; waypoint[5] = GameObject.Find("WayPoint5").transform; waypoint[6] = GameObject.Find("WayPoint6").transform;
}
// Update is called once per frame void Update() {
if (currentWayPoint < waypoint.Length)
{
target = waypoint[currentWayPoint].position;
moveDirection = target - transform.position;
transform.LookAt(waypoint[currentWayPoint]);
}
velocity = rigidbody.velocity;
if (moveDirection.magnitude < 1)
{
currentWayPoint++;
}
else
{
velocity = moveDirection.normalized * speed;
}
rigidbody.velocity = velocity;
if (Turrethit == true)
{
transform.Translate(Vector3.up * jumpspeed * Time.deltaTime);
isGrounded = false;
}
moveDirection.y -= gravity * Time.deltaTime;
}
public void OnCollisionEnter(Collision hit) { if (hit.collider.tag == "STurret") { Turrethit = true; } }
}
i know i havent added into make it hit the ground again and tryed looking at fps script but i dnt really understand how it works. i would like to understand how it works so i know what to do in future projects. thanks in adavance :)
Answer by Jason B · Feb 17, 2011 at 07:32 PM
I'm noticing something wrong with your code. You never turn off TurretHit, so once it's been turned on, your enemies repeatedly jump with nothing telling them to stop (nothing that turns TurretHit to false).
yeh thats exactly what happens and ive tried to resolve it but as soon as it resets to false my enemy stops immeditaley after. im trying to make it so when i hit the ground again i will set it to false but i dnt know the code to make it realise its back on the ground. thanks for your input :)
Alrighty, well, no problem. You actually are halfway to a solution already. A quick and dirty way to accomplish this would be to add an else condition to the end of your OnCollisionEnter if statement. If the hit collider is tagged STurret, turn TurretHit to true. Else, turn IsGrounded to true.
Now, this will also cause them to become grounded when they hit a wall, not just the ground. But that might still be a good fix, since typically they shouldn't be hopping into walls in the first place.
Answer by psychentist · Jun 14, 2013 at 08:06 PM
public CharacterController controller;
void Start(){
controller = gameObject.GetComponent(CharacterController);
}
void Update(){
if (controller.isGrounded == true && Turrethit == true ){
Turrethit = false;
}
}
Your answer
Follow this Question
Related Questions
3D Object Jump Glitch 0 Answers
NavMeshAgent Calculate Jump Trajectory 1 Answer
How to make an AI of a car to jump if there is any obstacle 2 Answers
Emit Particle upon Death 2 Answers
Monster in a certain range of random mobile problems 1 Answer