Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by eeveelution · Mar 30, 2015 at 09:14 AM · javascriptphysicsfollowslope

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 :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Why wont this ladder script work? 1 Answer

Cube To Follow Waypoints - Help! Att: Picture 1 Answer

Physics driven cars without Wheel Colliders. Is it practicable? 1 Answer

Need help with physics 1 Answer

How I can make a simple follow AI in Unity 5? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges