i'm using following script control ball doesn't want. our 3d game played in landscape mode home button (or bottom) of device in right hand. tilting (not turning) device left should make ball roll left, tilting right should make roll right. tilting device down (top of device going down) should make ball roll faster , tilting device upward should slow down. don't want ball indefinitely accelerate either.
the code below wants device held straight opposed laying flat , moves ball turning device not tiling it.
void fixedupdate() { // player movement in mobile devices // building of force vector vector3 movement = new vector3(-input.acceleration.x, 0.0f, -input.acceleration.z); // adding force rigidbody var move = movement * speed * time.deltatime; rigidbdy.addforce(move); }
for tilting problem need choose other (-input.acceleration.x, 0.0f, -input.acceleration.z);
, in example in documentation (-input.acceleration.y, 0.0f, input.acceleration.x);
tilt controls.
for max speed issue, add check rigidbdy.velocity.magnitude > maxspeed
in code , cap value if maxed.
public float maxspeed; void fixedupdate() { // player movement in mobile devices // building of force vector vector3 movement = new vector3(-input.acceleration.y, 0.0f, input.acceleration.x); // adding force rigidbody var move = movement * speed * time.deltatime; rigidbdy.addforce(move); //limits max speed if(rigidbdy.velocity.magnitude > maxspeed) { rigidbdy.velocity = rigidbdy.velocity.normalized * maxspeed; } }
that cause velocity capped whatever value have set maxspeed
in inspector.
Comments
Post a Comment