I am trying to get a dynamically instanced kinematicBody2D with an area 2D attached to handle mouse entered/exit inputs. I have created my area 2D with correct collision body, and have tested a similar collision body for detecting some area 2d's and this is working happily, however, the mouse detection is not triggering the function as it should.
I am unsure of why it does not appear to be detecting my mouse. I am assuming I have messed with the Masks incorrectly, and it is not on the same level, however looking at some of the documentation this is not suggested to be a problem.
I am unsure of what code to attach because it is not really coded at this point.
Any help would be appreciated.
To detect mouse events on an Area or KinematicBody, set input_pickable to true and connect to one or more of the provided signals.
KinematicBody2D and Area2D both inherit from CollisionObject2D, so they can both handle mouse input. This means you don't need to add an Area to your KinematicBody unless the area that detects clicks needs to be different than the area that detects collisions (e.g. only a small part of a larger object is clickable).
Here's how you could detect mouse events on a KinematicBody with some CollisionShape:
func _ready():
input_pickable = true
connect("mouse_entered", self, "_on_mouse_entered")
connect("mouse_entered", self, "_on_mouse_entered")
connect("input_event", self, "_on_input_event")
func _on_mouse_entered():
print("mouse entered")
func _on_mouse_exited():
print("mouse exited")
func _on_input_event(viewport, input_event, shape_idx):
var mouse_event = input_event as InputEventMouseButton
if mouse_event:
prints("Mouse button clicked:", mouse_event.button_index)
Related
I want to move my Player node (KinematicBody2D) from one scene to another. My code successfully moves Node2D node, but fails with KinematicBody2D node.
I have two Location scenes (inheriting from base Location scene) with the following structure:
FirstLocation (inherits Location)
- YSort
- Player
In the base Location scene I have two methods:
func add_player(player: Player):
get_node("YSort").add_child(player)
func remove_player() -> Player:
var player = get_node("YSort/Player")
get_node("YSort").remove_child(player)
return player
In GameWorld scene I store the possible locations inside a dictionary and the moving of the player happens inside change_location() function:
onready var _locations = {
Location.FOO: $CurrentLocation,
Location.BAR: load("res://locations/Bar.tscn").instance(),
}
onready var _current_location = $CurrentLocation
func change_location(location: int):
var player = _current_location.remove_player()
remove_child(_current_location)
_current_location = _locations[location]
add_child(_current_location)
_current_location.add_player(player)
The switching of the location works
The moving of the player also works in case the player is plain Node2D.
But when Player is KinematicBody2D then the game simply crashes, giving me no hint as to what's causing the problem.
The code works without crashing when I comment out the last line:
_current_location.add_player(player)
...but of course then the player simply doesn't get added to the other scene.
I verified that the player does get removed from the scene.
I tested with a dummy scene (which only contains KinematicBody2D as root node and a simple Sprite as a single child) instead of my actual more complex Player scene to make sure it's not related to any other code I might have in my Player scene. Node2D as root works, KinematicBody2D crashes. Must have been a fluke. Tested again and now both work, so there must be something different in my Player object.
I tried adding the Player node as a direct child of Location node (not having a YSort node in the middle) - nope, still crashes.
I tried setting the position/global_position of player before/after adding it to new scene - no difference.
I'm able to create new Player scene instance and add it to new location.
What might it be in KinematicBody2D that prevents me from moving it from scene to scene?
Found a solution, but I don't know why it works.
I was performing the location change as a response to Area2D.body_entered signal. I triggered my own signal "location_change" and then called the change_location() function in another part of the code in response to it.
Adding a tiny timeout (0.00000001 seconds) before doing the location change solved the issue. However I have no idea why, and I'm pretty sure adding timeouts is not a proper way to solve this problem.
I'm having trouble visualizing the situation. However, given that the problem happens when removing and adding a physics body, and that "body_entered" was involved, and that the solution you found was adding a time out…
Sounds like the issue was removing the physics body while Godot was still resolving physics response. And then the solution was to wait until the next frame.
You could wait until the next graphics frame with this:
yield(get_tree(), "idle_frame")
Or until the next physics frame with this:
yield(get_tree(), "physics_frame")
Which would be better than some arbitrary small timeout.
Alternatively, you could also make the signal connection deferred.
If you are connecting from the UI, there will be an "advanced" toggle in the "Connect a Signal to a Method" dialog. Enabling "advanced" toggle will reveal some extra including making the connection deferred.
If you are connecting form code, you can accomplish the same thing by passing the CONNECT_DEFERRED flag.
I am trying my best to build a mobile game in the godot game engine. And i have run into a problem. In a 3D game, how can i detect a mouse click on a specific object or rather where that object is on the screen. I do not understand how to use the control nodes in order to to this.
The easiest way to make a 3D object clickable is to give it a CollisionObject (such as a StaticBody) and connect to the input_event signal. For example, to detect a left-click:
extends StaticBody
func _ready():
connect("input_event", self, "on_input_event")
func on_input_event(camera, event, click_position, click_normal, shape_idx):
var mouse_click = event as InputEventMouseButton
if mouse_click and mouse_click.button_index == 1 and mouse_click.pressed:
print("clicked")
The docs mention that touch events are similar to click events.
Note that input_ray_pickable must be true on the CollisionObject (this is the default).
I have a game object (specifically, a rectangle). I want to display text that follows the mouse only when hovering over the rectangle.
I tried using a rectangle.on('pointover', function(pointer) {...}) listener, but that only catches the initial mouse over event. It won't fire continuously to allow the text to follow the mouse.
I assume I need something in my update() method like:
if (rectangle.onPointerOver()) {
update text x and y from pointer
}
But I don't see any such method on the GameObject or Rectangle.
I also know I could naively find the x and y coordinates and length and width of the rectangle and check that against the pointer, but Phaser 3 must have a better way of doing it.
It turns out that adding a listener is the right approach, but the event I should be listening for is .on('pointermove', function(pointer, x, y, event) {}).
I am definitely late to the party here. Just wanted to fill in some details for anyone who finds this later!
You can definitely use the pointerover event when creating your rectangle, just like any other GameObject.
You just need to make sure you call setInteractive() on the game object first.
const rect = this.add.rectangle(200, 200, 148, 148, 0x6666ff);
rect.setInteractive()
rect.on("pointerover", () => {
rect.setStrokeStyle(4, 0xefc53f);
});
Also, for future reference:
I couldn't find the full list of available input events in the docs, but here they are in the git repo.
https://github.com/photonstorm/phaser/blob/master/src/input/events/index.js
Good luck 😊
I used something like this:
gameObject.setInteractive().on('pointerdown', function(pointer, localX, localY, event){
// ...
});
I don't know how to change this for hover, but maybe this helps someone.
When an Object enters Area2D I expect an object entering to be removed but nothing happens
I have tried queue_free(area) and Area2d.queue_free()
func _on_Area2D_area_entered(area):
queue_free()
area.queue_free
Like said above nothing happens when an object enters Area2d
Ok. I have made a simple project to test the collision and queue_free() process with regards to an interaction between KinematicBody and Area2D. Basically, the Area2D is the object that detects and signals a collision (KinematicBody can cause a collision, but not detect it). Therefore it is up to Area2D to detect when a BODY has entered (or in this case, when it enters a body), and then it can signal the collision, which then calls the _on_Area2D_body_entered function in the other object, and then executes queue_free() as instructed.
My node setup is as follows:
Node2D
|_Area2D - (SIGNAL ATTACHED - "_on_body_entered()" - CONNECTED TO KINEMATIC BODY)
| |_Sprite
| |_CollisionShape2D
|
|_KinematicBody2D
|_Sprite
|_CollisionShape2D
For my trial, the Area2D object is placed on same horizontal axis, and to the left of the KinematicBody
SCRIPT ATTACHED TO AREA2D:
extends Area2D
func _ready():
pass
func _process(delta):
self.position.x += 1 #So it moves into and collides with other object.
SCRIPT ATTACHED TO KINEMATICBODY:
extends KinematicBody2D
func _ready():
pass
func _on_Area2D_body_entered(body): # This is called when Area2D detects a collision
queue_free()
print("HIT!!!!!") #To doubly confirm the collision is recognized
Unfortunately, it kind of has to be this way. Since Area2D is the only object in this scenario which is capable of detecting and signalling the collision, it cannot be ordered to queue_free() itself, as Godot throws an error when attempting to do so.
Alternatively, however, if you wanted it the other way around - for the KinematicBody to move towards the Area2D object, and delete itself upon impact. simply move the whole delta process from the Area2D script and place it in the KinematicBody script above the _on_Area2D_body_entered(body) and change the self.position.x to -= 1. Leave the signal on the Area2D.
I wondering if someone could give me a hand with this problem I'm having with objects and collisions in Unity.
I have a sphere object being controlled by the users phone's accelerometer. The sphere moves around fine but once it hits a wall the sphere starts acting weird. It pulls in the direction of the wall it collided with, starts bouncing, and just overall not responsive anymore to the movement of the phone.
Any idea as to why this could be happening?
Here is the script used to control the player's sphere.
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed;
void Update() {
Vector3 dir = Vector3.zero;
dir.x = Input.acceleration.x;
dir.z = Input.acceleration.y;
if (dir.sqrMagnitude > 1)
dir.Normalize();
dir *= Time.deltaTime;
transform.Translate(dir * speed);
}
void OnTriggerEnter (Collider other)
{
if (other.gameObject.tag == "Pickup") {
other.gameObject.SetActive(false);
}
}
}
That happens because your object has a 'Rigidbody' component, and, I suppose, it's not a kinetic rigidbody. Basically, it behaves just like it should: a real physical object will not pass through another object, that is the most basic behaviour of a physics engine. However, since you don't operate with the physics-based object using forces, but manually change it's position, you break a level of abstraction. In result, you move the object inside the wall, and now it can't get out.
Use ApplyForce method instead. If you want to pull or push object (instead of just move, which contradicts the fact that these objects are managed by physics) in a certain direction every frame, you should use ForceMode.Acceleration (or ForceMode.Force, if you want the effect to depend on the mass) every physics frame, which means that you have to use FixedUpdate method instead of Update.