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

Trying to Attack my player by PUSHING him, Just Push...No damage, etc...

Discussion in 'Scripting' started by ARRCOMM1, Nov 22, 2013.

  1. ARRCOMM1

    ARRCOMM1

    Joined:
    Aug 19, 2013
    Posts:
    44
    Need to tell the Animal to advance towards Player and Push Player.
    I am missing something here.


    $AntelopeScene.jpg $Inspector_Settings.jpg

    Code (csharp):
    1. #pragma strict
    2.  
    3. var pushPower : float = 10.0;
    4.  
    5.  function OnControllerColliderHit (hit : ControllerColliderHit) {
    6.  
    7.     var body : Rigidbody = hit.collider.attachedRigidbody;
    8.  
    9.    // no rigidbody
    10.  
    11.    if (body == null || body.isKinematic)
    12.       return;
    13.  
    14.  
    15.    if (hit.moveDirection.y < -0.3)
    16.      return;
    17.  
    18.  // push objects to the sides
    19.  
    20.     var pushDir : Vector3 = Vector3 (hit.moveDirection.x, 0, hit.moveDirection.z);
    21.  
    22.   body.velocity = pushDir * pushPower;
    23. }
    24.  
    25.  
    26.  
    27. function Start () {
    28.  
    29. }
    30.  
    31. function Update () {
    32.  
    33. }
     
  2. roooob

    roooob

    Joined:
    Jun 20, 2013
    Posts:
    24
    It looks like you're trying to make the Antelope act on the Player as though the Antelope has some sort of character controller on it. The OnControllerColliderHit function only triggers on an object that has a character controller attached to it, meaning that this script would have to be on the Player and would push the Antelope, rather than the other way around. I would suggest using the following instead:

    Code (csharp):
    1.  
    2. function OnCollisionEnter (collision : Collision) {
    3.     if (collision.gameObject.CompareTag("Player")) {
    4.         // logic to push the player
    5.     }
    6. }
    7.  
     
  3. ARRCOMM1

    ARRCOMM1

    Joined:
    Aug 19, 2013
    Posts:
    44
    Ok. I will need for the Animal to SEE the Player and go after him. So shouldn't I have some sort of Raycast Trigger on the animal to LOOK for the Player?
    This is something I saw a year ago in a book I had.
     
  4. CodeMonke234

    CodeMonke234

    Joined:
    Oct 13, 2010
    Posts:
    181
    Make animal a navmesh agent is one way
     
  5. BlackMantis

    BlackMantis

    Joined:
    Feb 7, 2010
    Posts:
    1,475
    Is nav mesh still a Pro feature ?

    A trigger that when entered references the player and player distance would be a good start. You def don't want to reference the player all the time so on exit remove the player reference.

    Using LookAt should get u turning, then once the distance is reached a little transform forward or CharacterController move should do the trick.
     
  6. ARRCOMM1

    ARRCOMM1

    Joined:
    Aug 19, 2013
    Posts:
    44
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DetectPlayer : MonoBehaviour {
    5.    
    6.     void OnTriggerEnter(Collider col){
    7.      if (col.gameObject.tag == "Player"){
    8.         col.gameObject.SendMessage("bCrystalPickup");
    9.         }
    10.     }
    11. }

    I used this and was SUCCESSFUL in getting the sound file to play when my Player entered the (detection) Trigger area of the animal.
    I had the sound file and Pickup script in an Inventory file. This "SendMessage" was only used to test this code to find out if it worked. I am trying to CALL a Function in a script that will make the Animal move toward the PLAYER and PUSH him around on contact with the Players' Collider. Reminder, note I am in C#, not JS.

    Anybody have a successful code for an Attacker to CHASE the player??
     
  7. ARRCOMM1

    ARRCOMM1

    Joined:
    Aug 19, 2013
    Posts:
    44
    Looking in my Unity help folders and my Unity Game Development Essentials by Will Goldstone and the Unity Script search.
    Tried to find a "LookAt" C# script and explanation of how to address this, but I did not find anything.

    Do you have a reference I can view/read somewhere??
    Thank you
     
  8. ARRCOMM1

    ARRCOMM1

    Joined:
    Aug 19, 2013
    Posts:
    44
    I do NOT want the ATTACK. I just want the animal to PUSH me around. BUT THIS WORKS, SO FAR, for getting the animal to detect me and then when I reach a certain distance, start coming after me. If I run away and get out of range, the animal quits chasing me.
    PERFECT.... NOW TO GET THE ANIMAL FROM ATTACKING ME and KEEPING THE ANIMAL STAYING ON THE GROUND.
    He attacks me about the head and turns sideways and makes me jump 30+ feet in the air. All Collision stuff.
    Have a few ideas. My Pushing script items not working correctly.

    Here is CODE so, far...

    Code (csharp):
    1. #pragma strict
    2.  
    3.         var distance : float;
    4.         var target : Transform;    
    5.         var lookAtDistance = 20.0;
    6.         var attackRange = 15.0;
    7.         var pushPower : float = 4.0;
    8.         var moveSpeed = 5.0;
    9.         var damping = 8.0;
    10.         private var isItAttacking = false;
    11.        
    12.        
    13.         function Update ()
    14.         {
    15.         distance = Vector3.Distance(target.position, transform.position);
    16.  
    17.         if(distance < lookAtDistance)
    18.         {
    19.         isItAttacking = false;
    20.            lookAt ();
    21.         }  
    22.            if(distance < attackRange)
    23.         {
    24.         attack ();
    25.         }
    26.     }
    27.  
    28.      function lookAt ()      // this makes the animal turn towards the player (target)
    29.     {
    30.     var rotation = Quaternion.LookRotation(target.position - transform.position);  
    31.     transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);  
    32.     }
    33.  
    34.     function OnControllerColliderHit(hit:ControllerColliderHit){
    35.             var body : Rigidbody = hit.collider.attachedRigidbody;
    36.            
    37.             if(hit.moveDirection.y < -0.3)                      //  this makes animal push sideways only
    38.             return;
    39.                                                // this next script Aims the animal toward the Player (target)
    40.             var pushDir : Vector3 = Vector3(hit.moveDirection.x,0,hit.moveDirection.z);  
    41.  
    42.             body.velocity = pushDir * pushPower;       // this gives the animal pushing power against player
    43.             }
    44.    
    45.     function attack ()                                 //  this makes animal go after player and attack
    46.     {
    47.         isItAttacking = true;
    48.         transform.Translate(Vector3.forward * moveSpeed *Time.deltaTime);  
    49.     }
     
  9. ARRCOMM1

    ARRCOMM1

    Joined:
    Aug 19, 2013
    Posts:
    44
    My AI is moving after me, but passes through me and circles around my head and is about 2 feet above ground as soon as he starts chasing me.
    Never does PUSH me.

    Any help with this code? I have been looking through my book and the Scripting Reference, but it makes me dizzy now.

    Code (csharp):
    1. var distance : float;
    2.         var target : Transform;    
    3.         var lookAtDistance = 20.0;
    4.         var pushRange = 15.0;
    5.         var pushPower : float = 7.0;
    6.         var moveSpeed = 5.0;
    7.         var damping = 8.0;
    8.         private var Pushing = false;
    9.  
    10.       function Update ()
    11.         {
    12.       distance = Vector3.Distance(target.position, transform.position);
    13.  
    14.         if(distance < lookAtDistance)
    15.         {
    16.         Pushing = false;
    17.            lookAt ();
    18.         }  
    19.            if(distance < pushRange)
    20.         {
    21.       push ();
    22.         }
    23.     }
    24.         function lookAt ()      // this makes the AI turn towards the player (target)
    25.     {
    26.             var rotation = Quaternion.LookRotation(target.position - transform.position);  
    27.         transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);  
    28.     }
    29.         function OnControllerColliderHit(hit:ControllerColliderHit){
    30.             var body : Rigidbody = hit.collider.attachedRigidbody;
    31.             var pushDir : Vector3 = Vector3(hit.moveDirection.x,0,hit.moveDirection.z);   //  aims AI toward player
    32.             body.velocity = pushDir * pushPower;       // supposed to give the AI pushing power against player
    33.             }
    34.        function push ()      //  this makes AI go after player and push
    35.    {
    36.         Pushing = true;
    37.         transform.Translate(Vector3.forward * moveSpeed *Time.deltaTime);
    38.  }