I want to spawn objects with text. Where each object is on X axis 60 apart. Plane + text are prefubs with Vector3.zero position.
Here is my code:
This is what I have:
This is what I want to do:
Any idea how to fix it please?
Related
So I am making a gun in a Godot game. I want to use rigidbodies for the bullets, and it spawns like normal out of the gun. However, I cannot seem to find a way to spawn the rigidbody bullets, with velocity. Here is my code so far, I would love some help with this! (I am using gdscript, and I am new to Godot):
extends Position3D
signal spawned(spawn)
export(PackedScene) var spawnling_scene
func _physics_process(delta):
if Input.is_action_pressed("leftClick"):
spawn()
func spawn():
var spawnling = spawnling_scene.instance()
add_child(spawnling)
spawnling.set_as_toplevel(true)
emit_signal("spawned", spawnling)
return spawnling
You can write the linear_velocity of the RigidBody3D before you add it to the scene tree. Here is a quick example:
extends Position3D
func _input(event: InputEvent) -> void:
var bullet := preload("res://scenes/bullet/bullet.tscn")
if event.is_action_pressed("ui_accept"):
var bullet_instance := bullet.instance() as RigidBody
bullet_instance.linear_velocity = Vector3.RIGHT * 50
add_child(bullet_instance)
bullet_instance.set_as_toplevel(true)
how do I make it so that instead of going in one direction, it goes in the direction that I face?
The Basis of the transform has the direction of the axis. Since we usually use the z axis as forward direction in Godot, usually the z axis of the Basis is the direction it is facing:
bullet_instance.linear_velocity = global_transform.basis.z * 50
I have a list of Vector2 positions. X positions goes from -190 to 190 and Y from -300 to 325. When object is instantiated to a panel as parent at a position i remove it from list and when object is destroyed i add position of object back to list, but there is a problem.
When i try to get the position of game object i get position where both X and Y position are close to 0 or 1 like (-1.025,0.3). At the and all my object are instantiated close (if not on top) to each other.
What should i do?
Best regards.
When you give a GameObject a parent transform, its transform.localPosition will return coordinates relative to its parent's transform's position. As such, a transform.localPosition of (-1.025,0.3) means that it is 1.025 units left of its parent and 0.3 units upwards of it.
If you use transform.position instead, you will get the absolute position in world space. This should have the expected values for x and y.
If that doesn't solve your problem, you likely are instantiating incorrectly. Make sure you use the Instantiate method with instantiateInWorldSpace set to true, like so:
Instantiate(myObject, myParent, true);
I am working on Unity 4.7 project and need to create shooting on the target. I simulated gunpoint using horizontal and vertical slider moving on the time. When I click the button I need to memorize x and y coordinates of handles and instantiate bullet hole at this point but don't know how to get cords of sliders handle. It is possible to get values but it seems that it doesn't correspond to coordinates. If horizontal slider changes its value for 1, would its handle change x position for 1?
Use this then:
public static Vector3 GetScreenPositionFromWorldPosition(Vector3 targetPosition)
{
Vector3 screenPos = Camera.main.WorldToScreenPoint(targetPosition);
return screenPos;
}
Have the reference to Handles of the horizontal and vertical sliders, and use them like:
Vector3 pos = GetScreenPositionFromWorldPosition(horizontalHandle.transform.position);
I am making a game in which the balls are falling down on a plane one ball fall over the other ball and they make a line, which goes from bottom to top. I want that at certain point on y axis the ball stop falling. Don't know how to do it the code I used until now for balls to fall down is:
function calling(){
functionsRandom.Range(0, functions.Length);
}
function sphereA() {
var go = Instantiate(sphere,new Vector3(Random.Range(-3, 3),Random.Range(-3,3),-12.78451),Quaternion.identity);
go.renderer.material.color = Color(Random.value, Random.value, Random.value);
}
function sphereB() {
var go = Instantiate(sphere1,new Vector3(Random.Range(-3, 3),Random.Range(-3,3),-12.78451),Quaternion.identity);
go.renderer.material.color = Color(Random.value, Random.value, Random.value);
}
I used random.range, so that the ball falls from points between it both in x and y, for x it is working, but it is not working for y.
Add all the instantiated spheres to an array go[] instead of go. which are in line. Now at certain point where you want to stop generating other spheres, take the count of the sphere in the array. If the count is equal to the max limit. Then DON'T instantiate the spheres. If any doubts, come to unity3d chatroom.
I am trying to position a body using the physics engine with this code:
float touchX = pp[0];
float touchY = pp[1];
float diffX = touchX - mBody.getPosition().x;
float diffY = touchY - mBody.getPosition().y;
mBody.setLinearVelocity(new Vector2(( pp[0]) / PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT , ( pp[1]) / PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT));
The X and Y coords are from a TMX tile map and are 32 pixels square, I am trying to get the body in the physics engine to move to certain tiles in a tile map (held in the pp[] array) but instead it moves to a random chaotic path. Is this the right approach to convert X,Y coords and move to that point??
Thanks
.setLinearVelocity doesn't move body to certain possition. To do this you need to use .setTransform
.setLinearVelocity is like you add force to the body and make it move, so it doesn't move to the position you want.
.setTransform is like you set the position of the body. So it doesn't move slowly from A to B, but disappears from A and suddenly appears at B
You can easy move it slowly by code yourself.