- Home /
Enemy vechicle reversing on slopes when it should follow
Hey! I'm pretty new to unity, and I've encounterred a problem. It's hard to explain, but I'll give it a go.
Basically, I have a script telling the enemy to follow me when within a certain range, brake when close enough, and reverse when very close (reverse if it is within stop distance - 10). The script works well usually, but I have this ramp with a flat top (like a hill sort of thing) and it is broken at some points. I approach the ramp and when I am at the very top of the slope, I am within range. The tank starts following me up until about 40m away from me. Then, it starts reversing but the wheel texture spins forward, the way it does when the DriveFwd() function is called. it reverses at an extreme speed, way higher than it's max speed for any gear. I have no idea what is causing this behaviour. Also, if I'm a bit higher up the slope so I'm almost touching the flat top of it, the AI acts normally. I think it might be some physics bug, but I'm not sure.
Here is the code (AI): #pragma strict var Player:Transform; var Move:EnemyMove; var dist:float; var maxTurnDist = 100; var followRange = 80; var stopDist = 45; var turnSpeed:float = 30; var following = false; var DmgdFollowRange = 100; var NormalFollowRange = 80;
function Start () {
}
function Update () {
var pos = transform.position;
var playerpos = Player.position;
dist = Vector3.Distance(pos, playerpos);
var dir = Player.position - transform.position;
if(dist <= maxTurnDist)
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(dir), turnSpeed * Time.deltaTime);
}
if(dist <= followRange && dist > stopDist)
{
Move.DriveFwd();
}
if(dist <= stopDist && dist >= stopDist - 10||dist > followRange)
{
Move.Brake();
Move.StopAccel();
}
if(dist <= followRange)
{
following = true;
}
else
{
following = false;
}
if(dist < NormalFollowRange)
{
followRange = NormalFollowRange;
}
if(dist < stopDist - 10)
{
Move.DriveBack();
Move.StopFwd();
}
}
function TakeDamage(dmg:int) {
followRange = DmgdFollowRange;
}
and the movement:
#pragma strict
var Accel = 25;
var Decel = -20;
var handbrake = 25;
var gear = 1;
var turn:float = 10;
var baseturn:float = 10;
var Wheel1 : WheelCollider;
var Wheel2 : WheelCollider;
var Wheel3 : WheelCollider;
var Wheel4 : WheelCollider;
var Mesh1 : Transform;
var Mesh2 : Transform;
var Mesh3 : Transform;
var Mesh4 : Transform;
var speed:float;
var maxspeed:float;
var g1maxspd:float = 5;
var g2maxspd:float = 10;
var g3maxspd:float = 20;
var g1fric:float = 0.35;
var g2fric:float = 0.05;
var g3fric:float = 0.005;
var g1turnamp:float = 1;
var g2turnamp:float = 0.5;
var g3turnamp:float = 0.2;
var isturning = false;
function Start () {
}
function Update () {
speed = rigidbody.velocity.sqrMagnitude;
Wheel1.brakeTorque = 0;
Wheel2.brakeTorque = 0;
isturning = false;
if(speed >= g1maxspd && gear == 1)
{
gear = 2;
}
if(speed >= g2maxspd && gear == 2)
{
gear = 3;
}
if(speed < g2maxspd && gear == 3)
{
gear = 2;
}
if(speed < g1maxspd && gear == 2)
{
gear = 1;
}
if(gear == 1)
{
Wheel1.forwardFriction.stiffness = g1fric;
Wheel2.forwardFriction.stiffness = g1fric;
Wheel3.forwardFriction.stiffness = g1fric;
Wheel4.forwardFriction.stiffness = g1fric;
Wheel1.sidewaysFriction.stiffness = g1fric;
Wheel2.sidewaysFriction.stiffness = g1fric;
Wheel3.sidewaysFriction.stiffness = g1fric;
Wheel4.sidewaysFriction.stiffness = g1fric;
maxspeed = g1maxspd;
turn = baseturn * g1turnamp;
}
if(gear == 2)
{
Wheel1.forwardFriction.stiffness = g2fric;
Wheel2.forwardFriction.stiffness = g2fric;
Wheel3.forwardFriction.stiffness = g2fric;
Wheel4.forwardFriction.stiffness = g2fric;
Wheel1.sidewaysFriction.stiffness = g2fric;
Wheel2.sidewaysFriction.stiffness = g2fric;
Wheel3.sidewaysFriction.stiffness = g2fric;
Wheel4.sidewaysFriction.stiffness = g2fric;
maxspeed = g2maxspd;
turn = baseturn * g2turnamp;
}
if(gear == 3)
{
Wheel1.forwardFriction.stiffness = g3fric;
Wheel2.forwardFriction.stiffness = g3fric;
Wheel3.forwardFriction.stiffness = g3fric;
Wheel4.forwardFriction.stiffness = g3fric;
Wheel1.sidewaysFriction.stiffness = g3fric;
Wheel2.sidewaysFriction.stiffness = g3fric;
Wheel3.sidewaysFriction.stiffness = g3fric;
Wheel4.sidewaysFriction.stiffness = g3fric;
maxspeed = g3maxspd;
turn = baseturn * g3turnamp;
}
if(speed > maxspeed)
{
Wheel1.brakeTorque = handbrake;
Wheel2.brakeTorque = handbrake;
}
}
function DriveFwd () {
Wheel3.motorTorque = Accel;
Wheel4.motorTorque = Accel;
Mesh1.transform.Rotate(0,-3,0);
Mesh2.transform.Rotate(0,-3,0);
Mesh3.transform.Rotate(0,-3,0);
Mesh4.transform.Rotate(0,-3,0);
}
function StopAccel () {
Wheel3.motorTorque = 0;
Wheel4.motorTorque = 0;
Wheel1.motorTorque = 0;
Wheel2.motorTorque = 0;
}
function StopFwd () {
Wheel3.motorTorque = 0;
Wheel4.motorTorque = 0;
}
function DriveBack () {
Wheel1.motorTorque = Decel;
Wheel2.motorTorque = Decel;
Mesh1.transform.Rotate(0,3,0);
Mesh2.transform.Rotate(0,3,0);
Mesh3.transform.Rotate(0,3,0);
Mesh4.transform.Rotate(0,3,0);
}
function Brake () {
Wheel1.brakeTorque = handbrake;
Wheel2.brakeTorque = handbrake;
}
function TurnLeft () {
rigidbody.AddTorque(Vector3.up * -turn);
isturning = true;
}
function TurnRight () {
rigidbody.AddTorque(Vector3.up * turn);
isturning = true;
}
function SetGear (Gear:int) {
gear = Gear;
}
It's a bit messy, so sorry about that. Any tips on optimisation are also welcome :)