- Home /
how to calculate force direction on mouse up
Hello
I want to addforce on mouseup in the direction where I am swyping. I coded like listed below but not working everytime. Anybody can tell me where i am mistaking..??
I did like this.
 if (Input.GetMouseButtonDown (0)) 
 {
     startPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 }
 
 if (Input.GetMouseButtonUp (0)) 
 {
     endPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
     GiveForce();
 }
 
 private void GiveForce()
 {
     Vector2 diff = (startPosition - endPosition);
     transform.rigidbody2D.AddForce (-diff * speed, ForceMode2D.Force);
 }
               Comment
              
 
               
              Answer by vikingfabian-com · Sep 01, 2014 at 07:04 AM
You need to normalize the diff vector.
 private void GiveForce()
     {
 
     Vector2 diff = (startPosition - endPosition);
 if (diff != Vector2.zero)
 {
     diff.normalize(); //not sure about the spelling
     transform.rigidbody2D.AddForce (-diff * speed, ForceMode2D.Force);
 }
    }
 
 
thanks for ur reply. but still the same issue is there.
like, suppose i change my swype direction, it is not working..!!!!
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                