Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Issue with transform.position and duplicating an object.

Discussion in 'Scripting' started by slykrooper, Mar 14, 2021.

  1. slykrooper

    slykrooper

    Joined:
    Oct 27, 2018
    Posts:
    10
    Hi, title isn't a very good descriptor but im kind of new to this so im not sure how to word it. My issue is that right now Im sending out a ray from a player and whenever it collides with a certain object and I press W, the player will change its transform.position to that of the object the ray hits and that works great no problems there. The issue is when I duplicate the object for the ray to hit and the player hits the duplicate, the transform.position of the player is set to the original position of the object.

    I have the Sphere gameobject put into my public variable in the unity inspector and yeah. Any help would be greatly appreciated along with any corrections to my code would help a lot.

    Here is the pretty basic code I have.

    Code (CSharp):
    1. public float speed = 1f;
    2.     float maxDistance = 1f;
    3.     public GameObject Sphere;
    4.  
    5.     // Start is called before the first frame update
    6.     void Start()
    7.     {
    8.  
    9.     }
    10.  
    11.     // Update is called once per frame
    12.     void Update()
    13.     {
    14.         RaycastHit hit;
    15.         Ray originF = new Ray(transform.position, Vector3.forward);
    16.  
    17.         Debug.DrawRay(transform.position, Vector3.forward * maxDistance);
    18.  
    19.         if (Input.GetKeyDown(KeyCode.W))
    20.         {
    21.             if(Physics.Raycast(originF, out hit, maxDistance))
    22.             {
    23.                 if(hit.collider.isTrigger)
    24.                 {
    25.                     transform.position = new Vector3(Sphere.transform.position.x, Sphere.transform.position.y, Sphere.transform.position.z);
    26.                     Debug.Log("Hit");
    27.                 }
    28.                
    29.             }
    30.         }
    31.     }
    32. }
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    In line 3 you define a GameObject Sphere, which you fill through the inspector. Then in Update you check whether you hit something, and if thats the case, you teleport to the location of THAT sphere. Instead, you seemingly want to teleport to the position of the hit object (possibly only if it is a sphere). You can get the hit object, and thus its position, from the raycasthit (hit) itself. Teleport to that, instead of your manually set sphere. If you want the raycast to only work on specific objects (spheres, for example), then create a new layer, put all those objects in there, and only cast the ray on that layer.

    Raycasthit documentation: https://docs.unity3d.com/ScriptReference/RaycastHit.html
    You are interrested in the "transform" property, through which you can get the position of the hit object. You could also work with the hit-position itself if thats desired.
     
  3. slykrooper

    slykrooper

    Joined:
    Oct 27, 2018
    Posts:
    10
    You are beautiful my dude. Thank you for the help
     
    Yoreki likes this.