collision with ai problem

I want to create basic physics between two colliding objects, If the players momentum is higher, the AI should fly backwards. If they AIs momentum is higher, the player should fly backward. All the AI does is try and collide with you.

Here is the AI code:

//attack target
var target : Transform; 

//move speed
var moveSpeed = 3; 

//speed of turning
var rotationSpeed = 3; 

// distance to attack
var attackThreshold = 3; 

// distance to start chasing
var chaseThreshold = 10;

// distance when gives up 
var giveUpThreshold = 20; 

// delay attacks
var attackRepeatTime = 1; 
var explosionPrefab : Transform;
//var countenermyhits = 0;

private var chasing = false;
private var attackTime = Time.time;

//enemry
var myTransform : Transform; 

function Awake()
{ myTransform = transform; }

function Start()
{
//target the player 
target = GameObject.FindWithTag("Player").transform; }

// function OnCollisionEnter(collision : Collision) {
// countenermyhits++;
// if(countenermyhits == ){
// Destroy (gameObject);
// } }

function Update () { 

// check distance to target 
var dist= (target.position - myTransform.position).magnitude; 

if (chasing) { 

    //rotate to look at the target 
    myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime); 

    //move towards the player 
    myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime; 

// too far away from target: 
if (dist > giveUpThreshold) { 
    chasing = false; 
} 

// attack if close enough and within time 
if (dist < attackThreshold && Time.time > attackTime) { // Attack
    Instantiate(explosionPrefab,transform.position, transform.rotation); 
    //Destroy (gameObject); 
    attackTime = Time.time + attackRepeatTime; 
} 

} else { // start chasing if target comes close 
if (dist < chaseThreshold) 
{ 
chasing = true; 
} 
}
}

Each object has a mass and a velocity, but when then Ai hits me with a higher momemtum, it flys off and not me. Any help would be appreciated!

ALSO, the ai seems to fly, and Id like it so stay on the ground...

Thanks!

Are you using the Unity physics system at all? i.e assigning a rigidbody to your objects?

How are you controlling your player? Unless you are using physics and rigidbodies only to control your player it will stay static when sometihng with force hits it. So if you are using the character controller it overrides the physics

2 Answers

2

Take a look at Rigidbody.isKinematic

A quick way of achieving what you want can be to set isKinematic = true on the body you don't want to fly backwards. If that isn't satisfactory for your needs do a search on absorbing velocity, many similar questions have been asked before.

To stop your enemy from 'flying' you can constrain their movement to the X and Z axes only. (Assuming by flying you mean its Y position increases)

You've commented off part of your script with //. (the OnCollisionEnter part). Is that meant to happen? It will mean that part won't work.

lol, hopefully he will pick that up :p