Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Samir 1 · Feb 17, 2011 at 06:27 PM · objectaijump

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 :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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).

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Samir 1 · Feb 17, 2011 at 07:56 PM 0
Share

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 :)

avatar image Jason B · Feb 17, 2011 at 08:17 PM 0
Share

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.

avatar image
0

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;
 
 }
 
 
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges