I have a problem in my mobile game , that I'm currently developing in Unity.
In my script I detect touches via a RaycastHit2D and then destroy the touched object (In my case a raindrop).
The only problem is that when I run the game and touch the raindrop, it doesn't always work.
It only works , when I slightly touch below it.
The Circle Collider 2D I'm using for my raindrop prefab is alright , so I don't think it causes the trouble.
My Script:
void Update()
{
GetInput();
}
private void GetInput()
{
foreach (Touch touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
Vector2 WorldPoint = Camera.main.ScreenToWorldPoint(touch.position);
RaycastHit2D hit = Physics2D.Raycast(WorldPoint, Vector2.zero);
GameObject collider = hit.collider.gameObject;
if (collider.tag == "Tropfen")
{
Destroy(collider);
}
}
}
}
Related
playerLayer = 11;
if (Input.GetMouseButtonDown(1))
{
RaycastHit2D hit = Physics2D.Raycast(transform.position, crooshair.transform.position - transform.position, 100, ~playerLayer);
if (hit != null)
{
Interactable interactable = hit.collider.GetComponent<Interactable>();
if (interactable != null)
{
SetFocus(interactable);
}
}
else
Debug.Log("Nothing was hit");
}
Every time my player shoot a raycast it ends up hitting my player. The reason this is is because the raycast is starting inside of the player (which is what I want it to do) but it keeps detecting the player no matter what I do. I've used layers, tried to disable Queries start in colliders, and even starting the raycast from a little bit outside of the player but nothing works. When I try and offset the raycast from the player it sometimes works but that is only as long as you shoot in the direction that the offset is going. Please help.
Does your Player have any childed objects with a Collider that are not layered as playerLayer? Just to assure your layermask is setup correctly, I would advise assigning it in the inspector then invert it using the tilde.
public LayerMask playerLayer;
void Update()
{
if (Input.GetMouseButtonDown(1))
{
RaycastHit2D hit = Physics2D.Raycast(transform.position, crooshair.transform.position, 100, ~playerLayer);
if (hit != null)
{
Interactable interactable = hit.collider.GetComponent<Interactable>();
if (interactable != null)
{
Debug.Log("Hit" + hit.collider.name);
}
}
}
}
If you are still unable to debug this with assigning the LayerMask in the inspector, if you could post your hierarchy it might help solve the issue.
I'm working on a simple android game in Unity 2D, I have to make our player jump by adding force or changing the velocity of its Rigidbody2D (I know how). The problem is I don't know how to tell unity that add force on single screen tap (I'm not familiar with touch inputs) so any help would be appreciated. Please keep it clean as I'm just a beginner.
Here's the script I made so far.
{
public Rigidbody2D _playerRB;
public bool _canMove;
public float _speed; //keep this above cam speed (relative)
public float _jumpForce;
void Start()
{
_playerRB = GetComponent<Rigidbody2D>();
}
void Update()
{
_playerRB.velocity = new Vector2(_speed, _playerRB.velocity.y);
if (Input.touchCount > 0)
{
_playerRB.velocity = new Vector2(_playerRB.velocity.x, _jumpForce);
}
}
}
You can achieve this by handling the touch input in your Update function:
void Update()
{
_playerRB.velocity = new Vector2(_speed, _playerRB.velocity.y);
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
_playerRB.velocity = new Vector2(_playerRB.velocity.x, _jumpForce);
}
}
}
Also there are different TouchPhases such as TouchPhase.Moved which indicates that the touch input has moved and TouchPhase.Ended which indicates that the finger has stopped touching the screen. Using these in your Update you can achieve a lot with touch controls.
Unity version: 2018.3.2f1
When I throw my ball against the wall, a decal projection spawned on the spot where the ball collides. This goes well until the ball touches the wall and the ground at the same time. The decal projector is then spawned incorrectly, so that the red spot does not come out nicely on both surfaces
As you can see in the screenshots below, I know that the decal has to stand at an angle against the wall so that it is also projected onto the ground. Only I have no idea how I can do this
I now ensure that when the ball touches a layer with the tag "col", it is exactly at the point where the ball collides it places a decal projector. I would like to know how I can also have these placed obliquely
Decal spawned wrong PNG
Decal spawned wrong video
This is what I want when it hits the wall and floor:
This is wat I want
public Camera cam;
public Transform sphere;
public float distanceFromCamera;
Rigidbody r;
public GameObject decalPrefab;
// Start is called before the first frame update
void Start()
{
distanceFromCamera = Vector3.Distance(sphere.position, cam.transform.position);
r = sphere.GetComponent<Rigidbody>();
}
Vector3 lastPos;
// Update is called once per frame
void Update()
{
if (Input.GetMouseButton(0))
{
Vector3 pos = Input.mousePosition;
pos.z = distanceFromCamera;
pos = cam.ScreenToWorldPoint(pos);
r.velocity = (pos - sphere.position) * 10;
}
}
void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "col")
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo, 100000f))
{
Instantiate(decalPrefab, col.contacts[0].point, Quaternion.FromToRotation(Vector3.up, col.contacts[0].normal));
}
}
}
}
I'm making a mobile game based on android. When I was making a transfer code for my player object, I wrote code like this.
void Update() {
if(Input.touchcount == 1)
{
Touch touch = Input.GetTouch(0);
if(touch.phase == TouchPhase.Began)
{
//Move
}
else if(touch.phase == TouchPhase.Stationary)
{
//Move
}
}
}
It's moving at right direction and speed that I wanted. But when I touch quicker, these codes are don't working smoothly. I mean the player object is not moving smoothly.
Is there any solution for this problem?
P.S. I cant use AddForce in rigidbody
I am making a game where an object follows my finger. But I do not want the object to jump to my finger if I tap anywhere on the screen. I am using a raycast to do this. It works perfectly except if I move my finger too fast the object freezes.
My theory is that because of it being in Update() that once I move my finger off the object it detects that I can no longer drag it. I have no idea how to fix this.
public GameObject Bigball;
public GameObject Smallball;
// Update is called once per frame
private void Update ()
{
// lets ball follow finger
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(0).phase == TouchPhase.Stationary)
{
var touch = Input.GetTouch(0);
Vector3 fingerPos = touch.position;
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(fingerPos);
if (Physics.Raycast(ray, out hit) && (hit.collider.tag == "ball"))
{
Smallball.SetActive(false);
Bigball.SetActive(true);
Vector3 pos = new Vector3((Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position)).x,
(Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position)).y, 0);
Bigball.transform.position = pos;
}
}
else
{
Bigball.SetActive(false);
Smallball.SetActive(true);
Smallball.transform.position = new Vector3(Bigball.transform.position.x, Bigball.transform.position.y, 0);
}
}
It might make more sense to only Raycast once when the finger touches the screen. If that Raycast finds an object to drag, simply store that object and move it to follow the finger every Update(), as long as the finger continues to touch the screen.
Once the finger is released, just forget about the object until the next touch finds a new object to drag.
An alternative would be to use no Raycasts at all but build on IBeginDragHandler, IDragHandler and IEndDragHandler, as Bijan already mentioned.