Forever moving ball [closed] - iphone

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.

Related

How to change game speed in mid development without touching the Timescale in Unity (2D)?

I have a game similar to Sand balls and I was wondering how can I speed up the game a bit without touching the timescale? A nice and somehow correct approach would be to scale down all my objects so gravity can also "affect" the drop movement. I already tested that and works as expected, except I have to apply that for 100+ levels... and would break some prefabs (a mass level editor to scale by bounding box would work but that's another story)
On the other hand, I have timescale but feels like it's the incorrect approach since it also affects animations and leads to unwanted behaviors.
So... do you know any other ways to speed up the game?
If objects is falling down, you can increase gravity. Or without touching timescale, you can just create a public speed multiplier variable and set it to 1 at start.
If you only move balls (Assumed from give sand balls reference), just multiply speed variable of ball with public speed multiplier variable. When you want to change the speed, just change the variable. As an example:
var ballSpeed = baseBallSpeed * speedMultiplier;

Object Sticks To The Floor When It Moves [closed]

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.

Object not rotating properly in unity? [closed]

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 2 years ago.
Improve this question
I can't really explane whats going on so here is the video:
I can't explain this
Something went wrong with the rotation axis? I don't know what cause it. I don't know how to fix it.
Its like someone messed something with the axis? I even don't know how to do that...
Please help me!
Don't worry, it's because you modify the value in Transform, in fact, the changed localPosition and localRotation are relative coordinate values; when this parameter is changed, it is relative to the parent node.
The objects we see in Scene are Position and Rotation, which are the values ​​in world coordinates, which are the relative coordinates plus the parent node's value in world coordinates.
So all you need to do is reset the Transform of the GameObject's parent node to the initial value.
then the modification of the relative coordinates will be consistent with the world coordinates.

Make Physics more accurate

I make a 2D-game. In this game, I have a projectile, which I move the fires by myself like this:
void Update()
{
gameObject.transform.position = new Vector3(
gameObject.transform.position.x + baseVelocity * Time.deltaTime,
gameObject.transform.position.y + baseVelocity * Time.deltaTime,
gameObject.transform.position.z);
}
And also, I use void OnTriggerEnter2D(Collider2D other) to know when collisions occurs.
The problem is when game running on a weak phone with 30-FPS it won't detect collision, while same fire in an 60-FPS phone will collide.
I think this is because the fires move 2x more in 30-FPS phones.
One option was use FixedUpdate() method for moving fiers, but it gave me jerky movement, and I used Update() method for moving because it gives me smooth movement(in both of them I used Time.deltaTime).
Can you please suggest me how make more accurate while using smooth movement?
like move object in Update() method but check object position in more(offset) positions than where it is!
I forget to note that I changed the value of Fixed TimeStep to 0.01 for getting more accurate physics.
Thanks in advance.
edit:
I finally end up with using FixedUpdate() for move objects by myself, for so many reasons I can't use Physics engine, I set FixedUpdate TPS(tick per second) around 60 to some how be match with my 60 FPS Update (and still can't figure out why increase FixedUpdate TPS will make object movement jerky!).
And will keep this question open for finding better answer.
You seem to be doing the physics manually. The issue with manually updating the position is that you don't know about the position of your projectile between frames. Even if you were to use FixedUpdate, you could still be in the situation where two FixedUpdate calls cause your projectile to miss an object.
So instead of manually updating the position, you should use a RigidBody2D, with the collision detection set to "Continuous". A continuous collision detection will interpolate object collision in between physics update cycles, so you hit your target even at very high speeds. Also, this solution does not depend on frame rate.
Note that the above will only work if you don't manually update your positions yourself. That is because the physics engine uses velocity to calculate collision in between physics update cycles.
So instead of updating the position yourself (which is bad for physics), use forces instead.
Or alternatively, if you don't want to mess with forces, update the velocity instead:
GetComponent<RigidBody2D>().velocity = baseVelocity;

Best way to smoothly move a character from one point to another [duplicate]

This question already has answers here:
Move GameObject over time
(3 answers)
Closed 4 years ago.
I am trying to make a game where if the user taps the screen their character moves from one point (say left side of the screen) to another point (right side of the screen) smoothly.
I know you can do this a few ways: Coroutines, Lerp or even an animation. I wouldn’t mind code examples as well..
What are some different ways to achieve this?
Thanks in advance!
Well best is mostly opinion based and not a good way to ask actually... anyway
What the other answer didn't mention:
Of course the smoothness doesn't come from Vector3.Lerp nor Coroutines but actually from the Time.deltaTime.
This Time.deltaTime value is the real time passed since the last frame. This allows to do all kind of smooth and especially always taking the same amount of real time movements and actions independent of the frame rate.
In terms of the specific usecase of movements I personally would prefer the usage of Vector3.MoveTowards over Vector3.Lerp because the later gets slower and slower as it gets closer to the target. (At least if you use the transform.position as first value as often done in examples)
Here is the example provided by the Unity scripting API
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public Transform target;
public float speed;
void Update() {
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, target.position, step);
}
}
Instead of target.position of course you can also just define a Vector3 as target position.
If you need collision detection than both shouldn't be used because you might just move the object to far in one step so the collision isn't triggered.
In this case you might find a hint in this post