I need to know if there is an established way to do this, given that what defines screen boundaries depends on the device, etc. I have these rigid bodies that are not kinematic and not affected by gravity with light masses. When I instantiate them, they are all in an empty with a sphere overlapping them to create an "explosion" effect.
This works well, however after the sphere pushes the rigid bodies apart and explosion has occurred, I need the bodies to ALL keep moving until out of the users view/the screen. This happens for most of them but they are some stragglers and some take a very long time, staying in center of the screen.
I would normally apply a force but I do not know for each rigid body (there are a lot) which way they would need to move depending on where they are.
How can I apply a force that gets all these rigdibodies off the screen? The explosion is supposed to be in all directions. Is there a certain Vector that would do this?
Project the position of the object onto the camera's facing vector. And then make a vector from the projected point, back to the object.
So, something like..
var t = Vector3.Dot(Camera.main.transform.forward, obj.transform.position); //distance along view vector
var p = Camera.main.transform.position + Camera.main.transform.forward * t; // position along view direction
var d = obj.transform.position - p; //vector from center to object
Related
I am developing a game that uses a third person camera view, character centralized, no issues with that. But I can't find a way to "lock" on another object while keeping my character in view (Rocket League ball cam: https://youtu.be/FDcO04gXihM ).
I am aware of lookAt() method but it's not enough on its own. I still need to calculate the position of the camera to avoid staying between the player and the target. The problem: http://imgur.com/a/MdO9m
This is what I'm currently doing to move the camera (the "free cam"):
if (freeCam) {
transform.position = Vector3.Lerp(transform.position, camTarget.transform.position, speed);
transform.rotation = Quaternion.Lerp(transform.rotation, camTarget.transform.rotation, speed);
} else {
// Lock cam
}
The camTarget is a game object parented to the player.
I now understand what your problem is and try to explain the solution.
Let's say you have the player object as PO and the target object TO and the camera object CO.
So when you want to achieve, to follow with the camera view always the target object and keep the player object in between, all you have to do ->
The CO needs to travel around the PO in an orbit (orbital camera) with a fixed distance.
Next create a vector from the point TO to PO (the center of the orbit of CO)
Then calculate the 2 hit points of the vector (TO|PO) with the orbit. You now have an enter hit point ENP and an exit hit point EXP.
As of vectors have always a direction the EXP will always be the further point if you keep the direction from TO to PO.
Set the CO position to EXP and lookat at TO
Apply an fixed offset to EXP corresponding to the world.upvector to always sit above the PO.
[optional] If surface exists, do a collision detection with EXP against the surface. If hit, set EXP to collision detection point, and set PO render to false. As soon EXP is no longer colliding with surface set PO render to true.
[optional] If want to achieve to always have a good view rather then always a good look at PO, take the angle of the forward vector of CO. If it is quarter I of IV (position of a angle in math) you can assume, that ur currently just seeing the PO and not TO because the camera sits right beyond PO and looks to TO right through PO. If this happens set PO render to false. As soon as CO forward vector angle is quarter II-IV set PO render to true.
That's it. I will try to give u more in depth code tonight, because right now I'm at work.
Maybe you could check if the camera's position is between the player and the target and just rotate it 180ยบ
I'm making a unity 3D game, part of the game allows the player to get into a car and drive it.
Inside the car I have put a "seat" GameObject whose position and rotation is used to determine where the player will sit.
When I want the player to sit in the car, I make the car the players parent, then set the player transform position and rotation to that of the seat.
Here is the C# code
// make player's parent the same as the seats parent (which is the car)
transform.parent = seat.transform.parent;
// put player in the exact position and rotation as the seat
animator.transform.position = transform.position = seat.transform.position;
animator.transform.rotation = transform.rotation = seat.transform.rotation;
animator.transform.localPosition = transform.localPosition = seat.transform.localPosition;
animator.transform.localRotation = transform.localRotation = seat.transform.localRotation;
This seems like it should work, but what ends up happenning is that for some reason the player does not end up perfectly in the seat, but some short distance away from it, and also the players rotation doesnt match the seat, instead ends up slightly off. So the player looks like he is not really in the seat but floating near it, and turned around in some other direction.
Anybody knows what I'm doing wrong?
position specifies an object's location in the world. If you take two objects and assign the same position, then they will both be in the same place.
localPosition specifies an object's location relative to its parent. If you take two objects and assign the same localPosition, the outcome will depend on the position of each object's parent. If they have different parents, different scaling, or different rotation, they may end up in different places.
Roughly the same idea applies to rotation and localRotation.
You seem to have confused the relationship between world-space and local-space coordinates. It's very important to understand the difference, and how they relate to the scene hierarchy.
If you want to put both objects in the same place, this should suffice:
transform.position = seat.transform.position;
transform.rotation = seat.transform.rotation;
If you want the object to move with the car, afterward, you could also reassign its parent:
transform.parent = seat.transform.parent;
Check your player object's pivot point.It might not be on position you expect.If it is not, try to make it point where you want by dragging your model.
I currently have an item placement system for building. It works by instantiating a "ghost" of the object that shows where it can be placed, the ghost (semi-transparent) object is attached to the camera and instantiates the object in its place when the player clicks.
I get the position at which to keep the ghost object like so:
var pos = transform.position + transform.forward * placeDistance; // A position 'someDistance' in front of the player
pos.y = Terrain.activeTerrain.SampleHeight(pos); // Get the position at the surface of the terrain to place the object
firePlaceable.transform.position = pos + Vector3.up * 0.001f; // 'halfHeight' is used in case the pivot is not on the base
Now.. I need the object to rotate according to the terrain so that the fire place is placed more or less correctly rotated. Any ideas? What would the best plan be?
Use the terrain normal vector at the place' position.
For example you could do a raycast straight down from the fireplace. The resulting hit contains a normal that is your place' up vector.
By thinking of it... I assume you already doing a raycast to get the position to place the fireplace right?
Use the placement raycast to get the up vector instead of making a new one.
So basicly do
fireplace.transform.up = clickPlaceHit.normal;
I'm making a platform game and I've looked at different ways to add collisions in a tile based game without rotation. But everything I look at assumes that in no case will one of the colliding objects be going fast enough to pass the other before collision is detected. I've tried using Box2D and Farseer but they were over complicated and ran quite slowly when making lots of tiles. I also tried my own method using 2D convex hulls but that ran too slowly as well. So is there a way to to detect collisions without a maximum speed or letting items pass each other that isn't over complicated and would work with lots of tiles? any help is much appreciated
Depend on objects and their movement and whole gameplay. if you are shooting bullet to some obstacle, you can calculate if bullet will intersect with obstacle before bullet hit it. you have positions, you can calculate distance between them. so you can check collision and distance.
other way, as #cwohlman suggested. you can have 2 collision boxes, one big "assumed" collision area and one excact that cover objects. so if object triggers assumed collision, you know that there i a chance that bullet will hit object and you do some detailed calculations if button will hit object or will pass above or under it. for determinating if object is in assumed collision area you can use circle collsion.
private bool DetectCircularCollision(Sprite a, Sprite b)
{
float radius = Math.Sqrt(a.Width / 2 * a.Height / 2 + b.Width / 2 * b.Height / 2);
float distance = Vector2.Distance(a.Position, b.Position);
if (distance < radius) return true;
return false;
}
so far i didn't encounter this problem, as all flying objects should be visible to players eye. if you move object 50px or 100px per frame player will not notice this or will be confused what is this.
You will probably need to combine few collision methods to achieve what you need. Advice, do not use pixel collsion as it's completly unnecessary and it's resuorce killer. Instead, cover objects with few bounding boxes or circles.
example where blue circle is assumed collision area, and red circles are exact collision area.
You might try a dual algorithm approach, using a simple algorithm first to deturmine if the objects 'might' have collided and if if so, use a complex algorithm to see if they did collide.
I want to allow a boat in my 3D simulation of a ship to rotate and be moved on all axes. However, the way that I have the boat's movement programmed makes this impossible.
The way it moves:
this.transform.Translate(Vector3.left * Time.smoothDeltaTime * speed);
The way it turns:
this.transform.Rotate(Vector3.forward * Time.smoothDeltaTime * (int)horizontal)
The shape of the boat also makes it impossible to simply move it on a solid base, since it does have the triangular shape (for underwater collisions).
So what I want to do is allow the boat to be affected by gravity, but still float on the water. Then, when the boat hits something, it needs to be able to "roll" and then eventually return to its normal position.
So, is there any way to make an object slowly return to its normal rotation(z rotation of 0) after it hit something, and not be affected by gravity once it reaches a certain elevation. (Y value of 34.75)
The boat has a Rigidbody and a Mesh Collider
I'm not sure if the player would be able to move when he gets hit, but you could save the current values to local variables (for example: Transform transformOnHit). After you complete the roll, you can use your own provided code to translate and rotate back to the original transform.