I have started to make Pacman and would like to know how can i write in my code the direction where the Ghost is going ? So if the transform.position.y is growing, its obvious Up Direction and so on....
This is for the Direction Change if they're hit the Wall.
Any suggestions?
It depends on how you've got your game set-up. One manual way you could go about it is by saving the position in a frame, and in the next frame you calculate the difference between the two positions (the previous frame and the actual frame), and divide that by the time that has passed (Time.deltaTime).
Another way you could go about it (and I would recommend if possible) is simply getting the Rigidbody component and checking the velocity attribute: https://docs.unity3d.com/ScriptReference/Rigidbody-velocity.html
Keep in mind, since this is a beginner's question, that the fact that an object is moving in a certain direction may not come from a ribidbody. As I said, this depends on your exact set-up.
Related
i try to to make an AI in C# (with unity) that can predict the estimated position of a moving object to hit it with a bullet
the moving object have a movement speed of 5f and the bullet have a speed of 7f
my problem is that the time my bullet travel to my estimated position my "enemy" already moved further and the bullet don't hit
do you know a formula or code that i can adapt to improve my targeting AI ? (already looking for that in google but don't find anything usefull)
thank
An answer to your question from unreal engine forums
Here is the top answer from there in case the link dies. I did not write this code I simply found it with a quick google of your problem which you stated you already tried.
Link answer:
Get the "velocity" of the target player. Multiply by the time the bullet will take to travel to the target. Then get the position of the target, add the velocity*time vector, and that's the position you should aim at. You can either hard-code the travel time (half a second, or whatever,) or you can in turn measure the distance between AI and player, and divide by bullet travel time, to come up with an approximate travel time. You can also apply a differential equation to calculate the exact time of impact and exact direction, but that requires a little more math and is slightly harder to write out, so I think the above will work best for you.
Simply:
Distance = Length(Target_Position - Firing_Position)
Time = Distance / Bullet_Speed
Predicted_Position = Target_Position + (Target_Velocity * Time)
For the past few days I have been working on some custom car physics as a coding exercise.
What my ultimate goal is to have some semi-realistic car physics something in between the quality of the older GTA games and the recent GTA 5.
Now what I got so far is a quite intricate process which eventually gives me a float which is the exact speed I want the car to go until the next frame, so far I have used the following code to do that;
this.transform.Translate (0, 0, speed * Time.deltaTime);
However because I want to use the Unity build in physics for collision detection I realised this wouldn't work because transform.Translate (as far as I know) literally places the object at that position, thus the object could (if going fast enough) suddenly be halfway stuck through a wall or just completely ignore the collision and appear on the other side of the wall. Now instead I decided to do this:
rb.velocity = transform.forward * speed;
To quickly note, I am doing both of these in my FixedUpdate and I am currently only using the second example (or at least trying to use it and am miserably failing at it). Due to it being in the fixed update I should be able to at least test it to a certain extend without Time.deltaTime usage, which is why you aren't seeing that in the second example right now.
To continue my story, for some reason when I set the velocity of the rigidbody instead of using transform.Translate my collision acts really strangely. The car becomes all floaty and nudges forward (stands on its nose) when I hit the wall, I have no clue what could be causing this seeing as I am using my own gravity (the gravity from the original Rigidbody is turned off in favor of my own custom gravity, which does result in a downward rigidbody force) and the drag, and angular drag of the rigidbody have been turned down to 0.
Basically my question is do any of you guys have an idea of what could be causing my rigidbody to be reacting in such a manner, literally the only difference between my normal code and the code that is acting up is that single line change.
I've looked in to using rigidbody.AddForce() instead but that just doesn't seem to do anything at all and on top of that the whole reason why I am writing my car script is to determine the velocity the object should be moving at thus making something like rigidbody.AddForce() which adds it to the velocity of the car more or less redundant and shouldn't be necesarry.
The only reason why I'm even using a rigidbody in the first place is so I can do two things: 1. Easy collisions. It means I can add simple forces such as gravity to it and not have to worry about it going through the floor. 2. easy rotations when affected by physics such as collisions.
If anyone has any ideas on how to do these two things in a custom script I'd much rather do that because as much as I like unity the Unity rigidbody physics are practically worthless in my eyes, however a must have because I can't write my own custom rigidbody and I can't find the source code of the Unity Rigidbody either. What I'd rather have is a stripped down version of the rigidbody which allows for easy collisions and easy rotation but without all the stuff like the build in drag or velocity because these are the exact things I want to be able to control myself and have been able to control myself so far when I use transform.Translate, but I lose this control when I have to use rigidbody.velocity.
according to the documentation you shouldn't use rb.velocity to change the velocity regularly - it could result in unrealistic physics
http://docs.unity3d.com/ScriptReference/Rigidbody-velocity.html
instead use rb.addforce
http://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html
hopefully that will solve the issue!
Replace the Translate with the following:
rb.MovePosition(transform.position + transform.forward * (speed * Time.deltaTime));
MovePosition basically acts as a translate (if you do it like this) except it also counts collisions in to it.
If you want super realistic 3D car physics, should look into ready assets like:
Edy's Vehice Physics https://www.assetstore.unity3d.com/en/#!/content/403
Also from Unite2015 talks, see example project: "EasySuspension" to build sample 3D car by script:
http://bit.ly/1y8ucNW (from video: https://youtu.be/WGvuP6vV6j4?t=17m8s )
Or is the game top down 2D game? (like early GTA's)
I'm working on a 2d spaceshooter game where the ship has boosters.
I want to determine how much force each booster needs to exert to move my ship with the maximum force in the direction it wants to go.
The ship may have any number of boosters but will probably be a small number like 2-10.
The rotations of the boosters are known but I can't determine their magnitudes.
The magnitudes can be from 0(min) to lets say 5(max).
I got to a point where I made this equation:
directionToGo = ForceB1*B1Direction + ForceB2*B2Direction
But now that I'm getting more boosters I'm just thinking, ok to find what I want I have to brute force in min/middle/max forces for each direction to find the others I don't know.
So how would I go about doing this?
Had to unanswer because I haven't figured out the answer.
This sounds to me like a constraint maximization problem. You want to maximize the force along a certain vector while applying exactly no force along the perpendicular vector.
If your thrusters aren't all pointing towards the center of mass, another constraint you need is that zero torque is applied. This makes sure the ship isn't rotated by an imbalance of forces. Some games ignore this for simplicity, though, and assume thrusters never apply torque.
One thing to note is that you only need to do the optimization when the orientation of thrusters changes: during gameplay you can just apply the ratio of thrusts to add that force vector. (Watch out, though, moving "diagonally" requires another solution: if you just try to apply forward + sideways, you will get > 100% power from a thruster, causing an imbalance.)
That is one way to go about it--there might be a simpler solution that I'm not aware of. If you can constrain your problem further, it's more likely that there's a simpler way.
I am a begginer. I am using Monogame and Farseer Physics Library in C#. (latest versions)
In my game, whenever my ball (circle) hits the corner of a rectangle, (or even another circle), it is supposed to only change direction on the Y-Axis.
However, it does not only change direction on the Y-Axis, (as intended), but it also moves a small amount to the right as well (or the left accordingly), depending on how you hit the corner (or another circle). It is as if some force is being put on the ball, making it move on X-Axis as well.
This movement is cool and all, and it makes a lot of sense, but in my game, it does not, therefore i want to get rid of it.
How is this possible ? I am guessing i have to change some default values.
This what my coding looks like:
BallBody.BodyType = BodyType.Dynamic;
BlockBody.BodyType = BodyType.Static;
Ball.LinearVelocity = new Vector(0,-1); // ball going up
BallBody.OnCollision += Ball_OnCollision;
public bool Ball_OnCollision(Fixture f1, Fixture f2, Contact contact)
{
// if the Ball (f1), collides with the Block (f2)
if (f2.Body == BlockBody)
// change the direction of the Ball on Y-Axis
Ball.LinearVelocity = new Vector(0,-1);
return true;
}
Also with high speeds, this occurs:
Even though the ball is never able to pass through the Block (tunneling), I want to know how could i possible fix that so that the ball never enters the Block area ?
Unfortunately, this is surprisingly difficult to do with Box2D. The best I could manage when I tried, was to record the velocity of the ball when the contact started, and then when the contact ends, manually set the velocity to what I wanted. That works fine when the ball only begins and end contact with one block at a time, but in some cases the ball can begin touching two blocks in the same time step. You would have to look at the arrangement of the blocks it touched, and calculate for yourself what angle you would expect it to bounce away at. Eventually I just gave up and did not bother to cover that case.
I'm trying to move a body:
if (ks.IsKeyDown(Keys.Up)) {
rocket.ApplyImpulse(new Box2DX.Common.Vec2(0, 30f), rocket.GetPosition());
}
Oddly, the body only moves when the key is released. Why is this? I've been looking at the manual but can't figure it out.
When I remove the conditional, and just have the applyImpulse() call in Step(), the rocket continually has the animation of thrusters but never actually moves. (As if I had been holding down the Up key the entire time.)
Looks like what I really need to get this working is a better understanding of what the first argument to applyImpulse() does:
new Box2DX.Common.Vec2(0, 30f)
What is the significance of the two values in the vector?
UPDATE This works much better:
rocket.ApplyImpulse(new Box2DX.Common.Vec2(0, -1), rocket.GetPosition());
It looks like if the second value in the force vector is negative, the object rises on the screen. Before, the impulse applied was just slamming it into the floor. When I released the key, it would sometimes bounce back, if the impulse had been strong enough.
Regarding your update: In XNA, depending on how you've situated your camera, negative Y is up. If you want the rocket to go up, you have to apply a force in that direction.
That makes no sense as to why it only moves when it's released, is there nothing else interfering with it elsewhere such as another keyboard input hidden somewhere? I only ask as it could be that another keyboard input statement in conflicting with another and causing the problem.