c# - Unity3D - Preventing 3D Character from Changing Direction Mid-air -


i'm second-year student studying video game programming , i've been struggling problem little while now, wondering if guys offer best suggestion on how fix issue.

i have 3d character able walk, run, jump, double jump, , rotate face cursor. however, noticed sort of bug character able control movement in mid-air.

for example, if run holding left shift + w , jump, you're able stop moving forward , start strafing left in mid-air.

here code:

void update () {     // turns off animations while in air     if (isinair)     {         animator.setbool("running", false);         animator.setbool("walking", false);     }      if (input.getkey(keycode.w))     {         // if character not in air turn on walk animation         if (!isinair)         {             animator.setbool("walking", true);         }         transform.translate(vector3.forward * movementspeed * time.deltatime);     }     else if (input.getkey(keycode.s))     {         // if character not in air turn on walk animation         if (!isinair)         {             animator.setbool("walking", true);         }         transform.translate(vector3.back * movementspeed * time.deltatime);     }      if (input.getkey(keycode.a))     {         // if character not in air turn on walk animation         if (!isinair)         {             animator.setbool("walking", true);         }         transform.translate(vector3.left * movementspeed * time.deltatime);     }     else if (input.getkey(keycode.d))     {         // if character not in air turn on walk animation         if (!isinair)         {             animator.setbool("walking", true);         }         transform.translate(vector3.right * movementspeed * time.deltatime);     }      if (input.getkey(keycode.leftshift))     {         // if character not in air turn on run animation          //  , change character's movementspeed         if (!isinair)         {             movementspeed = runspeed;             animator.setbool("running", true);         }     }     else     {         // if character not in air reset movement speed         //  , turn off run animation.         if (!isinair)         {             movementspeed = walkspeed;             animator.setbool("running", false);         }     }      // when key released, turn off movement animations     // not cause issue since each movement if statement turns animation on     if (input.getkeyup(keycode.w) || input.getkeyup(keycode.s) || input.getkeyup(keycode.a) || input.getkeyup(keycode.d))     {         animator.setbool("walking", false);         animator.setbool("running", false);     }     #endregion      // jumping     if (input.getbuttondown("jump") && jumpcounter != 2)     {         jumpcounter++;         isinair = true;         myrigidbody.addforce(new vector3(0, jumpforce));         if (jumpcounter == 1)         {             firstjumpstamp = time.time;         }     }      if(physics.raycast(groundcheckray, groundcheckraydistance) && time.time > firstjumpstamp + 0.5f)     {         jumpcounter = 0;         isinair = false;     } } 

i have removed code not relating character movement.

is able give me suggestion on method i'd able use make work?

i'm not asking code fix problem, suggestion...

i feel need learn on own, need point me in right direction.

if not understand part of code, feel free ask me , i'll gladly show i'm attempting it. :) maybe know better way write lines example.

(i try avoid using input.getaxis though because find harder make movement when don't have 100% control on it)

i can suggest re-evaluate requirements , compare you've written. there slight differences between you've described , you've done in code. first of want character able move either left or right , not these 2 @ same time, same forward , backward.

to can use input.getaxis instead of input.getkey you're using right now.


a second thing can suggest use local vector3 calculate force , apply character. can then, based on vector3 value, set animator variables.


your main problem consists of movement technique used. since you've decided use transform.translate instead of using rigidbody move character, have calculate position delta on frames.
assume character start @ position [ x:1.00f, y: 0.00f, z: 1.00f ] , move in direction [ x: 0.30f, y: 0.00f, z: 0.70f ] on frame 1 , decided jump. have grab previous position ( 1 before frame changed ) [ x:1.00f, y: 0.00f, z: 1.00f ] , subtract current position [ x:1.30f, y: 0.00f, z: 1.70f ]. return vector equals previous position delta ( [ x: 0.30f, y: 0.00f, z: 0.70f ] ). add position current position , should remain same course ( direction ).
not include friction character move same speed in same direction time when in mid air.

even though haven't asked code, it's easier describe in in plain english ( @ least me :) ) so, here's example code:

// value indicating position before change vector3 previousposition;  void update () {     // user input vector     vector3 horizontal = vector3.right * input.getaxis("horizontal");     vector3 vertical = vector3.forward * input.getaxis("vertical");      bool isrunning = input.getkey(keycode.leftshift);     float speedmodifier = isrunning ? runspeed : walkspeed;      // add these inputs     vector3 currentvelocity = (horizontal + vertical);      // check if player in mid air     if (isinair)     {         // if is, previous , current positions         // add calculate distance between frames         // , normalize movement direction         vector3 currentposition = transfor.position;         vector3 direction = (currentposition - previousposition).normalize();         // apply movement direction "currentvelocity"         currentvelocity = direction * speedmodifier * time.deltatime;     }     else     {         // user not in mid air can calculate         // position change         currentvelocity = currentvelocity.normalize() * speedmodifier * time.deltatime;          if ( currentvelocity != vector3.zero )         {             animator.setbool("walking", !isrunning);             animator.setbool("running", isrunning);         }     }      // current position ( before changes )     // should copied previous position     // calculate in next frame update     previousposition = currentposition;      // translation     transform.translate(currentvelocity); } 

additional info: still recommend switch transform.translate rigidbody , "re-use" it's velocity when character in mid air. not require calculate position delta each frame, making more easier use.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -