Emitting a constant force from an object(Leaf blower Mechanic) C# - c#

I am just about to tackle a new project where the core game mechanic is covering up dead bodies killed by the mafia with leaves using a leafblower.
You need to cover them well enough to not draw attention when the police question you.
The only problem I can see with this project is how to create this leafblower mechanic.
Off the top of my head I can think of attaching an empty object to my leaf blower which has a constant [rigibody.AddExplosiveForce] that procs on mouse click but thats it.
What should I be looking at and what would be a logical way to create this? You do not need to write the whole code for me, I just wan't to know what tools or scripting API I need to consider.
Thank you :)

You could try continuous adding force using Rigidbody.AddForceAtPosition(). Or calculate directions and try Rigidbody.AddForce().
Basically Rigidbody.AddExplosionForce is working as Rigidbody.AddForce, but uses sphere to calculate Vector3. If you need to emit it continuously, you should just use it in FixedUpdate() and list of object you can get by using Raycasts or simple Trigger Collider, for example Capsule Collider.
You could use Collider Capsule like this ↑↑↑. And in OnTriggerEnter or OnCollisionEnter:
private void OnCollisionEnter(Collider other) {
if (other.gameobject.tag != "leaf") return;
// Your AddForce here.
// Also you can get exact point of collsion by using
// other.contacts and use this as AddForce argument.
}

Related

Spawning gameobjects relative to the position, width and length of another gameobject?

I'm currently developing a game in Unity using C# and I've run into a small problem.
I need to spawn a certain gameobjects relative to the Spawnposition and length of another game object. Now I figured that bounds.size would be the best function to use in this instance. As shown bellow I declare first the variable that uses this in my start method:
public void Start()
{
GameObject PointBar = (GameObject) Resources.Load("PointBar Horizontal");
PointBarVectorLength = PointBar.GetComponent<BoxCollider2D>().bounds.size.x;
PointBarVectorConv = Camera.main.WorldToViewportPoint(new Vector2(PointBarVectorLength, 0f));
}
However, the gameobjects in question are inactive at start's call and thus I presume don't return any value for bounds.size when called.
Does anyone know how I can access the bounds.size or equivalent of an inactive gameobject or is there something else I'm missing here?
As noted in the documentation for the Collider.bounds property, bounds "will be an empty bounding box if the collider is disabled or the game object is inactive". So your assumption was pretty much right. Since the object doesn't exist in worldspace that makes sense.
I'm not sure about the most elegant solution for your use case but two options spring to mind.
Calculate the object's spawn dimensions by accessing its transform.localScale and factoring in the scale of prospective parent objects. That could get messy but you could probably also write a helper method to make it more manageable.
Instantiate the object somewhere off screen after you load it, access its Collider.bounds property to get the information you need then move it where ever you want it or store the information for later.
There may be better solutions but that's what leaps to mind for me.
I solved the issue by using GetComponent().bounds.size.x;
instead of BoxCollider. This component can get accessed when the game object is not active.

Moving a gameObject closer/farther to the HoloLens camera when the hand drags closer/farther to the HoloLens camera

I am currently stuck on a problem where I want to move a gameObject closer/farther to me (the HoloLens camera) when my hand forward/backward. Well, I have correctly implemented the logic to move the object closer/farther but couldn't work my way through determining the movement of the hand.
I am using HoloToolkit's IManipulationHandler interface to move the object and this is the main logic that I am using right now:
void IManipulationHandler.OnManipulationUpdated(ManipulationEventData eventData)
{
transform.position = Vector3.MoveTowards(objectPosition, cameraPosition,
- (eventData.CumulativeDelta * ManipulationSpeed).z);
}
(It only works fine when I don't change my position)
This is another logic I used:
void IManipulationHandler.OnManipulationUpdated(ManipulationEventData eventData)
{
transform.position = Vector3.MoveTowards(objectPosition, cameraPosition,
Vector3.Distance(cameraPosition, eventData.CumulativeDelta));
}
According to above logic whenever I move my hand (forward or backward) the object always comes closer, because the distance b/w the two vectors always increases at first.
Kindly help me with this issue. Thankyou.
Note: I want to rotate the object when my hand drags left/right and to move the object closer/farther when my hand drags forward/backward.
You may check out HandDraggable script from MixedRealityToolkit.
Looks like you are still using the HoloToolkit (now on the htk_development brunch), you can actually use the TwoHandManipulatable to achieve that. Otherwise, you may have to write your own solver. I don't recommend people to implement that from scratch, it is painful.
With TwoHandManipulatable.cs, you can move the object towards any direction. If you want to limit the direction (e.g. only towards the cam), you can override that in your own class.
In my application, I use voice command as a second choice to the hand gestures by modifying SimpleTagAlong scripts. Then associate this action with speech keywords "closer/further". In my experience with end users, they will thank you for having the voice command.
Thankyou... But my problem got solved when I used INavigationHandler interface instead of IManipulationHandler. I worked like a charm. Thanks again.

How to fix interactable objects movement if not interacted without adding gravity

VR-oriented game in dev.
I have the two-hand-manipulation-script from the MRTK implemented, interacting with my objects works. Goal is to let the objects remain static in their position after interacting - theoretically working. As soon as I add collision with my environment (rigidbody # interactableGameObjects).
But as soon as I start interacting with them and let them actively collide with one of the scenery objects, they start tilting and floating without being willing to stop.
So I'm looking for an easy and not resource hungry way to stop any movement (translation, rotation) of my object after interacting with it - without adding gravity to it.
My main concern is not how to stop all the motion, as I could freeze all constraints of the objects rigidbody or set the Rigidbodys Velocity and angularVelocity = 0.
So my basic idea: "if(movement != 0), and if(object != grabbed), then trigger movement = freeze."
Haven't figured out, how to trigger this kind of event and how to check if item is grabbed or not. Or is there maybe an even easier way?
Thanks for any help!
Note: even if you take the sample scene of the twohandedmanipulation and add ridigbodys to the components, they start floating away without any interaction.
Tried so far:
Putting sample scene in same conditions
Expect vs result: Floating away objects instead of remain fixed in space.
You can use RigidbodyConstraints to freeze the object's position and rotation while retaining the collision properties.
GetComponenet<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;
and to unfreeze:
GetComponenet<Rigidbody>().constraints = RigidbodyConstraints.none;

Character Controller For C# Need transform.translate to collide, not teleport

I am fairly new to code, I know some of the basics but my knowledge is limited, so please let me know WHY in your answer if it's not too hard to explain, I'd like to learn rather than just be given the answer!
This code is the basic controls for a game i am making. I'll be explaining the premise of the game just so you're able to better grasp what my intent is.
The game will focus on the completion of mazes, however movement is restricted to only being able to go forward, and right. You may never do a u-turn, go left or go backwards.
With my current code, it is possible (VERY EASILY POSSIBLE) to just translate over the entire maze and the colliders for walls don't apply due to it being a translate, which to my understanding is essentially 'teleporting' it.
I've looked around on similar topics and discussions but I was unable to find any answers that addressed this kind of issue with colliders.
When the object collides with a 'wall' in my maze, I want the object to be reset to it's original position, or at the very least die, I'm not sure if that will affect the answer given, but just incase keep that in mind, thank you!
using UnityEngine;
using System.Collections;
public class Movement_Script : MonoBehaviour {
public float playerspeed = 1;
void Update () {
if(Input.GetKeyDown("up"))
{
transform.Translate(Vector3.up * 1);
}
if(Input.GetKeyDown("right"))
{
transform.Translate(Vector3.right * 1);
transform.Rotate(Time.deltaTime,0 ,-90);
}
}
}
When you control the transform, you are telling "Place this there".
So when you do:
transform.Translate(Vector3.up * 1); // multiplying by one is useless
you tell to displace the object by 0,1,0 from its current position. This is regardless of any environment. You still can detect collision by it won't be resolve by the engine, you would have to do it yourself.
Unity came up with a ready to use solution with the character controller component. It does the same as you do in your code but also perform a bunch of collision checks and resolve them. The latest version is actually using rigidbody for a more physical approach.
http://docs.unity3d.com/Manual/class-CharacterController.html
http://docs.unity3d.com/ScriptReference/CharacterController.Move.html
the Move method is the one to use for Unity to handle the whole process for you. I would recommend to install a basic four wall room with a few boxes here and there so that you get the grip of it. Then it will be easy to move on to do whatever you want with it, whether 2D or 3D movement.

Finding the Joint from the Connected Body

I'm wondering if it's somehow possible to find the Joint that holds my Rigidbody, through the rigidbody?
Allow me to clarify more
I have my GameObject and it has a FixedJoint which has a Rigidbody attached to it (the connectedBody) and what I want to do is to find the FixedJoint, by looking for it from the Rigidbody.
Reason to why I want to do this is because I want my Joint to break when my Rigidbody hits something hard enough. I can't use BreakForce as it's checked all the time, so if I turn my original object fast enough it will break.
Through the Rigidbody, unfortunately there isn't. Given the FixedJoint, you can check its connectedBody property, but you can't check it the other way around.
Don't forget, though, that you can always attach a custom component.
Something like this would be simple enough:
public class FixedJointConnection : MonoBehaviour {
public FixedJoint joint;
}
When you attach the joint, you can also attach a FixedJointConnection and set the joint reference.
You can even write a helper function to do just that:
public static FixedJointConnection Connect(Rigidbody anchor, Rigidbody child) {
FixedJoint joint = child.AddComponent<FixedJoint>();
joint.connectedBody = anchor;
joint.connectedAnchor = anchor.transform.InverseTransformPoint(child.transform.position);
//TODO: any more joint config? depends on your game
FixedJointConnection connection = anchor.AddComponent<FixedJointConnection>();
connection.joint = joint;
return connection;
}
From there, you can add a check during each collision:
Do we have any FixedJointConnection(s) attached?
If so, did we hit hard enough to break them?
Depending on what exactly you need, your custom component class can be very simple or fairly complex. Doesn't matter, though; the point here is just that Unity's components provide powerful features, and that you can amplify those features by writing your own.

Categories