Search Unity

Bug Teleporting to a projectile - Moving the player error

Discussion in 'Scripting' started by UTCLuisMoseley, Nov 29, 2022.

  1. UTCLuisMoseley

    UTCLuisMoseley

    Joined:
    Sep 29, 2022
    Posts:
    6
    I've been working on a project involving instantiating an orb which then teleports the player to its location when hitting any object I tag with "floor". I am relatively new to programming and so any help is appreciated.
    Code (CSharp):
    1.   void OnCollisionEnter(Collision col)
    2.     {
    3.         if (col.gameObject.tag == "Floor")
    4.         {
    5.             script.canShoot = true;
    6.             isTriggered = true;
    7.             player.transform.position = teleportLocation;
    8.             Destroy(gameObject);
    9.             Debug.Log("hit floor");
    10.         }
    11.  
    12.     }
    My "teleportLocation" is just the orbs constant position as of currently.


    Currently the issue I am facing is with moving the player, I have substituted the player for a cube which did work when setting the position.
    This is my script for instantiating the projectile:

    Code (CSharp):
    1.   Vector3 dir = mouse.transform.position - transform.position;
    2.         if (!disabled)
    3.         {
    4.  
    5.              GameObject instance = Instantiate(orbPrefab, orbShooter.transform.position, orbPrefab.transform.rotation);
    6.              instance.GetComponent<Rigidbody>().AddForce(dir * power);
    7.              instance.transform.position = new Vector3(0, 0, 0);
    8.  
    9.         }
    The only way I have been able to teleport the player to the orb successfully was through this script:

    Code (CSharp):
    1. public class teleport : MonoBehaviour
    2. {
    3.     public Vector3 tpPos;
    4.     public GameObject testOrb;
    5.     // Start is called before the first frame update
    6.     void Start()
    7.     {
    8.      
    9.     }
    10.  
    11.     void Update()
    12.     {
    13.         tpPos = testOrb.transform.position;
    14.         if (Input.GetKeyDown(KeyCode.E))
    15.         {
    16.             transform.position = tpPos;
    17.         }
    18.     }
    19. }
    I could move the player to an orb (which was not an instantiated object but was instead a normal game object moved by clicking) when I press "E" and this seemed to work.
    The problem doesn't seem to lie within the collision detection or transform positions but instead the instantiation. It would be helpful if anyone knows a way which could teleport my player to the instantiated orb.
     
  2. UTCLuisMoseley

    UTCLuisMoseley

    Joined:
    Sep 29, 2022
    Posts:
    6
    Found the solution which just involved creating a method inside of the script attached to the player which changes the players transform position to the orbs. The method is then called inside of the orb script when it collides with the floor. The problem seemed to be with where the teleport was called inside of the script for some reason, hopefully others will understand if they have this issue also..
     
  3. TzuriTeshuba

    TzuriTeshuba

    Joined:
    Aug 6, 2019
    Posts:
    185
    Firstly, great first post! using code tags is helpful and you provided a lot of information relatively concisely. It would be helpful, for me at least, to know which code snippets belong to which class and possibly a step by step explanation of the use case. I will point out that you seem to be affecting an object with a rigidbody attached via AddForce() and by directly setting its transform.position. this will generally cause issues like yours. if you want to move an object using forces, then dont transform.position. If you absolutely need to, then use myRigidBody.MovePosition(). If you need a rigidbody and want to set the transform.position, but do not plan on using forces, then set the rigidbody to kinematic.
     
    UTCLuisMoseley likes this.
  4. UTCLuisMoseley

    UTCLuisMoseley

    Joined:
    Sep 29, 2022
    Posts:
    6

    Thanks for the reply, I have finished with this project and ended up fixing the issue I had. The problem seemed to rely on where the position was set. I changed it by setting the position in the script attached to said object and then calling it through a method in another script. I'll be sure to post more in the future anyway!