Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I think it is simple, but I don't find the solution. I want my player to move some times red boxes and others blue boxes. I tried to freeze box X position when player collides, but then I have moving platforms so it doesn´t work. I tried to increase the mass, but I have also problems with some situations. Can I know the force that the player is applying in the box in order to apply exactly the inverse force ? Do you think another way ?
Yous shouldn't be freezing the position or changing the mass to get this effect.
If you don't want move any object to be moved by another object, enable Is Kinematic on that Object.
Is Kinematic description from the doc:
If enabled, the object will not be driven by the physics engine, and
can only be manipulated by its Transform
public Rigidbody redBoxes;
public Rigidbody blueBoxes;
Don't move Red and Blue Boxes by another object(Player)
redBoxes.isKinematic = true;
blueBoxes.isKinematic = true;
Allow movement of Red and Blue Boxes by another object(Player)
redBoxes.isKinematic = false;
blueBoxes.isKinematic = false;
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm trying to implement a lot of poker chips using Unity.
But poker chips are moving like a spring.
I want to implement poker chip in real world using Unity physics.
I tried modifiying the value on Rigidbody (Mass, Drag, Angular Dag, etc.)
and also tried modifiying Physics Material.
But Those didn't work.
I want to remove bounce of poker chips.
Sorry for my bad english.
please help me.
Here is my link
https://www.youtube.com/watch?v=Lw02Jkpfw2I
and a poker chip prefab
enter image description here
Maybe your problem can be solved by adding a physics material.
Right click in the project hierachy and create a new Phsyic Material and set its values to something like this. I have named the object ZeroBounce.
Add the Physics Material to the colliders of your chips and your floor.
You should immediately see a better result.
If the above solution doesn't satisfy you you could also add a script that limits the velocity of the chips. It will slowly lower its vertical speed to zero if it is going up. This will result in a small bounce which I think is a bit more realistic. A damp value of 10 looks pretty smooth to me. You can also just straight up set the y velocity to zero like this: currentVelocity.y = 0; instead of the lerp method. Then you won't have any bounce at all.
public float damp;
void FixedUpdate(){
var currentVelocity = rigidbody.velocity;
if (currentVelocity.y <= 0f)
return;
currentVelocity.y = Mathf.Lerp(currentVelocity.y, 0, damp * Time.fixedDeltaTime);
rigidbody.velocity = currentVelocity;
}
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
so, a little backstory. I have been working on a cel shaded 3d rpg for a while now. it' s going to have the options to switch between action based and turn based. that's for a later time though.
the question I have is I want to add a way to damage the enemy when the sword collides with it. I know onCollisionEnter is a way to do it, I also know raycasts are a different but more complicated way to do it.
I want for each sword swing to damage the enemy (Slimes for now, more enemies later.) incremently instead of an instant death. I have tried onCollisionEnter before, but couldn't get it to decrease in increments.
just, well... an instant death
the onCollisionEnter I have used is with the comparetag<> function.
it might also be important to know that I am on unity 2019.4.X
I have tried to use unity 2020.x but it was too taxing on my PC, as every time I would do something an application load thing would appear and it would be annoying.
Raycasts is not suitable for you if you are not throwing swords. You are using the correct way (collision), but you are missing a point:
This does not happen once when there is a collision. Lots of collisions happen consecutively.
To avoid this, you can close one of the colliders for a short time when there is a collision. (this means that: If the enemy has 100 health and we are decreasing 20 health with each hit, more than 5 collisions occur consecutively. Therefore, we are killing the enemy in one fell swoop.)
Or you can use a bool value (for example swordSwingCollisionActive). If the collision occurs after the sword is swung, you set this bool value to true. When the sword swing is over, you go false again. Just write if (swordSwingCollisionActive) return; at the beginning of OnCollisionEnter.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm making a game like Super Mario. The player is a circle and the enemy is a square that goes left and right after hitting a wall. If the enemy touches the player on the sides the player dies so to survive the player has to jump on the head of the enemy to kill it.
I separated the enemy to two parts body and head (body is parent and the head is a child). Now killing the player is already done but I'm stuck at killing the enemy. How do I kill (destroy) the parent when the player touches the head (child)?
You should be able to add a collider to the child/head, BoxCollider2D for example. Then you can handle the Collision event and call a method on the parent object.
It would look something like:
// In the child/head
void OnCollisionEnter2D(Collision2D col)
{
var goomba = transform.Parent.GetComponent<Goomba>();
goomba.Kill();
}
This is probably the simplest way, but I think there is a way to propagate collisions to the parent, can't remember though.
Edit:
Apparently,
If you're using a Rigidbody with the character, you can get this out of the box. :)
Found that in this answer: https://gamedev.stackexchange.com/questions/151670/unity-how-to-detect-collision-occuring-on-child-object-from-a-parent-script
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I've got an issue in my VR project. I'm trying to move one object based on rotation of another one and first one should move only in kind of borders. I'm using a raycast to detect and control these objects. Both should go in x direction.
1 - Object that I rotate
2 - Object that should move
Any tips? Thanks in advance!
Well this looks like a lathe so I asume that you want to move the tool post over one axis when rotating the wheel.
You want to "link" them, so what I would do is rotate the wheel using a method that also moves the tool post.
Try this:
public float ratio = 1.0f;
public GameObject wheel;
public GameObject tool;
public void RotateWheel(float amount)
{
wheel.transform.Rotate(Vector3.forward * amount);
tool.transform.Translate(Vector3.left * amount * ratio);
}
Note the following:
Amount is passed as degrees.
Ratio is the ratio between wheel
rotation and tool translation. At 1, for each degree, the tool moves
1 unit. Adjust it at your will. I assumed the orientation of your
moving parts based on the model. Try changing the vector directions if they do not work as expected.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I want to make the player move by a specific amount (1 tile/unit in this case) and then stop him. Just like in this video:
http://www.youtube.com/watch?v=DotOAbNngc4
By pressing a key the player should move smoothly to the next tile, than stops and so on.
How can I archieve this? (in C#)
You could take a look at GridMove which is from UnifyWiki.
You would probably get more of a response if you posted this question to answers.unity3d.com or forum.unity3d.com as these forums are specifically target towards Unity3d.
Here's what I would do: you give your player class a target field, e.g. a 2d vector. This will either contain the current position of the player (when initializing or when the target has been reached) or the target that you want to move the player to.
Now in your update you do this:
if target equals your current position then check user input for movement.
if user requests movement set target to position adjacent to current position.
else slowly move player towards target.
You may want to create a field for the current position of the player besides the normal position in GameObject. You could then update GameObject.position in the update-cylce by a small delta until the positions of GameObject.position and target have the (roughly! use epsilon when comparing) same value.
Some hints
Vector2.MoveTowards can help you
To check if the target and position are "close enough" you could subtract one from the other and look at the resulting vector's magnitude.
Beware of overshooting when you move towards the target. If your movement distance is more then or equal to the distance between position and target, just set the position to the target.