- Home /
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;
}
}
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).
nothing now nothing is being called. Log is also not showing up in the console window.
This is the code after modifications. And the Game screen if it helps.
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;
}
}
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.
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;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613170645im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Enemy Jump AI Problem 0 Answers
How to make a Raycast enemy? 0 Answers
Raycast from camera not hitting Terrain 1 Answer