Making an Object faster in unity3d - unity3d

I'm making a 2D game in unity and I'm stuck. I have a rigidbody2d projectile that is fired based on the click position. I want to make this projectile faster without changing its trajectory or changing the timescale of the entire world and I cant change the gravity scale. Any ideas ? Thank you in advance :)

give the object less mass, for one. But if you are using a Vector for velocity so.
Where ever you are setting the velocity of the bullet in the script, add this (in JavaScript)
var speed = 2.5; // or what ever you want it to be, play with it
// as long as you multiply both x and y the same, they will keep their direction.
gameObject.rigidbody2D.velocity.x *= speed;
gameObject.rigidbody2D.velocity.y *= speed;

Related

How to make object pull another in Unity 2D

So I'm trying to simulate orbital mechanics in Unity 2D. I have a ship and Moon model in scene. I calculated the sphere of influence, and if the distance between the Moon and ship is smaller than sphere of influence, the gravity will effect. Problem is;
I can't get gravity affect working.
I've tried using AddForce method, but since it requires Vector2 elements and I calculate my force as float (I use Newton's law of gravitation formula and I get a float), I don't know how to include my float force in Vector2 force.
force = (GravitationalConstant * ((planetMass * ship.GetComponent<Rigidbody2D>().mass)/Mathf.Pow(Vector3.Distance(ship.transform.position,transform.position),2)))/(realityConstant * forceReducer);
if (Vector3.Distance(transform.position,ship.transform.position) < SOI/realityConstant){
ship.GetComponent<Rigidbody2D>().AddForce(new Vector2((float)force,0f));
}
This code makes Moon pull the ship to itself only from left side. When ship passes Moon, it keeps pushing it to right. Not to itself.
I need a fix, that makes Moon pull the ship to itself everytime, with a specific force. How do i make this happen?
Any idea would be helpful.
Thanks a lot!
Force is applied using a vector. You have the magnitude of your vector but not its direction. You can calculate a direction by subtracting moon position from the ship position:
ship.GetComponent<Rigidbody2D>().AddForce((Moon.transform.position -
ship.transform.position).normalize * force);

Unity: Calculate angular velocity of rigidbody to always face specific direction

I'm trying to make a pick up objects mechanic like the one in Amnesia. It's easy to calculate needed rigidbody's velocity, so that the held object stays in front of camera, but my problem is that the object doesn't rotate at all when I hold it. And I would rather have it always be rotated towards the camera. This could easily be achieved with simply parenting the object to player's camera, but...
The behaviour I'm after is as follows: if the bottle I picked up was standing on a table, with neck of the bottle facing ceiling, I would like to see this bottle always with its neck facing ceiling while I hold it. But if this bottle collides with something, it should behave like it actually bumped onto something, so it should rotate some small amount, but it should always try to return to its "original" rotation (in this case, neck facing ceiling).
I think that I need to calculate angular velocity for that and probably have some lerp to return to original rotation, but I'm at a loss on how to do that properly.
I think that the first thing I would need to do is to store the initial direction the moment player picks object up:
Vector3 targetDirection = playerCamera.transform.position - transform.position;
Script is on the held object, so "transform" refers to it. In FixedUpdate() I probably need to have some interpolation, so that angular velocity always tries to rotate the object to original rotation:
rigidbody.angularVelocity = Vector3.Lerp(rigidbody.angularVelocity, targetAngularVelocity, lerpSpeed * Time.fixedDeltaTime);
I don't know how to calculate targetAngularVelocity, because after all I would like the held object to return to original rotation smoothly. I'm not even sure if that's the right way to do this thing and perhaps I should do something else than to calculate angular velocity needed to rotate object properly. I tried just interpolating localRotation to original local rotation, but that did not allow the held object to bump on stuff (the movement then was very jittery). Any ideas?
You need a stabilizer. A script which will add torque/angular velocity to the object, whose angle is different from the target one. Say, you have two variables: targetDirection and currentDirection aka transform.forward. Then you write something like this in fixed update:
var rotation = Quaternion.FromToRotation(currentDirection, targetDirection).eulerAngles * sensitivity;
rigidbody.angularVelocity = rotation;
I recommend to set sensitivity about 0.05 and then increase it if the object stabilizes too slow.
Probably I confused the order, so you should put minus somewhere, but the approach itself is applicable.

Unity - How to Smooth Out Constant Velocity on Rigidbody2D

I am setting constant velocity for my character movement in my 2D game. However since using the method my character seems to be shaking a little. Is there a way to fix this and smooth the movement out?
Here is I set the velocity in the Update function:
constantVelocity = new Vector3 (playerInputX * speed, playerInputY * speed, 0);
I then apply this velocity to the the Rigidbody2D component in the FixedUpdate function.
It turns out that my problem had nothing to do with the object I was moving but the camera that was following it. The camera was trying to Lerp towards the object in Update. I changed this to FixedUpdate and it now works perfectly.
Thanks for the help anyway,
Tommy
There are two ways to do this.
You can always use Time.fixedDeltaTime to smoothen your player movement.
You can try low pass filtering on our constantVelocity.

How can i give soccer ball a trajectory?

I am a beginner and trying to make penalty shooter Game in unity.. I have just setup the scene and just trying to shoot the ball towards the goal post.
When i shoot the ball it goes toward the goal but does not come down, because i am shooting it through the script and its gravity is off and kinematic is on.
Currently i have the following script:
void Start () {
startTime = Time.time;
rb.GetComponent<Rigidbody>();
}
void Update () {
transform.position -= fakevel * Time.deltaTime;
transform.position += fakeGravity * Time.deltaTime;
fakevel *= 0.999f ;
}
void OnTriggerEnter( Collider other ) {
fakevel = new Vector3(0.01f, 0, 0) * 2000f;
fakeGravity = new Vector3 (0 ,0.01f, 0)* 200f;
y = -45.68312f;
}
}
I have tried enabling the gravity and disabling the kinematic at some specific position but i does so the gravity just pull it down at that position and it does not look realistic. Some screen shoots are attached.
Please help me in setting trajectory of the ball and stopping it when it collides with the goalpost,
I would suggest you use a non-kinematic rigidbody on your ball and add a force to it when you shoot it: GetComponent<Rigidbody>().AddForce(forceVector3, ForceMode.Impulse) (this is just an example, you should avoid calling GetComponent on a frequent basis, you are better off calling it for the rigidbody component once on Start and store it in a private variable)
This way Unity's physics engine can handle gravity and the balls' velocity over time. To modify the trajectory you can play around with the properties of the balls' rigidbody component.
If you want to have more control over the velocity, you can also set the rigidbody's velocity directly in the FixedUpdate() (which is called right before each physics update). When doing so, you have to consider that velocity is measured in m/s and the physics engine handles sub-steps for you (I am assuming that FixedUpdate() is called more than once per second), meaning you do not have to multiply your velocity with some delta time (but I am not 100% sure about this right now).
Seeing how it does work, just doesn't give the effect you want, you can either spend several hours fine tuning it, or like xyLe_ stated, use the built in physics. Using the built-in physics isn't a bad thing, you just gotta be smart with it. You can add a bouncy physics material also, which will help give the ball a well, ball feel.

How to use AddForce and Velocity together?

I have the jump component who use the AddForce for jumping and the movement component who move left and right using the Velocity.
If you don't move the character when your are jumping the jumping will be fine but when you move the character and jump at the same time then the movement component will break the jumping because the velocity is setting up a Vector2 point where define the Y axis too. I tried to use the current Y axis from the transform component in the movement but even that doesn't work.
What I should do for fix the problem between AddForce and then use Velocity?
It seems your move function is creating a new velocity vector and overwriting the existing one.
Vector2 velocityVector = rigidbody.velocity;
velocityVector.x += movement * force;
rigidbody.velocity = velocityVector;
This will retain the existing velocity, both X and Y, and modify it. You will of course need to add deceleration (usually I use something along the lines of if(grounded) velocityVector.x *= 0.999f;, but I'm sure more fancy maths exists for more realistic deceleration) and some kind of maximum speed (again, I keep things simple and use similar to if(velocityVector.x > maxSpeed) velocityVector.x = maxSpeed;).
Rigidbody.AddForce has the following definition:
public void AddForce(Vector3 force, ForceMode mode = ForceMode.Force);
One of the options available for ForceMode is ForceMode.VelocityChange:
Add an instant velocity change to the rigidbody, ignoring its mass.
Apply the velocity change instantly with a single function call. In contrast to ForceMode.Impulse, VelocityChange will change the velocity of every rigidbody the same way regardless of differences in mass. This mode is useful for something like a fleet of differently-sized space ships that you want to control without accounting for differences in mass. In this mode, the unit of the force parameter is applied to the rigidbody as distance/time.