I am working on a project with Unity and Leap Motion.
Basically, there is an inputField. In the inputField the user will write the distance, in centimeters, for which the grasp event should be triggered. When the fingers get closer than the distance of the user's input, the grasp event is triggered and a ball is grabbed. That's because I'm working with people with movement deficits and they can't fully close their hands.
I've written a script that prints the distance between the two fingers at each frame, but I don't know how to trigger the grasp event using the distance of the inputField. Any advice?
The most straightforward way to implement this is to introduce a variable which reads the content of the inputField and an if statement that triggers the event on Update() within the script you have written - something like that (assuming you're calculating graspDist as you described):
float dist = float.Parse(gameObjectWithInputField.GetComponent<InputField>().text);
if(dist/100f<graspDist) TriggerGraspEventFunction();
and then you define your TriggerGraspEventFunction() somewhere else within that script. You can either make the gameObjectWithInputField a public GameObject and drag it in in the inspector or tag it and use FindGameObjectWithTag
There are many more refined ways but this should do it for now
Related
I'm trying to make a game where you're a boy and you can transform into a deer.
I don't know how to make the player transform into a deer when pressing a button. Can someone please tell me how to make the player transform into a deer when pressing a button and how to make the deer transform back into the player when pressing the button again?
There are 2 ways depending on what you want exactly.
1.Transformation without an animation, you can do this by having 2 separate models
public GameObject boy;
public GameObject deer;
//Give both objects the same tag and don't add anything else under the
tag in order for this to work
private Transform currentPosition;
private void Start()
{
deer.SetActive(false);
}
public void TransformButton()
{
currentPosition = GameObject.FindObjectWithTag(LayerMask.NameToLayer("TransformationTag"));
//Unity doesn't pick up disabled GameObjects so it will pick up only the
//active state (the active object)
boy.SetActive(!activeInHierarchy);
deer.SetActive(!activeInHierarchy);
GameObject.FindObjectWithTag(LayerMask.NameToLayer("TransformationTag")).getComponent<Transform>() = currentPosition;
}
I recommend this way if your deer and boy have different scripts
you could also play an animation on click (apply this animation to the active character) and when it is done (below is how to check if it is done) you can trigger the code above
https://answers.unity.com/questions/362629/how-can-i-check-if-an-animation-is-being-played-or.html
I am pretty sure there are other better ways but this is the way I do it
The other way which is probably easier if you are good at animating is to just trigger an animation using the animation bools when the button is clicked but I haven't tried this way because I'm not that good at animation. Instead I use the first way (usually for switching skins in a shop for example) but I never did animations that completely change the size ratio so much so I don't know how well would it work (I usually do 1:1 transformations like humanoid to humanoid or tank to tank so you will have to experiment a bit)
I dont think that this is possible in unity. Maybe there are some Assets that offer something like this.
But you could create a similar effect with Blender.
Check out Morphing Shape Animations.
There are some cool tutorials out there Morphing Shape Animations in Blender
I recently started having a look at game development with Unity and was trying to make a simple 2D character with basic movement abilities. This character is supposed to jump and move from side to side, but only if it is standing on something.
Now my question is: How do you check if a player is standing on something? / Get the distance to the next game object / collider beneath the player game object?
Would greatly apreciate any helpful answers and especially explanations on how exactly it works. Thanks!
To do this, you need to send a ray to detect the point of impact on the ground and then calculate the distance. The code below sends a ray from the center of your object down to the maximum height (3) and gives the size.
public LayerMask groundLayer;
public float maxRayLength = 3;
public void Update()
{
var hit = Physics2D.Raycast(transform.position, Vector3.down, maxRayLength, groundLayer.value);
if (hit) Debug.Log(hit.distance); // it will print current distance from pivot
}
If you want to calculate the height of the ray from the character's foot, there are two methods, one is subtracting half the height of the character from it.
Physics2D.Raycast(transform.position-transform.up*height, ....)
Next one is to use an empty object at the base of the character, which we refer to instead of the center.
public Transform pivot;
Then..
Physics2D.Raycast(pivot, ....)
There are a few ways of actually doing this.
The most usual although a bit complicated way of doing it for a beginner is using Raycasts. A Raycast is basically a small invisible line that starts and ends where you tell it to. If anything of a specific tag or layer is caught in it's crossfire you can basically pull that object from your code. Raycasts are used for a lot of things, most notably in Shooter games to shoot or in games like Skyrim to pickup objects and interact with them.
Another way to do this, which is a bit more popular in 2D games is to create a "feet" GameObject and make it the child of the player in the hierarchy. You can add a box collider on that GameObject and check the "IsTrigger". You can add a Tag to your ground objects and through your code using the OnTriggerEnter() and OnTriggerExit() Methods you can basically tell when your character is floating on air and when he is on ground.
Another popular method is to use the Physics.OverlapBox() Method which is pretty much the same as the Trigger Method but you are creating an invisible box (much like a raycast) and instead of only getting notified when Triggered (something enters or exits) you check if the invisible box is colliding with another object/tag/collider (which could be your ground).
There are also a few different things you can do with a Nav Mesh (mostly in 3D) but I think that for now these 3 should suffice!
How do I detect Input.GetMouseButtonUp outside a specific GameObject's area in Unity? Are there any Unity Assets for this, or should we do this programmatically?
It certainly has to be done by programming it, whether there is an asset or not. As it is rather easy to program, I doubt there is one.
What you need to understand is, that Input.GetMouseButtonUp(int button) is an event and entirely decoupled from any "position". The description from the API says "Returns true during the frame the user releases the given mouse button.".
So in the frame a mouse button has been released you want to check if the arrow is "touching" or laying over some GameObject (GO). According to your example you will do nothing when the GO was hit, and else do something.
One way to do that is to use the following method:
http://docs.unity3d.com/ScriptReference/Camera.ScreenPointToRay.html
Your GameObject will at least need a collider, maybe a rigidbody too in order to be detected by the ray. I'm not completely sure atm if both are needed.
Additional note: Having an UI element, this method could also be used to set some flag while hovering over it e.g. : http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseOver.html
Currently when I am trying to detect whether an object has been clicked / interacted with by the mouse, I create a script on that GameObject and then define void OnMouseDown() and apply functionality within. If I then want to trigger a function in another script that is not attached to the GameObject I call it through a GetComponent<>() reference.
Is there a way to add a callback to the OnMouseDown event from a script on another GameObject? Potentially in the same way delegates / events are allocated and invoked? Something such as: gameObjectReference.GetComponent<BoxCollider>().onmousedownevent += MyFunction; So that it automatically calls this function on mouse down?
Note: I know that you can use raycasting to achieve something similar, but my question is about attaching callbacks to the existing Unity event.
1) I understand the sense of what you mean by this
pseudocode:
gameObjectReference.GetComponent<BoxCollider>().onmousedownevent += MyFunction
and there is no way to do that.
2) please do understand: it is incredibly easy to achieve your aim (elegantly). Add a UnityEvent in script "b", Invoke it in the OnMouseDown. drag anything you want there. For anyone reading who is new to Unity, pls try UnityEvent in general as it is used constantly
3) A I mention you can kind of do what you want by fooling around with the OnPointerDown(PointerEventData eventData) systems, but this just emphasises you can't do what you ask, do it with OnMouseDown.
Sorry for the grim news.
I want to throw a gameObject (like a sprite) with my mouse.
I would like to handle to power if the mouvement is fast.
I've try to develop it myself but I haven't found yet.
In my mind I think I need to do this :
on mouse down : get the position
0.5secs after of on mouse up : get the new position
and calculate the distance between the 2 points.
Conclude a direction and a force.
What do you think about this ?
I created something fairly similar to this. You should do OnMouseDown() then Raycast on the position of the mouse click. Then your Raycast should grab the collider of the GameObject that was hit. Then you send a message, SendMessage, to that collider telling it what you want to do.
All the links I have given you and some trial an error will help you construct what you need, that is how I created my level editor which is in essence the same thing. Note that all links have code samples that teach you all you need to do. Just use your imagination and you can get it done!
Just start coding. If you get stuck then ask a more specific question and someone will help you. Good luck.