Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by jensuazo · Jan 03, 2017 at 01:58 PM · raycastingtouchesselecting objects

Selecting Object to throw using raycasting

Hi there,

Like so many others here, I'm very new to Unity and I'm currently making a basic mobile game wherein I have to throw multiple objects one at a time to hit a characters to gain points. Pretty simple right? I have the SwipeControl down thanks to thegamecontriver and it I managed to tailor it to my game and it works perfectly. My problem is that when I swipe anywhere on the screen, my game objects all fly off. So I thought "Hey why not learn about raycasting?" coz I've read around that that would definitely help. I was able to do learn a bit of it but I'm still not sure how to tailor fit it to my game. I just basically want the raycast to check if I've selected an object and only apply that swipe control to that object. Can anyone help? Thanks!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class TouchInput : MonoBehaviour {
 
     //Raycasting
     Ray ray;
     RaycastHit hit; //info regarding the object the ray has hit
     bool isSelected;
 
     // Update is called once per frame
     void Update () 
     {
         //this is checking if finger count is more than 1 and that 1 finger's touch begins.
         if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began) 
         {
             //gets touch and converts to a ray.
             ray = Camera.main.ScreenPointToRay (Input.GetTouch (0).position); 
             //just to show where the ray is hitting. 20 is just 20 times long. 
             Debug.DrawRay (ray.origin, ray.direction * 20); 
 
             //casting a ray from camera to object and check if it's colliding with an object
             if (Physics.Raycast(ray, out hit, Mathf.Infinity))
             {
                 //part I'm having trouble with
                 SwipeControl sc = hit.collider.gameObject.GetComponent<SwipeControl> ();
                 isSelected = true;
             }
         }
     }
 }
 

Thanks guys! Apologies for the funny notes. Newbie here.

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

0 Replies

· Add your reply
  • Sort: 

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

89 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

Related Questions

Obtain a Transform using RayCastAll without a Collision (ie a point in space) 2 Answers

Ray does not rotate with gameobject when IK is enabled 1 Answer

Calculate normal vector to the plane that was hit by raycast 0 Answers

Will a raycast collide with another raycast if properly aligned? 1 Answer

Ignore Raycast not working properly? 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