- Home /
New Unity Input System Touch Controls - Destroying GameObjects On Mobile
Here's the issue: I wanna destroy gameobjects after I "discover" them.
What I Have Done: I put an interactables script on my object that sets FX active when the player gets close and collides with a trigger. I wanted to use a raycast on the screen so that the player can tap on the object and collect it. I'd like to destroy the object before adding it as a variable to inventory.
Raycasting: I've researched raycasting and, after throwing a raycast on my camera, I was able to trigger raycasts (using debug.log and debug.drawray), but I don't know how to connect raycasts to the new Unity input system's touch stuff. On top of that, my cubes aren't recognizing the raycasts either when I used the "OnColliderEnter" method. I haven't yet found a YT tutorial that explains how to connect raycasts from one script to triggers and scripts on another separate gameobject. On top of that, all of the YT videos I've seen so far just show how to use the new input system for movement, etc, using gamepad/keyboards, but none really talk about destroying gameobjects. Any tips?
-curious hobbyist
----> the script that I have been using so far (Samyam script) ---> you can see that at the bottom, I was trying out the inputtouch stuff, but obviously it isn't working because I must either be using the old system or I'm not connecting it properly to the new input touch system... v.v Thank you in advance for any help!
using UnityEngine; public class PlayerController : MonoBehaviour {
private PlayerInputControls playerInput;
private Transform cameraMain;
private Transform child;
private CharacterController controller;
private Vector3 playerVelocity;
private bool groundedPlayer;
[SerializeField] private float playerSpeed = 2.0f;
[SerializeField] private float jumpHeight = 1.0f;
[SerializeField] private float gravityValue = -9.81f;
[SerializeField] private float rotationSpeed = 4f;
private void Awake()
{
playerInput = new PlayerInputControls();
controller = GetComponent<CharacterController>();
}
private void OnEnable()
{
playerInput.Enable();
}
private void OnDisable()
{
playerInput.Disable();
}
private void Start()
{
cameraMain = Camera.main.transform;
child = transform.GetChild(0).transform;
}
void Update()
{
//moving & jumping
groundedPlayer = controller.isGrounded;
if (groundedPlayer && playerVelocity.y < 0)
{
playerVelocity.y = 0f;
}
Vector2 movementInput = playerInput.PlayerMain.Move.ReadValue<Vector2>();
Vector3 move = (cameraMain.forward * movementInput.y + cameraMain.right * movementInput.x);
move.y = 0f;
controller.Move(move * Time.deltaTime * playerSpeed);
// Changes the height position of the player..
if (playerInput.PlayerMain.Jump.triggered && groundedPlayer)
{
playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
Debug.Log("Space is pressed");
}
playerVelocity.y += gravityValue * Time.deltaTime;
controller.Move(playerVelocity * Time.deltaTime);
if (movementInput != Vector2.zero)
{
Quaternion rotation = Quaternion.Euler(new Vector3(child.localEulerAngles.x, cameraMain.localEulerAngles.y, child.localEulerAngles.z));
child.rotation = Quaternion.Lerp(child.rotation, rotation, Time.deltaTime * rotationSpeed);
}
if ((Input.touchCount > 0) && (Input.GetTouch(0).phase == TouchPhase.Began))
{
Ray raycast = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
RaycastHit raycastHit;
if (Physics.Raycast(raycast, out raycastHit))
{
Debug.Log("Something hit");
Destroy(gameObject);
}
}
} }
![alt text][1] [1]: /storage/temp/187138-proto3.png
Answer by frknerstr · Oct 06, 2021 at 07:39 AM
Hi ,
If you want use "OnCollisionEnter()", "OnTriggerEnter()" or "Ray" etc. you should add Collider and Rigidbody components to your gameobjects.
(Add one Rigidbody Component enough)
I have already added Colliders and Rigidbody components to the cubes and Player. Only colliders on the ground (no rigidbody). I put a raycast on the camera (set to the middle of the screne) and it was triggered by the box, but I can't get the box to recognize the raycast.
Should my raycast script have a function for "isTriggered" and then the box interactable script read the raycast "isTriggered" function and then do stuff?
Your answer
Follow this Question
Related Questions
Tie touch logic into InputManager 0 Answers
Mobile Input - Sensitivity, Gravity Matching InputManager's Key 0 Answers
Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers
mouse input to touch input help please 0 Answers
Is there a way to temporarily disable an inputmanager axis through script? 1 Answer