Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

how to raycast from enemy to the player

Discussion in 'Scripting' started by marixPrototype, Jul 23, 2014.

  1. marixPrototype

    marixPrototype

    Joined:
    Jul 23, 2014
    Posts:
    3
    i want to make it so the enemy goes to attack mode only when he sees the player by raycasting at his position so if it will hit a wall the enemy will not attack only if the player can be "seen" by the enemy
    so how do i script it ?
    (java script)
    thanks....
     
  2. raycosantana

    raycosantana

    Joined:
    Dec 23, 2012
    Posts:
    319
    This is an extract from my AI script, its in C# but it should be pretty similar to what you would do on javascript

    Code (CSharp):
    1. private Ray sight;
    2.  
    3. //.................................
    4.  
    5. void FixedUpdate(){
    6. sight.origin = new Vector3(transform.position.x, transform.position.y + 0.5f, transform.position.z);
    7.     sight.direction = transform.forward;
    8.     RaycastHit rayHit;
    9.     Vector3 fwd = transform.TransformDirection(Vector3.forward);
    10.  
    11.     if (Physics.Raycast(sight, out rayHit, chaseRange)){
    12.         Debug.DrawLine(sight.origin, rayHit.point, Color.red);
    13.         if (rayHit.collider.tag == "Player"){
    14.             CurrentState = EnemyStates.following;
    15.         }
    16.  
    17.         if (rayHit.collider.tag != "Player" && rayHit.collider.tag != "Enemy"){
    18.             CurrentState = EnemyStates.idle;
    19.         }
    20.     }
    21. }
    Study it thoroughly and apply your conclusions into your own script.

    Also study the scripting reference

    http://docs.unity3d.com/ScriptReference/Physics.Raycast.html
     
  3. marixPrototype

    marixPrototype

    Joined:
    Jul 23, 2014
    Posts:
    3
    i tried it and unity said "A namespace can only contain types and namespace declarations"
     
  4. raycosantana

    raycosantana

    Joined:
    Dec 23, 2012
    Posts:
    319
    Well that's because that's not the whole script, like I said its an extract, I give it to you so you can see how raycast works but I ain't gonna code the script for you, you have to code it yourself or you wont learn anything.
     
  5. marixPrototype

    marixPrototype

    Joined:
    Jul 23, 2014
    Posts:
    3
    i changed it to java it works thanks but if i want it to raycast to the player i mean whereever the player will be the enemy will try to raycast at him
     
  6. rrh

    rrh

    Joined:
    Jul 12, 2012
    Posts:
    331
    You set direction to be player's position minus enemie's position.
     
  7. raycosantana

    raycosantana

    Joined:
    Dec 23, 2012
    Posts:
    319

    Thats right. This is also from my script:

    Code (CSharp):
    1. Quaternion rotation = Quaternion.LookRotation(target.position - transform.position);
    2.         transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
    target is the player object. damping its a float variable, the greater its value the faster the enemy with rotate towards the player