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 hiback · Nov 03, 2014 at 08:54 PM · collisionraycastenemy aienemyai

Enemy not jumping using Raycast

I am building an endless running game in which the main character is being followed by a bull. so the bull should jump platforms like the main character but instead its flying right way. Any help

public class S : MonoBehaviour

{ public LayerMask GroundLayer;

 private Transform tr;

 bool jumped;

 GameObject player,platform;

 void Start () {

     player = GameObject.FindGameObjectWithTag("Player");
     tr = transform;
     jumped = false;
 }
 

public float maxspeed=8f;

 void FixedUpdate () {
     
             //float move = Input.GetAxis ("Horizontal");

             rigidbody2D.velocity = new Vector2 (1 * maxspeed, rigidbody2D.velocity.y);

             if (Physics.Raycast (tr.position, -tr.up, 20f) && !jumped) { // if no ground below

                     if (jumped) {
                             Debug.Log ("JUMP");
                     } else { 
                             jumped = true;
                             Debug.Log ("False Jump");
                     }
             } 
             else 
     
             {
                     Debug.Log ("else");
                     jump ();
             }

     //jumped = false;
     //if (!hit.collider) {
     //    jump();
     //}
     
     
     
     
 }    void Update () {

     //transform.LookAt (player.transform);
     //transform.Translate (0,0,8*Time.deltaTime);
 
 }

 void jump()
 
 {

     rigidbody2D.AddForce (new Vector2 (0f, 450f));
     jumped=false;
 }

}

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

Answer by robertbu · Nov 03, 2014 at 08:57 PM

You've structured your logic so that your call to Jump() on line 20 is in the 'else' clause of the Raycast(). This means that your object will jump any time the Raycast() fails. In other words, any time the ground is further than 20 unites away. Seems to me that you want to move line 20 to line 13 and get rid of the whole else clause (lines 16 - 21).

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 hiback · Nov 04, 2014 at 06:59 AM 0
Share

Ok i will try that and report. Anyways Thanks

avatar image hiback · Nov 04, 2014 at 07:06 AM 0
Share

nothing now nothing is being called. Log is also not showing up in the console window.

avatar image hiback · Nov 04, 2014 at 07:28 AM 0
Share

This is the code after modifications. And the Game screen if it helps. alt text

public class S : $$anonymous$$onoBehaviour { public Layer$$anonymous$$ask GroundLayer; private Transform tr; bool jumped; GameObject player,platform; void Start () {

     player = GameObject.FindGameObjectWithTag("Player");
     tr = transform;
     jumped = false;
 }
 

public float maxspeed=8f;

 void FixedUpdate () {
     
             //float move = Input.GetAxis ("Horizontal");

             rigidbody2D.velocity = new Vector2 (1 * maxspeed, rigidbody2D.velocity.y);

             if (Physics.Raycast (tr.position, -tr.up, 20f) && !jumped) { // if no ground below

                     if (jumped) {
                             Debug.Log ("JU$$anonymous$$P");
                     } else { 
                             jumped = true;
                             jump ();
                             Debug.Log ("False Jump");
                     }
             } 
             
     //jumped = false;
     //if (!hit.collider) {
     //    jump();
     //}
     
     
     
     
 }    void Update () {

     //transform.LookAt (player.transform);
     //transform.Translate (0,0,8*Time.deltaTime);
 
 }

 void jump()
 
 {

     rigidbody2D.AddForce (new Vector2 (0f, 450f));
     jumped=false;
 }

}

avatar image hiback · Nov 04, 2014 at 12:36 PM 0
Share

I want the bull to jump on the platform automatically.

avatar image
0

Answer by hiback · Nov 04, 2014 at 03:31 PM

More Simplified Question.And the Game screen if it helps. I want the bull to jump on the platforms automatically. alt text

This is the Code.

using UnityEngine; using System.Collections;

public class S : MonoBehaviour {

 private Transform tr;
 bool jumped;
 GameObject player;
     public float maxspeed=8f;
 void Start () {

     player = GameObject.FindGameObjectWithTag("Player");
     tr = transform;
     jumped = false;// How Should i use the flag to make it jump at appropraitae time
 }
 
      void FixedUpdate () {
     
         

             rigidbody2D.velocity = new Vector2 (1 * maxspeed, rigidbody2D.velocity.y);

             if (!Physics.Raycast (tr.position, -tr.up, 20f)) { // if no ground below
                             jump ();
                             Debug.Log ("Bull Jump");
                     }
         
 }    void Update () {

 }

 void jump()
 
 {

     rigidbody2D.AddForce (new Vector2 (0f, 450f));
     jumped=true;
 }

}

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

2 People are following this question.

avatar image avatar image

Related Questions

Enemy Jump AI Problem 0 Answers

How to make a Raycast enemy? 0 Answers

Raycast from camera not hitting Terrain 1 Answer

raycasts only working with IsTrigger colliders? 1 Answer

OnTriggerEnter not working for NavMesh 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