Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by kakashidiot · Oct 06, 2021 at 07:14 AM · inputmobileinputmanager

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

proto3.png (107.8 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kakashidiot · Oct 06, 2021 at 04:30 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

210 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Tie touch logic into InputManager 0 Answers

Mobile Input - Sensitivity, Gravity Matching InputManager's Key 0 Answers

New Input System, Multiple controllers plugged in Issue. 0 Answers

Finite State Machine 1 Answer

Cross-Platform User-Defined Keys? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges