Search Unity

Moving Objects

Discussion in 'Getting Started' started by moeman1989, Nov 13, 2018.

  1. moeman1989

    moeman1989

    Joined:
    Sep 26, 2018
    Posts:
    10
    Hey Guys and Girls.
    I have two issues that I need help with. Firstly I should say I come from a lua background in another engine.
    So what I want to be able to do is two things. I want to be able to pick info from an object and I want to be able to convert 2d camera coordinates to 3d world coordinates using something similar to project/unproject commands.
    The purpose is;
    I want to be able to assign a value like type = sceneobject to a tree. Then in my fps controller say pick info with a radius of X from the crosshair. If object exists with type sceneobject and windows key E is pressed. pickinfo.entity position is = to crosshair position but also x distance in front of the camera. If E is pressed again drop object.
    Essentially saying that if I stand close enough to an object of the correct type I can pick it up and move it. I need to figure this out because I am rebuilding a game in Unity. Alot of the stuff I did in that game relies on this concept. Because later on I need to be able to add things like rotate on Y axis.
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Have you gone through the Unity tutorials (see the "Learn" link at the top of this page)? The things you're asking for are fairly simple, but may be easier to pick up from the tutorial videos than from quick answers here.

    But here are some quick answers anyway, so at least you know what to watch for:
    • Convert screen coordinates to world coordinates with Camera.ScreenToWorldPoint.
    • But what you probably actually want here is Camera.ScreenPointToRay, so you can use Physics.Raycast to find out what it hits in the world.
    • Once you've found something you want to pick up, you'll keep a reference to it in a script, and probably convert back and forth between camera-relative coordinates and world coordinates using Transform.TransformPoint and .InverseTransformPoint.
    Good luck!
    - Joe
     
    moeman1989 likes this.
  3. moeman1989

    moeman1989

    Joined:
    Sep 26, 2018
    Posts:
    10
    Thanks mate.