c# - How do I stop the rotation of my gameobject when it reaches a certain point on its y-axis? -
i'm working on simple baseball game. i'm trying have player able swing bat backwards "charge up" power, speak, , when button released swing bat forward @ speed equal amount of power stored up.
this , good, problem need stop motion of bat when reach point on y-axis, , i'm bit unsure how go doing cannot tell stop rotating after set time bat won't reach front point @ same time every time due difference in speed each swing might have.
what i'm trying if(reached 266 on y-axis stop rotating)
, i'm unsure of how this.
anyway here code i've written far:
public int rotatespeed = 50; public float amountofpowerchargedup = 0; void update() { if (input.getmousebutton(0)) { amountofpowerchargedup += 5f; transform.rotate(vector3.up * rotatespeed * time.deltatime); debug.log(amountofpowerchargedup); } if (!input.getmousebutton (0)) { transform.rotate(vector3.down * amountofpowerchargedup * time.deltatime); } } private void ongui() { gui.label (new rect (50, 15, 250, 25), "amountofpowerchargedup: " + amountofpowerchargedup); }
if rotating amount of degrees, should use slerp
or rotatetowards
vector3 targetdir = (target.position - transform.position).normalized; // step size equal speed times frame time. float step = speed * time.deltatime; vector3 newdir = vector3.rotatetowards(transform.up, targetdir, step, 0.0); transform.rotation = quaternion.lookrotation(newdir)
Comments
Post a Comment