Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
-2
Question by mangohack · Oct 03, 2011 at 01:00 AM · aienemyjumpobstacle

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

Comment
Add comment · Show 4
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 Veehmot · Oct 04, 2011 at 11:00 PM 4
Share

You should edit the title.

avatar image mangohack · Oct 08, 2011 at 12:11 AM 0
Share

why....................

avatar image Eric5h5 · Oct 08, 2011 at 01:28 AM 1
Share

Because it doesn't tell you anything about the question.

avatar image syclamoth · Oct 08, 2011 at 04:07 AM 0
Share

And because people won't take it seriously.

2 Replies

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

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
     }
 }

}

Comment
Add comment · Show 6 · 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 mangohack · Oct 03, 2011 at 03:59 AM 0
Share

i like you because you always answer my questions but can you gime me a example because im not good coding

avatar image aldonaletto · Oct 03, 2011 at 04:01 AM 0
Share

What is your enemy? A CharacterController? Post the current script you're using to move the enemy.

avatar image mangohack · Oct 03, 2011 at 04:02 AM 0
Share

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);
      
     }
 }
 
 

}

avatar image aldonaletto · Oct 03, 2011 at 04:38 AM 0
Share

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.

avatar image mangohack · Oct 03, 2011 at 04:46 AM 0
Share

ok take you time

Show more comments
avatar image
-1

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

Comment
Add comment · Show 4 · 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 mangohack · Oct 03, 2011 at 03:51 AM 0
Share

it doesn't work

avatar image mangohack · Oct 03, 2011 at 07:02 PM 0
Share

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

avatar image aldonaletto · Oct 04, 2011 at 03:02 AM 0
Share

@mangohack, UA ate the CharacterController type (it thought <CharacterController> was a tag). I fixed the answer, and now it must work.

avatar image mangohack · Oct 04, 2011 at 10:58 PM 0
Share

thankyouuuuuuuu thankyouuuuuuuu it workkkkkkkkkkkkkkkkkkkk im so happyyyyyyyyyyyyyyy thankyouuuuuuuu i love you you the BESTTTT everrrr lol

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

How can I ground enemies and use target transform? 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