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:
function OnCollisionEnter (collision : Collision) {
if (collision.gameObject.CompareTag("Player")) {
// logic to push the player
}
}
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.
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.
using UnityEngine;
using System.Collections;
public class DetectPlayer : MonoBehaviour {
void OnTriggerEnter(Collider col){
if (col.gameObject.tag == "Player"){
col.gameObject.SendMessage("bCrystalPickup");
}
}
}
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??
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
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…
#pragma strict
var distance : float;
var target : Transform;
var lookAtDistance = 20.0;
var attackRange = 15.0;
var pushPower : float = 4.0;
var moveSpeed = 5.0;
var damping = 8.0;
private var isItAttacking = false;
function Update ()
{
distance = Vector3.Distance(target.position, transform.position);
if(distance < lookAtDistance)
{
isItAttacking = false;
lookAt ();
}
if(distance < attackRange)
{
attack ();
}
}
function lookAt () // this makes the animal turn towards the player (target)
{
var rotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
}
function OnControllerColliderHit(hit:ControllerColliderHit){
var body : Rigidbody = hit.collider.attachedRigidbody;
if(hit.moveDirection.y < -0.3) // this makes animal push sideways only
return;
// this next script Aims the animal toward the Player (target)
var pushDir : Vector3 = Vector3(hit.moveDirection.x,0,hit.moveDirection.z);
body.velocity = pushDir * pushPower; // this gives the animal pushing power against player
}
function attack () // this makes animal go after player and attack
{
isItAttacking = true;
transform.Translate(Vector3.forward * moveSpeed *Time.deltaTime);
}
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.
var distance : float;
var target : Transform;
var lookAtDistance = 20.0;
var pushRange = 15.0;
var pushPower : float = 7.0;
var moveSpeed = 5.0;
var damping = 8.0;
private var Pushing = false;
function Update ()
{
distance = Vector3.Distance(target.position, transform.position);
if(distance < lookAtDistance)
{
Pushing = false;
lookAt ();
}
if(distance < pushRange)
{
push ();
}
}
function lookAt () // this makes the AI turn towards the player (target)
{
var rotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
}
function OnControllerColliderHit(hit:ControllerColliderHit){
var body : Rigidbody = hit.collider.attachedRigidbody;
var pushDir : Vector3 = Vector3(hit.moveDirection.x,0,hit.moveDirection.z); // aims AI toward player
body.velocity = pushDir * pushPower; // supposed to give the AI pushing power against player
}
function push () // this makes AI go after player and push
{
Pushing = true;
transform.Translate(Vector3.forward * moveSpeed *Time.deltaTime);
}