Search Unity

Screen space to WolrdSpace problem!!! (300$ PRIZE!!!!!!)

Discussion in 'Scripting' started by myunity, Apr 8, 2010.

  1. myunity

    myunity

    Joined:
    Nov 7, 2009
    Posts:
    117
    Hi guys,

    I have a problem that is very urgent and i need it.if anybody solved my problem i give 300$ prize.

    i want simulate light gun in unity3d.i can get my info(x,y coordinate in pixel) from external device(gun) and every thing is right until this point.

    now when i get x,y i want instantiate a projectile in that coordinate and give a force or velocity that projectile goes straight.i did it but my projectile beacause of viewing system(camera) of unity and prespective when goes far it sounds that projectile goes to the center of screen.i do not know how can i add force to projectile that goese realy straight.

    in fact i want simulate light gun like old 2d sega games.but my game is 3d.

    please help.
     
  2. Ramen Sama

    Ramen Sama

    Joined:
    Mar 28, 2009
    Posts:
    561
    I have an idea what you want. I'll try a simple sample project and see if it works.
     
  3. Ramen Sama

    Ramen Sama

    Joined:
    Mar 28, 2009
    Posts:
    561
    Ok, figured it out. you need to use this code

    Code (csharp):
    1.  
    2. var ray = camera.ScreenPointToRay (Input.mousePosition);
    3.  
    i put this code in a new javascript file and attached it to the camera object.

    Code (csharp):
    1.  
    2. function Update () {
    3. var mray = camera.ScreenPointToRay (Input.mousePosition);
    4. Debug.DrawRay (mray.origin, mray.direction * 10, Color.yellow);
    5. print(Input.mousePosition);
    6. }
    7.  


    This will generate a ray along the XY coords of the mouse position. Input.mousePosition is a vector 3 consisting of the X Y position of the mouse and 0 for the third number. Bottom left corner is origin. You should be able to convert the data from your light gun to create a different vector 3 to supply the ScreenPointToRay function.

    Using this ray, you should be able to then create your projectiles along it.

    I've attached a screenshot. you can't see my mouse, but it's about where the red circle is.


    more info found here:
    http://unity3d.com/support/documentation/ScriptReference/Input-mousePosition.html
     

    Attached Files:

  4. brucostam

    brucostam

    Joined:
    Sep 6, 2009
    Posts:
    1
    Ramen Sama did a really good approach to work with the camera perspective.

    I already used some like this on my codes and problably would go for the same path.

    You can now use your projectile script to instantiate in the XY position you were already using, but now you add the force using the ray vector instead of the foward vector.

    And your projectile will fly following the ray.

    =D
     
  5. myunity

    myunity

    Joined:
    Nov 7, 2009
    Posts:
    117
    So thanks.

    I will test it and give you the result.
    if it was true i need your Account Number.
     
  6. Ramen Sama

    Ramen Sama

    Joined:
    Mar 28, 2009
    Posts:
    561
    :) well i have paypal. can use the email address listed in my profile
     
  7. myunity

    myunity

    Joined:
    Nov 7, 2009
    Posts:
    117
    how can i convert direction to quaternion for instantiate projectile?

    so thanks.
     
  8. Ramen Sama

    Ramen Sama

    Joined:
    Mar 28, 2009
    Posts:
    561
    Code (csharp):
    1.  
    2. //a prefab with a rigidbody
    3. var missle : GameObject;
    4.  
    5.  
    6. function Update () {
    7.  
    8. //create a ray towards the mouse position
    9. var mray = camera.ScreenPointToRay (Input.mousePosition);
    10.  
    11. //grab a distant point to use to calcuate where to aim the projectile
    12. distantPoint = mray.GetPoint (10);
    13.  
    14. //the direction of the ray
    15. var rDirection = mray.direction * 100;
    16.  
    17.  
    18.  
    19. //draw a line in the editor
    20. Debug.DrawRay (mray.origin, mray.direction * 10, Color.yellow);
    21.  
    22.  
    23.  
    24.  
    25.  
    26.  
    27. //fire a missle
    28. if (Input.GetKeyDown (KeyCode.Space)) {
    29. var myMissile = Instantiate (missle, transform.position, transform.rotation);
    30. //rotate the missle to look at the distant point
    31. myMissile.transform.LookAt(distantPoint);
    32.  
    33. //move the missle along the ray
    34. myMissile.rigidbody.velocity =rDirection;
    35.  
    36. //destory the missle after two seconds
    37. Destroy (myMissile, 2);
    38. }
    39.  
    40.  
    41. }
    42.  
    43.  
    44.  

    This does everything.

    The forums refuses to allow me to attach a zip file with a a project.

    heres a live demo
    http://ramensama.site88.net/Unity/missle.html
     
  9. myunity

    myunity

    Joined:
    Nov 7, 2009
    Posts:
    117
    So thanks my dear friend.i must test it on First Person Controller and give you result.

    if u can upload the project on the rapidshare/multiupload please.

    cheers...
     
  10. myunity

    myunity

    Joined:
    Nov 7, 2009
    Posts:
    117
    it does not work on First Person Controller correctly!!!
     
  11. myunity

    myunity

    Joined:
    Nov 7, 2009
    Posts:
    117
    ok.

    i solved that. :D
    i used this code:

    Code (csharp):
    1. Physics.IgnoreCollision(myMissile.collider,transform.root.collider);
     
  12. myunity

    myunity

    Joined:
    Nov 7, 2009
    Posts:
    117
    i tested it on First Person Controller.there is a problem.when i click on corners the projectile will digress.

    i think this is a bug from unity3d.what do you think?
    what must we do?

    thanks.
     
  13. Ramen Sama

    Ramen Sama

    Joined:
    Mar 28, 2009
    Posts:
    561
    works fine here.

    heres the project file. i changed the fire button to return since the player jumps when you press return.
    http://ramensama.site88.net/Unity/missle.unitypackage


    there's an issue with the bullet hitting the player object on startup, i just moved the camera up a bit. but you could simply change the start position of the bullet, or just add a collision ignore for the player.
     
  14. myunity

    myunity

    Joined:
    Nov 7, 2009
    Posts:
    117
    It does not open!!!

    please upload again.if you can package that in unity and then upload.

    thanks.
     
  15. Ramen Sama

    Ramen Sama

    Joined:
    Mar 28, 2009
    Posts:
    561
    Sorry, linked the wrong file. I've corrected it in the above post.
     
  16. myunity

    myunity

    Joined:
    Nov 7, 2009
    Posts:
    117
    thanks dear friend.

    i tested your project.it was true,but when i used Use Gravity for bullet,the bullet digressed at corners.
    when bullet have not gravity everything is true.

    what must we do dear friend?

    be sure i pay 50$ to you.but i need the project true.
    please help.
     
  17. Ramen Sama

    Ramen Sama

    Joined:
    Mar 28, 2009
    Posts:
    561
    With gravity on a bullet, the bullet must have small mass. plus a real bullet will move extremely fast and not be affect by gravity for some distance.

    So really, why in the world would you want your bullets to have gravity?
     
  18. myunity

    myunity

    Joined:
    Nov 7, 2009
    Posts:
    117
    it is not a game.it is a simulation.therefore i need gravity for bullet.

    what is the technical problem in that case?what must we do?

    thanks.
     
  19. Ramen Sama

    Ramen Sama

    Joined:
    Mar 28, 2009
    Posts:
    561
    I put gravity on mine and it seems to work fine. i don't know what issue you are having.
     
  20. myunity

    myunity

    Joined:
    Nov 7, 2009
    Posts:
    117
    i test it again.
     
  21. myunity

    myunity

    Joined:
    Nov 7, 2009
    Posts:
    117
    ok.i tested it again.but there is problem yet in corners and when i look up completely, the bullet will digress.

    i think this is a problem in viewing system in unity.

    what is your idea?

    thanks.
     
  22. Ramen Sama

    Ramen Sama

    Joined:
    Mar 28, 2009
    Posts:
    561
    I tried a few things and i think i figured out your issue. i think it's physics causing the bullet to digress. i think it's bouncing or sliding away from the Blocks.


    I aimed up at the bottom corener and it would fly away from the cube. this is most definately phsyics related. Besides, the physics sometimes doesn't detect a hit anyhow and goes thru the blocks.

    The bullet aways flys from the cursor no matter the angle.
     
  23. myunity

    myunity

    Joined:
    Nov 7, 2009
    Posts:
    117
  24. Ramen Sama

    Ramen Sama

    Joined:
    Mar 28, 2009
    Posts:
    561
    oh, i see, the bullet fly off to the side when shooting it straight. I'll see if any of my code is causing it, if not it's a unity issue.


    ::Edit::

    Well it seems to be a bug with gravity at high angles. This doesn't happen with the gravity is enabled, so it's an issue with that.
     
  25. myunity

    myunity

    Joined:
    Nov 7, 2009
    Posts:
    117
    so,what must we do now?
     
  26. Ramen Sama

    Ramen Sama

    Joined:
    Mar 28, 2009
    Posts:
    561
    Don't know. it seems like a unity bug. I sure can't fix that.
     
  27. myunity

    myunity

    Joined:
    Nov 7, 2009
    Posts:
    117
    i asked it before in Answers,and Duck answered me.if u can check that and vote the quastion to we get the answer sooner.

    so thanks.
     
  28. apple_motion

    apple_motion

    Joined:
    Jul 2, 2009
    Posts:
    169
    "the bullet will digress... " NO. well... it looks like digressed but it just a perspective distortion. That is no way to fix, since that is no wrong :)

    maybe you could change the camera matrix to correct some distortion for better viewing. However, it doesn't works on large amount of camera tilting.
     
  29. myunity

    myunity

    Joined:
    Nov 7, 2009
    Posts:
    117
    do you know any way?(code)

    thanks.
     
  30. apple_motion

    apple_motion

    Joined:
    Jul 2, 2009
    Posts:
    169
    hi myunity,

    The following 3 lines of code will "shear" the camera matrix in Y axis. That is similar to use the tilt/shift lens in the real world for taking architectural photo :)

    Code (csharp):
    1. function Start()
    2. {
    3.     var M:Matrix4x4 = Camera.main.projectionMatrix;
    4.     M[1,2] = 0.5; //  y-axis shearing
    5.     Camera.main.projectionMatrix = M;
    6. }
    7.  


    Hope those informations are helpful for you.

    Remark: M[0,2] is for x-axis shearing, in case you want to play with it too :)
     
  31. myunity

    myunity

    Joined:
    Nov 7, 2009
    Posts:
    117
    so thanks my freind,

    if you can please upload the complete code with comments.if you solved my problem i can pay prize to you.

    please help.
     
  32. apple_motion

    apple_motion

    Joined:
    Jul 2, 2009
    Posts:
    169
    Sorry that, I don't have the complete code, I only added 3 lines of code on top of Ramen Sama's version. You should give the prize to Ramen since he already answered your original question :)
     
  33. myunity

    myunity

    Joined:
    Nov 7, 2009
    Posts:
    117
    i tested your code.in direct view it have lesser problem.but when i look up and fire it have problem yet.even bullet's rotation have problem.

    do u know another idea?
    i do not know why nobody can not solve this!!!!

    i can pay prize to u and Ramen Sama if the problm solved.beacuse i need that.

    thanks.
     
  34. apple_motion

    apple_motion

    Joined:
    Jul 2, 2009
    Posts:
    169
    Since you keep asking more question :p

    It is better to upload your project and design document to here. so that, we don't need to guess want you really want.
     
  35. myunity

    myunity

    Joined:
    Nov 7, 2009
    Posts:
    117
    thanks for guide.

    i do it.i get a video and upload the project.

    now,can u solve that problem?
     
  36. myunity

    myunity

    Joined:
    Nov 7, 2009
    Posts:
    117
  37. myunity

    myunity

    Joined:
    Nov 7, 2009
    Posts:
    117
    any answer!!!
     
  38. myunity

    myunity

    Joined:
    Nov 7, 2009
    Posts:
    117
    No body can not help me?
     
  39. Ramen Sama

    Ramen Sama

    Joined:
    Mar 28, 2009
    Posts:
    561
    It's a bug of some type.

    it doesn't seem related to the project, so you'd need to file some sorta bug report, or just not use gravity. honestly, i don't know why you need to use projectiles. you've never stated exactly why you can't just use a raycast to determine a hit and call it a day.

    Maybe you could make another thread about the gravity issue as your original problem of shooting based on mouse position has been solved. Now it's a unity issue that it seems no one has encountered.

    My only thought is to turn of gravity in the inspector and do some "fake" gravity. just drop the Y setting over time and see if it still will digress. If so, it really is a perspective distortion of some type.
     
  40. Flynn

    Flynn

    Joined:
    May 24, 2009
    Posts:
    311
    I agree with apple_motion - I think what you are dealing with is distortion. The human eye sees almost 180 degrees, but all we pay attention to is the first.... 60? Perhaps if you set the camera angle to 180, and then cropped the camera to show only the first 60 degrees (it won't be exact, but at least close), you may get a less distorted version. But for all I know, you may just end up INCREASING the distortion.


    Oh, and to test if it is actually a gravity bug, try adding a trail renderer to the bullet, firing, and then observing the trail from within the editor. If it followed the same curve as it seemed to in the game view, then you may have a gravity problem. But if it is straight, or at least natural considering the amount of gravity, then you are dealing with distortion.

    If you are having a gravity problem, try turning down the mass of the bullet (If you haven't allready).

    I'm just a little curious what this simulation is for?
     
  41. myunity

    myunity

    Joined:
    Nov 7, 2009
    Posts:
    117
    so thanks dear friends.

    it is a light gun simulation.

    i think if i uncheck the Use Gravity and use fake gravity with code be good.what is your idea?
     
  42. Flynn

    Flynn

    Joined:
    May 24, 2009
    Posts:
    311
    Well, if it's a light gun simulation, I doubt that Earth's gravity would be enough to genuinely affect the bullet. ;) (If by light you mean photons, not lightweight.) In other words, go ahead and turn it off.

    You may however still find this digression exists - which means that your issues are in distortion, not gravity.

    If you are goign for a light gun, it would behave more like a laser, not a bullet - However, plasma guns are more or less what you see in Star Wars.

    If you want a plasma gun, you can go ahead and apply fake gravity, but it will likely end up having the same effect.
     
  43. Ramen Sama

    Ramen Sama

    Joined:
    Mar 28, 2009
    Posts:
    561
    But yeah, to sum it up, this is an issue caused by perspecitive distortion. This is normal with all cameras based on their lens size, this is no different in unity. the only way to "fix" it is to set your scene to orthographic view. or.. not to use gravity.

    http://en.wikipedia.org/wiki/Perspective_distortion_(photography)

    more information can be found there.
     
  44. apple_motion

    apple_motion

    Joined:
    Jul 2, 2009
    Posts:
    169
    yeah... and scale all the objects by 1/distance :idea: I did this trick in my video project many time 8)
     
  45. Disati

    Disati

    Joined:
    Oct 5, 2009
    Posts:
    111
    This kinda doesn't anything have to do with the main topic, but i have a question, a newbie question that is.

    How did you make the cube rotate when it's hit by a bullet?

    (Marked with a red circle)

    [/img]


    It can't be a rigidbody because it will fall due to forces of gravity, i think. But if i won't make it rigidbody it won't act like one. So that's my question, i hope you will be able to tell me


    Best regards
     
  46. Taintspore

    Taintspore

    Joined:
    Sep 29, 2009
    Posts:
    185
    Make it a rigidbody and uncheck UseGravity or set rigidbody.useGravity equal to false in code.

    Cheers
     
  47. Ramen Sama

    Ramen Sama

    Joined:
    Mar 28, 2009
    Posts:
    561
    Disati wrote:

    It can't be a rigidbody because it will fall due to forces of gravity, i think.


    i used a hinge joint between an invisible rigidbody and the other.
     
  48. myunity

    myunity

    Joined:
    Nov 7, 2009
    Posts:
    117
    So thanks my dear friends.
    i do your recommends and give you the result.

    but if u can help me more or solve this problem i pay good money.because i need it!
     
  49. myunity

    myunity

    Joined:
    Nov 7, 2009
    Posts:
    117
    viewing system(Camera) have problem!!!
     
  50. myunity

    myunity

    Joined:
    Nov 7, 2009
    Posts:
    117
    I think that when the gravity is ON, will effect on FOV.

    because in Edit view the bullet goes straight while in Game view the bullet will digress when gravity is on.and when the gravity is off the buulet goes straight in both view.

    in addition i think ProjectionMatrix command help me.becaus it can help me that i have my FOV.

    i do not know what must i do exactly?no body can not help me!!!