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.
Related
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!
I want to add knockback to my entire player when it shoots, so it can impulse itself.
I'm new to making games, I don't know much about c# and I haven't found any tutorials for the thing I need
Difficult to know what you need help on here. But try to I'll answer the question.
When your player fires their gun, you would typically create a new object and apply a force to it for slow move projectiles or alternatively, you'll do a ray trace to see if the target is hit for laser beam style shots.
To make the player move, use AddForce on their Rigid Body to apply a force in the opposite direction. That direction is simply the negative direction of the projectile's direction (or the ray that was traced)
Hi guys i was wondering how to create a shape adjustment with two objects which specifically could be described as the independent cells, one of which is static, and the second one is dynamic and surrounded by "plasma". The movement of the active object must be controllable by the user (WSAD). Collision of the active object with the static one causes the static object to be swallen, though doesn't change it's position stays in place all the time. As the active object moves, passes the swallen object and troughts it out.
See the image below:
Player character
When it comes close enough to pink enemy it's starting to swallow it (surround by yellow thing)
Pink enemy is completely sourrounded when red circle is in the centre of both.
When it leaves enemy it takes off the yellow thing
I was wondering what is the simplest way to do it. I've been thinking about cloth, physics joints, mesh substraction (is it even possible?), some kind of animation... I don't have much time to do it. Can you show me the simplest way. Which tools and approach should i use? I'm not asking for full code or full solution only for some tips.
Tim Hunter mentioned a wonderful way, most perfect in 3D.
You can use another approach in 2D :
Inside OnCollisionEnter2D try finding hit points using Collision2D.contacts . See this reference .
Create some particle effect there.
Disable the enemy
Now play swallowing animation of the player.
At animation end, enable enemy again.
Maybe calculation is little tricky, still efficient.
I have the ball in the very right bottom side. When I click somewhere, I want to be able to figure out the direction I clicked and once the user starts to drag, I will calculate the distance. Once the user lets go of the mouse, I want to give the ball some velocity and have it move towards the direction it was first clicked.
I don't know the formulas to compute these things. Any help with explanation is greatly appreciated.
Thanks.
You can check the first mouse down Mouse.GetState() (I think) and save it to a variable. Then check, if the mousedown-state is still given (do this in the Update() function), if not, this will be your destination point. Now you have a starting point and destination point, so you can move your ball in the update-method through the destination point. I hope, the explanation is clear :)
I have a player sprite (playerTexture) and a crosshair sprite (crossTexture) in my game. I need to make the player sprite always face towards the crosshair.
Does anyone know how to do this? I have tried doing it myself but the math involved boggles my mind. I know there's a rotation parameter in the spriteBatch.Draw() method but I'm unsure how to use it.
Thanks!
rotating a sprite to following another sprite is mostly just triangle math. I was going to try and type up a good explanation, but then i found this posting - which includes pictures to help you along the way, which hopefully will help you understand what's going on and what you need to do.
http://www.berecursive.com/2008/c/rotating-a-sprite-towards-an-object-in-xna