I made a script to pick up cubes in my game. It is supposed to send out a raycast and then parent the cube to my player and set it’s transform so it’s in the center of the camera instead of off to the side or something. When I hit the cube with my raycast, it parents to the camera but then my player position changes to 0,0 and I fall through my world.
Here’s the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Laser : MonoBehaviour
{
public Camera PlayerCamera;
void Update()
{
RaycastHit hit;
if (Input.GetKeyDown(KeyCode.E))
{
if (Physics.Raycast(transform.position, transform.forward, out hit, 4.0F))
{
if (hit.transform.tag == "cube")
{
hit.transform.parent = PlayerCamera.transform;
hit.transform.gameObject.transform.position = new Vector3(0, 0, 0);
}
}
}
}
}
I’m trying to achieve a pickup cube effect like in Portal or Half-Life. Do I have the right idea, or should I be doing this some other way?