Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Jump after detection range.

Discussion in 'Scripting' started by Pablo787, Jul 19, 2017.

  1. Pablo787

    Pablo787

    Joined:
    Jul 19, 2017
    Posts:
    3
    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.
     
    Last edited: Jul 19, 2017
  2. capnjake

    capnjake

    Joined:
    Nov 12, 2013
    Posts:
    53
    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.
     
  3. Pablo787

    Pablo787

    Joined:
    Jul 19, 2017
    Posts:
    3
    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.
     
  4. capnjake

    capnjake

    Joined:
    Nov 12, 2013
    Posts:
    53
    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.

    Code (csharp):
    1.  
    2. void OnTriggerEnter(Collider other)
    3. {
    4.     if (other.gameObject.tag == "Obstacle")
    5.         cube.rigidbody.AddForce(transform.up * jumpHeight);
    6. }
    7.  
     
  5. Pablo787

    Pablo787

    Joined:
    Jul 19, 2017
    Posts:
    3
    How i can set range for collider? Like this?
    Code (CSharp):
    1. col.radius = 3f;
    Yes but cos.radius change my height. Now my cube fly. :(
     
    Last edited: Jul 19, 2017
  6. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    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)

    Code (csharp):
    1.  
    2.     // the target that we are going for
    3.     public Transform target;
    4.     // the speed which we move
    5.     public float speed = 5;
    6.     // the radius of our collider
    7.     public float radius = 1;
    8.     // how far out to detect bullets
    9.     public float detectionDistance = 2;
    10.  
    11.     // must set the rigidbody in Start
    12.     private Rigidbody rigidbody;
    13.     // the current velocity
    14.     private Vector3 velocity;
    15.  
    16.     void Start()
    17.     {
    18.         // collect the rigidbody
    19.         this.rigidbody = gameObject.GetComponent<Rigidbody>();
    20.     }
    21.  
    22.     void Update()
    23.     {
    24.         // null checks to make sure we are valid
    25.         if (this.target == null || this.rigidbody == null) return;
    26.  
    27.         // set the velocity's y to zero so that it does not affect movement.
    28.         this.velocity.y = 0;
    29.         // collect the direction/distance from us to the target
    30.         var targetVelocity = Vector3.Scale(this.target.position - transform.position, new Vector3(1, 0, 1));
    31.         if (targetVelocity.sqrMagnitude <= 1)
    32.         {
    33.             // stop moving if we are within 1 unit of our target
    34.             targetVelocity = Vector3.zero;
    35.         }
    36.         else
    37.         {
    38.             // change the distance to a velocity
    39.             targetVelocity = targetVelocity.normalized * this.speed;
    40.         }
    41.         // use this to gradually change velocity.
    42.         this.velocity = Vector3.MoveTowards(this.velocity, targetVelocity, 10 * Time.deltaTime);
    43.  
    44.         // reset the velocity to the current rigidbody velocity.
    45.         this.velocity.y = rigidbody.velocity.y;
    46.         // check to see if we are on the ground
    47.         if (this.IsGrounded())
    48.         {
    49.             // if so, we need to see if bullets are coming towards us.
    50.             // collect all colliders that are near us
    51.             var colliders = Physics.OverlapSphere(transform.position, detectionDistance);
    52.             // find all the colliders that are...
    53.             var bullets = colliders
    54.                 // bullets
    55.             .Where(c => c.transform.root.gameObject.tag == "Bullet")
    56.                 // have rigidbodies
    57.             .Where(c => c.transform.root.gameObject.GetComponentInChildren<Rigidbody>() != null)
    58.                 // who's rigidbodies are heading towards us
    59.             .Where(c => Vector3.Dot(
    60.                 c.transform.root.gameObject.GetComponentInChildren<Rigidbody>().velocity.normalized,
    61.                 (transform.position - c.transform.root.position).normalized
    62.             ) > 0.9f);
    63.             // if we have anything left... jump
    64.             if (bullets.Any())
    65.             {
    66.                 this.velocity.y = 10;
    67.             }
    68.         }
    69.         // apply the velocity back to the rigidbody
    70.         this.rigidbody.velocity = this.velocity;
    71.     }
    72.  
    73.     // simple grounding
    74.     bool IsGrounded()
    75.     {
    76.         return Physics.Raycast(transform.position, -Vector3.up, radius + 0.1f);
    77.     }
    78.  
    Of course.. not tested, but the theory should be sound.