Unity: Drag on Screen and instantiate game object - unity3d

How can I continuously instantiate Game objects by dragging on screen with unity and making the game object to follow the direction of the dragging?
For example: Drag right instantiates right, Drag up instantiates up or drag upwards at a certain angle and instantiates accordingly.

I don't know what you have done so far or how good you are in programming, but the basic principle would be
Create a DragInstantiate Script, which you put on a GameObject on your Scene, but not the one you want to Instantiate.
In the Update Method of this Script you have to check, if the left mouse button is pressed down. https://docs.unity3d.com/ScriptReference/Input.GetButtonDown.html
When you registered the click you have to save the position of the click. https://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html
The next step is to check when the left mouse button is released and again save the position. https://docs.unity3d.com/ScriptReference/Input.GetButtonUp.html
From these two positions you can really easy calculate the direction, in which the object should move. https://docs.unity3d.com/Manual/DirectionDistanceFromOneObjectToAnother.html
The last thing is to instantiate the object and set it's moving direction. https://docs.unity3d.com/Manual/InstantiatingPrefabs.html

Related

Child gameObject doesn't move with the parent gameObject

So, right now I'm working on a Unity game where if you click on an item, it will become the child of the click object. The click object is also the child of the player and moves with the player, but the object that gets clicked (making it the child of the click object) just stays where it is. Even if you move. I don't know why it doesn't move with the player, does anyone know why?
Never nest (parent) Rigidbody2D (=> physics based objects) under another object.
It doesn't behave as you expect since the Physics engine doesn't care about the hierarchy of the objects.
If you need a rigidbody to follow another object rather use Joints!

In unity why cannot Instatiate a gameobject

I am currently coding in Unity and I am trying to Instantiate a gameObject. The mechanism I'm developing will delete the Image I'm on and load the image underneath, using two buttons ; One to create and one to delete thus giving the effect of moving through a graphical stack.
I have thought about moving the image forward along the z axis so it is out of view, but I have read that the object MUST be instatiated and not Transformed
I have a Rigidbody2d and (2x) 3D Box Collider over the image that has the code. One on the left and one on the right
The delete button works a treat and just to check I do not have a problem with the box collider I have tried the delete code on the other button which also works fine, so I have come to the conclusion that the code is flawed in some respect
My problem is that I cannot seem to recreate the object once it has been deleted, it is my understanding that the object does not have to exist in world space for it to be recreated. If I use the Quaternion Identity I get a few errors about the gameObject being rendered off screen
***
Instantiate(gameObject,new Vector3 (8,-17,11),
// Removed until I can actually get the image to Instantiate
Quaternion.identity);
***
I have also tried
***
Instantiate(gameObject,new Vector (gameObject.transform.position.x -0.08, gameObject.transform.position.y, -0.17, gameObject.transform.position.z, 11), Quaternion.identity);
}
}
***
Can someone please tell me what I am doing wrong?
I am fairly new to Unity too, but I believe what you need is a Button that Swaps the Sprite image.
Select your Button in the Hierarchy.
In the Inspector:
In the Image component, drag in the first Sprite
In the Button Component:
select the check box Interactable
select Sprite Swap in the Transition Drop Down
drag your second Sprite to "Selected Sprite"
I am fairly new to Unity too, but I believe what you need is a Button that Swaps the Sprite image.
Select your Button in the Hierarchy.
In the Inspector:
In the Image component, drag in the first Sprite
In the Button Component:
select the check box Interactable
select Sprite Swap in the Transition Drop Down
drag your second Sprite to "Selected Sprite"

Problem animating the position of a GameObject in Unity while moving it

I am working on moving a Cube across the screen when I press an arrow and display two different animations depending on if it is moving or idle. I used a very basic translation to do this when I press the right arrow. I have two different animation states that work fine independently. One spins the cube when it is “idle” and the other makes to bounce up down in the “right” state. Both are 1 second animations. The idle animation spins the cube 360 degrees, and the right animation will move the cube up 1 unit then back down to its original position. Everything works well by itself. I included an image for the states for the animation.
Cube Animation States
In order to get the movement working with the animations I made an empty GameObject and made the cube a child if it. When the cube is idle it spins, and when I press right it moves and bounces up and down. The problem is when I release the right arrow the cube snaps back to its original position from the start of the last bounce animation. If I hold right for 10 seconds, and release it will only snap back to the beginning of the most recent iteration of the bouncing animation (not to when I started pressing right).
Does anyone know why this would happen? I tried changing some of the settings for the transitions, but it doesn’t seem like that is the problem.
Can you check that your idle animation modifying its position in any case.
First, you can't move the game object with animation.
you can have the rotate animation in the cube but not the translating part.
Because when the animations switch between the states the object will be moved to its original position.
solution:
-Create a script and attach the script to that game object.
-use translate functions to move the object

What's the best way to trigger an animation from the input of another object?

I'm new to unity and c#.
When i air tap on the left sphere, the plane and the right sphere should appear.
I figured that an OnPointerClicked event on the left sphere the animation of the plane and right sphere triggers (with an if/else statement).
Is this the way to go?
If so, how do i do that?
Or is there an easier method?
Thats the script of the left sphere:
Read this: IPointerClickHandler.OnPointerClick
You need to implement the interface, i.e. IPointerClickHandler in your MonoBehaviour class. When you click on the object in game, it will send a click event to EventSystem, which triggers the function body you have in your OnPointerClick method.
So as the doc mentioned, also make sure you have an EventSyetem (an empty game object with the script attached, or attach the component to your main camera).

Make an object rotate according to mouse movement. [UNITY C#]

I’m starting a learning project. The idea is that you have an archer character that is static, that has a bow attached to it that shoots arrows to targets of varying difficulty.
Turns out that right at the start I’m stuck. How do I make it so that the bow rotates when the player clicks and holds the mouse anywhere on the screen? So I click+hold and move left/right and the bow rotates left/right to aim the shot. I’d like to also eventually make it portable to phones (so you’d tap+hold etc).
Stack Overflow isnt a code writing service but i will explain what you must do:
Method 1 (Exact Aiming):
For every frame the mouse is down:
Make a Ray from a screen point... hint (use
camera.ScreenPointToRay).
Get a far point along the Ray using ray.GetPoint(distance);.
Bow.Transform.LookAt(newPoint, Vector3.Up);.
Method 2 (Continuous Movement):
Make a variable oldMousePos to store a Vector2 location.
Record your initial screen click position into that variable on a
mouse down event.
Have a function that runs once every frame the mouse stays down.
For the direction of the rotation of the bow, you can use
(newMousePos - oldMousePos).normalized;.
For the speed of rotation for your bow, you can use (newMousePos -
oldMousePos).sqrMagnitude;.