I know this can be frustrating—it was for me too! After you blow up the dam, buy the race car (I don't think the specific one matters) and the race stunt track. Then get in the car and complete Woody's ring-collecting race quest. Once that's done, you'll finally unlock the ray gun. To recap: buy the race car and track, beat the racing quest in town, and a carriage will deliver the UFO gun to you.