i have weird issue in 2d unity game, able reduce following core problem/minimal reproducing test case. follow these steps reproduce (unity 5.1.1f1):
- create player object (cube) @ location
(0,0,0). - remove
boxcollidercomponent. - attach following
c#script, unity automatically add required components , thereby make rigidbody collider. - set
iskinematicflag. - add cube scene @ location
(2,0,0). - remove
boxcollidercomponent , addboxcollider2d. makes cube static collider. - set
istriggerflag. - run scene.
expected behavior:
player cube accelerates towards other cube , stops moving once touches it.
observed behavior:
player cube accelerates towards other cube, continues moving @ constant speed.
additional implementation details:
moved objects translating transform , didn't use rigidbodies @ because didn't need collision detection. do, want rigidbodies. dived online resources , found out i'm supposed use rigidbody.moveposition() rather transform.translate() or transform.position. changed script, , above error appeared.
going transform.position fixes issue, that's not solution, involves bad practice which, according read, produces significant cpu loads.
failed attempts solve:
- switching
update(),time.deltatimedidn't make difference. - i tried not returning in
update(), instead resettingtimestep0whilestopset. no change. - fiddling inspector , doing stuff freezing position on rigidbody or setting player objects trigger had no effect @ all. changing on rigidbody component while game running (after collision) makes cube stop immediately. literally anything, setting mass 0.
- i tried setting
velocity0, resulting in no change. make sense sinceupdate()skipped entirely (i checkeddebug.log()way).
so @ point, i'm down barely 30 lines of code , still have no idea what's causing this. since involved objects static trigger collider , kinematic rigidbody collider, both no physics materials, there should nothing makes thing move once flag set. move.
simplecontroller2d.cs
using unityengine; using system.collections; [requirecomponent (typeof (boxcollider2d), typeof (rigidbody2d))] public class simplecontroller2d : monobehaviour { public vector3 velocity = vector3.zero; private transform thistransform; private rigidbody2d thisrigidbody; public bool stop = false; void awake () { thistransform = getcomponent<transform> (); thisrigidbody = getcomponent<rigidbody2d> (); } void fixedupdate() { float timestep = time.fixeddeltatime; // temporarily stored ease of access if (stop) { return; // freeze on hit } velocity.x += timestep; // accelerate /* add second slash (/) toggle between transform , rigidbody thistransform.position += velocity * timestep; /*/ thisrigidbody.moveposition ((vector3)thisrigidbody.position + velocity*timestep); //*/ } void ontriggerenter2d(collider2d col) { stop = true; } }
solution
this bug in unity 5.1.1f1 , fixed in patch release 5.1.1p2 , later.
get here: http://unity3d.com/unity/qa/patch-releases?version=5.1
what happened?
you can reduce problem single moveposition call. moveposition uses physics engine move object. therefore unity calculates velocity necessary reach target position within next physics update. version 5.1.1f1 fails reset velocity 0 after reaching position, object continue moving calculated velocity.
Comments
Post a Comment