im making a 2D game where you need to swing from one rope to another;
so start position is rope. Your character connected to this rope with HingeJoint and when you start moving your character you can press some button and your character disconnecting with joint.
So i used OnTriggerEnter2D to find next and in this method i need to connect with founded rope.
SO HOW CONNECT WITH JOINT IN CODE
I tried a lot but it didnt work
You need to set one joint as a parent of the other.
On the child joint, set the "Connected Body" property to reference the Rigidbody of the parent joint.
You may then need to configure some limits/springs/motors/etc., but they should be connected.
You would probably chain them.
Like in rope-B set connectedBody to rope-A, and in your player set connectedBody to B.
In general a joint is always either connected to an Rigidbody or a world space anchor => you need to AddComponent and Destroy it on your player!
Related
I am trying to make an AR game and am currently using Unity's high-level networking classes. I have set my player prefab to be spawned in one of the two network spawn locations, which are both childs of the Ground Plane Stage. When user has tapped to make their ground plane stage and has tapped to be the host, their player character appears. Unfortunately, if they press the fire button, the bullets appear above the stage and unscaled, meaning they aren't parented to the stage. This confuses me because I've checked many times and the bullet emitter is a child of the player, and in my code it references said emitter. Thus I'm rather confused why the bullets don't seem parented.
I've attempted to attach a script to make the bullet emitter a child of the player when it spawns. I've also tried making it a child of the stage when it spawns. I've tried making the player character not dependent on the Network manager spawning it when the player joins, but then that leads to other networking problems when it comes to controlling the character, but it can shoot then.
The only one that was relatively successful was making the bullet a child of the stage when it spawned, but it would only stay in one place. Attempting to make the bullet a child of the player did nothing
//This is the class I'm trying to use to make the object a child of
something
public class AddToBeetle : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
GameObject player =
GameObject.FindGameObjectWithTag("Player");
transform.SetParent(beetle.transform, false);
}
}
It rarely prints any error messages. I hope that I can eventually get the bullet to spawn in front of the player model when the button is pressed.
Oh gosh I was dumb, the one thing I didn't apply the script to was the player's bullet emitter. It was super late and that's probably why I missed trying it on that. Anyway I hope this helps someone else if they ever have to deal with AR and Unity.
Ok, for this task I need to coordinate physics hitting a game object at a certain point and animation, to create the illusion of punching a character and he stumbles back as if propelled by that contact point.
I have rigid bodies on both the hitting object and character being hit, and can tell when the hitting object enters the character's box collider. What I thought to do first was create an impulse at the contact point then trigger my pre made character animation -
Vector3 direction = (this.transform.position - collider.transform.position) / (this.transform.position - collider.transform.position).magnitude;
this.transform.GetComponent<Rigidbody>().AddForce(direction, ForceMode.Impulse);
Problem is this just makes the character float slowly off opposite the hitting object (Rigidbody has gravity checked on character), and depending on where the character is facing, the animation looks not coordinated with the punch.
I wanted to see whether there's a streamlined way of doing this - how can I create a realistic punch/moving backward situation in Unity?
There's no easy way to get this behavior out-of-the-box with Unity. You will need to script a blending of ragdoll physics with animations.
One approach you might want to try is a system that "pins" your ragdoll to the bones of an animation, and if a collision occurs, the ragdoll unpins itself (partially or completely) from the bones, temporarily. When/if it completes being affected by physics, you would probably want to animate from a keyframe dynamically created based on the position of the ragdoll to a target keyframe.
There are also tools like PuppetMaster on the asset store that are meant to do things like this, but they are often not free, because they are difficult to make well.
How do I make a gameobject like a sphere to jiggle in space, meaning rapidly moving around in random directions while holding it's original position.
I know I can come up with some really long codes to kind of simulate this but I was wondering if there is a trick to it.
Thanks
I think what you want are joints. You can attach a spring joint component to a gameObject with a rigidbody. On the spring joint, set the connected body to a seperate gameObject with a kinematic rigidbody, then play with the spring joint settings until you get a nice jiggle effect. Probably increase the Spring value a bunch.
I'm trying to make a 2D top-down perspective game in which I want to try and implement a shoving mechanic as a way of the player interacting with the environment or even enemies, but I can't seem to nail down the physics behind it.
(Player object (P) and interactive object (O))
My Question is: what is necessary to make this work properly? I've tried a few things out, such as having the information to add the force to the Rigidbody2D on P first and then on O and I'm trying to get it so that the player pushes the object in the direction it's facing, which I have a script for transform.up to face the mouse. I did also try to implement a dash mechanic to see if it would push an object further, however the physics meant that P would dash in a random direction as opposed to being towards the mouse
give the player object a rigidbody and a colider and the object a colider, should work no problem then!
http://docs.unity3d.com/Manual/class-Rigidbody2D.html
http://docs.unity3d.com/Manual/class-BoxCollider2D.html
should just work automatically then, Unity doing all the physics work for you
edit
if you want the object to be more strongly affected just reduce it's mass int he rigidbody in the inspector, or increase the players mass
I have a character, that has a character controller attached to it I am using transform.position, transform.forward and transform.rotate to move with the 'wasd' keys, that all works fine.
However it will not collide with any sort of colliders, whether is be box colliders or mesh colliders. trigger works on my character, i.e. it can trigger an object to destroy if it passes through, but the colliders that are not set to triggers (like walls), don't block the player, the player just goes through them.
Any ideas?
Note: using C#, unity free version 4.5.1
You Should not use transform for colliding.
Add ridged body and get reference to ridgedbody.
Then use reference.addForce
Guys i figured out the problem, as the previous answer said i should not use transform with character controller, however there's no need to change any colliders, i still kept my character controller on my player, but rather i used controller.SimpleMove to move the character, that fixes the problem and i get to keep the character controller on my object (no need to add extra colliders or rigidbody to the player).
http://docs.unity3d.com/ScriptReference/CharacterController.SimpleMove.html