Jump after detection range.

I have a cube that shoots and I have a cube to jump over the missile when it detects a bullet.

I’m newbie. I was looking for solutions, but I can not find anything.

If I could also ask for an explanation.

What question/problem are you actually having? This script doesn’t have anything that would make something jump. You could try using rb.AddForce(transform.up * jumpHeight) but I’m not really sure what you want based on the question.

My cube have to detect bullet. Then jump over it. The jump also has a set time(duration).

I do not really know how to do it. Simple IF is enough? All I know is that I need AddForce to jump.

Well there are a hand-full of ways to detect if something is around an object. The most simple, probably, is to have a trigger collider handing out in front of your cube. When something enters your collider then you can call the AddForce() function from your rigidbody.

void OnTriggerEnter(Collider other)
{
    if (other.gameObject.tag == "Obstacle")
        cube.rigidbody.AddForce(transform.up * jumpHeight);
}

How i can set range for collider? Like this?

col.radius = 3f;

Yes but cos.radius change my height. Now my cube fly. :frowning:

I see three basic problems.

  1. You have nothing that “detects” objects.
  2. You have nothing that “jumps”
  3. You are using transform.position on a Rigidbody.

(seems that the code was taken down)

So, to tackle problem 1, you need either a trigger (as suggested) or do a OverlapSphere call to get everything in a sphere. Only do this while you are not jumping.

problem 2, is where you need to use the power of the Rigidbody to help you. Stop using transform.position +=…

problem 3. And here is why it is a problem. You are forcing the position of the body forward numerically. this means that there could be a possibility of things not happening the way you want them to. (physics wise)

    // the target that we are going for
    public Transform target;
    // the speed which we move
    public float speed = 5;
    // the radius of our collider
    public float radius = 1;
    // how far out to detect bullets
    public float detectionDistance = 2;

    // must set the rigidbody in Start
    private Rigidbody rigidbody;
    // the current velocity
    private Vector3 velocity;

    void Start()
    {
        // collect the rigidbody
        this.rigidbody = gameObject.GetComponent<Rigidbody>();
    }

    void Update()
    {
        // null checks to make sure we are valid
        if (this.target == null || this.rigidbody == null) return;

        // set the velocity's y to zero so that it does not affect movement.
        this.velocity.y = 0;
        // collect the direction/distance from us to the target
        var targetVelocity = Vector3.Scale(this.target.position - transform.position, new Vector3(1, 0, 1));
        if (targetVelocity.sqrMagnitude <= 1)
        {
            // stop moving if we are within 1 unit of our target
            targetVelocity = Vector3.zero;
        }
        else
        {
            // change the distance to a velocity
            targetVelocity = targetVelocity.normalized * this.speed;
        }
        // use this to gradually change velocity.
        this.velocity = Vector3.MoveTowards(this.velocity, targetVelocity, 10 * Time.deltaTime);

        // reset the velocity to the current rigidbody velocity.
        this.velocity.y = rigidbody.velocity.y;
        // check to see if we are on the ground
        if (this.IsGrounded())
        {
            // if so, we need to see if bullets are coming towards us.
            // collect all colliders that are near us
            var colliders = Physics.OverlapSphere(transform.position, detectionDistance);
            // find all the colliders that are...
            var bullets = colliders
                // bullets
            .Where(c => c.transform.root.gameObject.tag == "Bullet")
                // have rigidbodies
            .Where(c => c.transform.root.gameObject.GetComponentInChildren<Rigidbody>() != null)
                // who's rigidbodies are heading towards us
            .Where(c => Vector3.Dot(
                c.transform.root.gameObject.GetComponentInChildren<Rigidbody>().velocity.normalized,
                (transform.position - c.transform.root.position).normalized
            ) > 0.9f);
            // if we have anything left... jump
            if (bullets.Any())
            {
                this.velocity.y = 10;
            }
        }
        // apply the velocity back to the rigidbody
        this.rigidbody.velocity = this.velocity;
    }

    // simple grounding
    bool IsGrounded()
    {
        return Physics.Raycast(transform.position, -Vector3.up, radius + 0.1f);
    }

Of course… not tested, but the theory should be sound.