Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
When the object moves up and down sometimes it stays stuck on the floor. What is the reason for this? And I am using RigidBody2D and my code in FixedUpdate() method also I use box collider.
I mean when I start the game a few times, it sometimes happens and sometimes doesn't. I would be very happy if you could help. Thanks in advance.
So let me explain in more detail what I want to tell you in more detail, I want to move the obstacles up and down with the help of the code, that is, the obstacles change their direction when they touch every floor or ceiling, and they move up and down, that is, when they hit each ceiling or floor, their directions change.but sometimes obstacles stick to the ceiling or floor when they need to change direction
I'd just make a fixed point for each game object to change direction instead of checking for collisions with a rigid body.
void FixedUpdate() {
if(obstacle.transform.position.y + obstacle.transform.localScale.y >
upperPointOfReturn)
{
obstacle.ChangeDirection();
}
if(obstacle.transform.position.y - obstacle.transform.localScale.y <
lowerPointOfReturn)
{
obstacle.ChangeDirection();
}
obstacle.transform.position = new Vector2(
obstacle.transform.position.y
+ velocity* Time.deltaTime * direction, 0
);
}
void ChangeDirection() {
direction *= -1;
}
Create an Physics Material 2D with 0 Friction and 0 Bounciness and attach to the Player's Collider.
Related
I don't want my player to be able to walk off ledges. I did this by shooting a single raycast downwards in front of the player, and if ground is NOT hit, then ignore input.
However this is jarring, especially if you diagonally walk along an edge you just completely stop, rather than 'slide' along it.
So I thought I could add two raycasts, one per side to detect which side the ledge is, then allow movement (or steer) the player as applicable.
The problem is I'm not sure how to proceed from here. I'm using a character controller for movement, my current code is like:
velocityXZ = velocity;
velocityXZ.y = 0; // we deal with gravity elsewhere
velocityXZ = inputDir * playerSpeed;
if (facingDropLeft || facingDropRight) {
velocityXZ.x = 0;
velocityXZ.z = 0;
}
velocity = new Vector3(velocityXZ.x, velocity.y, velocityXZ.z);
// handle gravity
charController.Move(velocity * Time.deltaTime);
Could anyone offer some insights into what direction to look into, or methods I will need?
I think that if you want to accomplish an enjoyable result you should use invisible walls. I think that probuilder can help you.
This is the approach I would have with this type of problem.
Use boxes to make wall than turn off the mesh renderers this will make invisible walls
This question already has an answer here:
Stop Rigidbody Movement/Rotation instantly after collision
(1 answer)
Closed 4 years ago.
How to stop a 3D object(say a car) falling downwards when collided with another 3d object(say a road). I have tried this the whole day but no results.
Both the 3D objects should have colliders, and the car should also have rigidbody component.
You can set the rigidbody to kinematic or just disable the rigidbody. That can be done by code. This piece stops momentum entirely (which may be helpful to know anyway):
rigidbody.velocity = Vector3.zero;
rigidbody.angularVelocity = Vector3.zero;
If you ask how to detect collisions, you can do it by using the following method, whereas the "collision" parameter yields data about the collision occurring itself, like the target collider and contacts:
OnCollisionEnter (Collision collision) { ... }
See also the Unity documentation: Collider.OnCollisionEnter
I really killed few hours to try to fix this, Googling for a solution, but I could not.
I've got a vehicle, it's a go-cart, so there are no suspensions (technically there are, but the values are close to 0, to simulate the tires). The vehicle has a rigid body attached and the child object contains the 4 wheel colliders (and the model itself), as can be seen here:
https://dl.dropboxusercontent.com/u/76599014/sxc/Clipboard01.jpg
For testing, I added a short script to make the vehicle move. It's in the GameObject called "gokart":
public class carControler : MonoBehaviour {
public WheelCollider fr, fl, rr, rl;
public float performance = 50f;
void FixedUpdate () {
throttle();
}
public void throttle() {
this.rl.motorTorque = this.performance;
this.rr.motorTorque = this.performance;
} }
What happens is: the rear wheels start to rotate, as intended, but the vehicle starts moving sideways slowly. The movement speed depends on the torque amount (the wheel rotation in this case). There is no movement forward, so this is not the bug, where when you are standing on a flat surface you are drifting on the sides.
Any ideas? If you need a video or a GIF (I have to figure out how to make one) of the movement, I'll be glad to provide one.
I think you should try and apply 0 brake torque to the front wheels while applying motor torque to the rear wheels.
public void throttle() {
this.rl.motorTorque = this.performance;
this.rr.motorTorque = this.performance;
this.fr.brakeTorque = 0f;
this.fl.brakeTorque = 0f;
}
That being said, anything could go wrong if the Rigidbody/wheelcolliders aren't set up correctly. Unity's wheel colliders can be difficult to set up and work with. Unity changed the physics in Unity 5 so most documentations are outdated.
I found this very good short document that was made by unity: http://unity3d.com/profiles/unity3d/themes/unity/resources/downloads/beta/unity-5.0-user-guide.pdf
It highlights the changes that was made to unity 5 and at the end of page 5 you can find a section that explains how to set up a car. I have tried it about a month ago in a new unity project and it worked. the instructions are clear so try this tutorial out and I hope it will help.
Regards
Not having the image available I'm not a totally sure about the situation but my assumption based on your description is you should try increasing the sideways friction and tweak the forward friction a little bit according to your taste. You have to increase the sideways friction anyways as you are making a go-cart. As far as I know, most arcadey go-carts don't drift and behave a lot different from regular racing cars.
Hope that will solve the problem.
So I am having an odd problem just now. I wrote a small script that, when atatched to an object will cause it to face the mouse pointer. However, since I switched from an orthographic camera to a perspective camera, the script has ceased to work. I have added in some debug and it looks like the ScreenToWorldPoint is just returning the same value no matter where the mouse is. I suspect this has something to do with the mouse being a fundamentally 2D entity, but I am not sure how to solve the problem.
Any help is much appreciated! :)
void Update () {
Vector3 difference = camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
//Debug.Log(Camera.main.ScreenToWorldPoint(Input.mousePosition));
difference.Normalize();
difference.Set (difference.x, difference.y, 0);
transform.up = difference;
This is addressed in unity answers.
Unity Answers say, and I quote:
ScreenToWorldPoint receives a Vector3 argument where x and y are the
screen coordinates, and z is the distance from the camera. Since
Input.mousePosition.z is always 0, what you're getting is the camera
position. The mouse position in the 2D screen corresponds to a line in
the 3D world passing through the camera center and the mouse pointer,
thus you must somehow select which point in this line you're
interested in - that's why you must pass the distance from the camera
in z. If you try something like this:
function Update() {
var mousePos = Input.mousePosition;
mousePos.z = 10; // select distance = 10 units from the camera
Debug.Log(camera.ScreenToWorldPoint(mousePos));
}
you will get the world point at 10 units from the camera.
Please use google before posting a question. There is a high change that you will find your answer before posting here.
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 8 years ago.
Improve this question
How do I create a body [A ball Body] that will bounce around the screen, never losing or gaining speed, regardless of what it hits in cocos2d-box2d?
Set restitution of the fixture to 1, and friction to 0.
Box2D manual says:
Restitution is used to make objects bounce. The restitution value is usually set to be between 0 and 1. Consider dropping a ball on a table. <...> A value of one means the ball's velocity will be exactly reflected. This is called a perfectly elastic collision.
A friction value of 0 turns off friction
Without friction and with perfectly elastic collision your ball will bounce around the screen, never losing or gaining speed in a static environment. If environment is not static, then colliding with moving object will change speed of the ball.
To solve this problem, I suggest next trick. Set contact listener, and in PostSolve method correct speed of you ball like this:
void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse)
{
if(contact->GetFixtureA()->GetBody() == YOUR_BALL_BODY ||
contact->GetFixtureB()->GetBody() == YOUR_BALL_BODY)
{
float speed = YOUR_BALL_BODY->GetLinearVelocity().Length();
float koef = YOUR_NEEDED_SPEED / speed;
YOUR_BALL_BODY->SetLinearVelocity(koef * YOUR_BALL_BODY->GetLinearVelocity());
}
}
How to set contact listener see there.
Periodically multiply the velocity of the body by the scalar quantity t/v.length where t is target velocity.
The other answer is likely a bit more complex than what you need.