Quantcast
Channel: Questions in topic: "addforce"
Viewing all articles
Browse latest Browse all 1264

AddForce Jump/Movement speeds multiplying together problem

$
0
0
Hey, When getting touch input from iOS screen, it seems that if I press a movement Rect, along with jump at the very same time. The MovementSpeed, and JumpSpeed seem to add together (As in the character goes flying). See script below: function Update(){ var touches = new Array(); //are we grounded? var hit : RaycastHit; if (Physics.Raycast (transform.position, Vector3.down, hit)) { var distanceToGround = hit.distance; if(hit.distance<2.5){ grounded = true; } if(hit.distance>2.5){ grounded = false; } } touches = new Array(); leftdir = Rect(0,0, 150, 150); rightdir = Rect(150,0, 150, 150); jumpbox = Rect (Screen.width-150, 0, 150, 150); gravbox = Rect (Screen.width-150, 170, 150, 150); fingerCount = 0; for (var touch : Touch in Input.touches) { if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled){ fingerCount++; } for (var k = 0; k < Input.touchCount; ++k) { if (Input.GetTouch(k).phase == TouchPhase.Began) { if (jumpbox.Contains(touch.position)){ jumpKeyPressed = true; } } } if(leftdir.Contains(touch.position)){ leftkey = true; } if(rightdir.Contains(touch.position)){ rightkey = true; } } } } function FixedUpdate () { if (leftkey == true){ var backwardInputVelocity = -turningRigidbody.GetForwardDirection() * movementSpeed; //rigidbody.velocity = new Vector3(backwardInputVelocity.x, rigidbody.velocity.y, backwardInputVelocity.z); rigidbody.AddForce (backwardInputVelocity * Time.deltaTime); } leftkey = false; if (rightkey == true){ var forwardInputVelocity = turningRigidbody.GetForwardDirection() * movementSpeed; //rigidbody.velocity = new Vector3(forwardInputVelocity.x, rigidbody.velocity.y, forwardInputVelocity.z); rigidbody.AddForce (forwardInputVelocity * Time.deltaTime); } rightkey = false; } if (jumpKeyPressed){ //Debug.Log("Jumping"); rigidbody.velocity.y += jumpSpeed; } jumpKeyPressed = false; } I really don't have a clue how to stop this. Whats the usual way to combat this kind of thing? Thanks,Bobble.

Viewing all articles
Browse latest Browse all 1264

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>