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.
Your answer
Follow this Question
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