Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I want to add a constant horizontal force, just like gravity to a projectile or a sphere moving in 3d space, which will make the projectile or ball or sphere or whatever be influenced by this force so that it will curl in the direction of the constant horizontal force. I have this code which will add force to the object on swiping the screen, but it doesnt drift/curl or whatever you may call it:

var anglex : float; 
var angley : float; 
var elevationAngle : Vector3; 

function FixedUpdate () {
    if(Input.touchCount>0 && Input.GetTouch(0).phase == TouchPhase.Ended) 
               {
                    var touchdeltaposition: Vector2 = Input.GetTouch(0).deltaPosition;
                    anglex = touchdeltaposition.x * Time.deltaTime *2.0;
                    angley = touchdeltaposition.y * Time.deltaTime *5.0;
                    Debug.Log(angley);
                    elevationAngle = Vector3(anglex,angley,0); 
                    rigidbody.velocity = transform.TransformDirection(Vector3(anglex,angley,angley ));
                    var elevation : Vector3 = Quaternion.Euler(elevationAngle) * transform.forward; 
                    rigidbody.AddRelativeForce(elevation);
                  }
           }

I feel this is simple, but I'm just not able to turn the crank.

share|improve this question
    
Describe a bit more what's happening. Are your Debug statements being executed? Is the object being affected at all? Have you tried some visual debugging to ensure you're applying the force in the correct direction? –  Byte56 Jul 9 at 14:27
    
@Byte56 Actually I didn't test it then, but when I tested it now, it doesn't seem to work, toucheddeltaposition is zero regardless of the swipe position, direction, magnitude... What am I doing wrong? –  gameOne Jul 10 at 0:22

2 Answers 2

You add a constant force by doing pretty much what you're doing already.

The problem you're likely having is a debugging one, or your expecting more force to be added then you're actually adding. Keep in mind that if you want the force to affect the object, you'll probably want to apply a larger force when the touch has ended.

Remember that how much the object moves from the force is dependent on the mass of the object, so you'll probably want to ensure the mass of the object isn't too high as well.

share|improve this answer
    
Actually I didn't test it then, but when I tested it now, it doesn't seem to work, toucheddeltaposition is zero regardless of the swipe position, direction, magnitude... What am I doing wrong? I added a factor of 100 to the force, but that makes the sphere always roll forward regardless if the swipe gesture. –  gameOne Jul 10 at 0:40
    
Attach the debugger, check out the values of your variables. Specifically, I'd poke around Input.GetTouch to see if there are any other touches available and what the value of deltaPosition is, and check the calculations along the way. –  user45357 Jul 10 at 15:28

All you need is the object to have a velocity. Then, every tick you add your force (gravity) to it's velocity, and add your velocity to it's position.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.