Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How to get Object's data if you look at it

Lodeon003Lodeon003 Member
edited November 2020 in Programming

Hello, im not very good at programming in unity, and I don't understand how to get an object's data or script if I'm looking at it...

So I wanted to make an ability that allowed you, if you click a button, to teleport to a "teleportation point" positioned around the map, if you are looking at it.

I thought about putting some gameObjects (teleportation point) with a "teleport to" script around the map and put in the player script a raycast. If the raycast from the player finds the "teleport to" script on the teleportation point and you are looking at it, it sets your position to that teleportation point's position (teleporting you there)

I don't know how to access the "teleportation point that you are looking at" 's position... Any ideas how to?

Best Answer

  • COOLCODERCOOLCODER Member
    edited December 2020 Accepted Answer


    you have to the give a tag to the teleportation point

    public tranform playerbody;

    use raycast and get the hit variable Raycasthit hit;

    brackeys have a full tutorial based shooting and raycast.

    and check if (input.GetMouseButtonDown(0)

    {

    playerbody.position = hit.point;

    }

    sorry put && hit.transform.tag == "teleportationpoint") in the raycast shoot if statement

Answers

  • Oh right, I knew how to use raycasts but I didn't know I could take the position directly to the hit variable... Thanks a lot!!

  • there was a mistake i edited it.

Sign In or Register to comment.