I've seen other threads that want to make turrets rotate back and forth, however my question is more complicated because I want to allow the player to control his/her camera while the sway is affecting it. This is to simulate the feeling of recoil when the player gets hit by an attack.
Ideally, I'd like to have the sway first up-right then down-left and gradually return to center using something like Mathf.Sin(timer * Time.deltaTime * scalar)
I've been able to get the camera to sway using Quaternion functions but when doing so, the camera becomes level with the ground until the sway is complete, or the player is locked facing north while the camera shakes.
What code can I use to perform the tasks above?
My camera instance is named mainCamera,
The way I've done this in the past is to have a GameObject that the camera is a child of.
Position and rotation of the parent will affect the child relative to the parent, but the player can still have control over the camera itself. You can then have the parent node swaying, but without the code on it clashing with the camera itself moving and rotating based upon player interaction.
Related
it's a bit tricky.
the problem is that the target object is moving and bouncing so if i will make the freelook camera to lookat or follow the moving object the camera will also bounce and i don't want it.
what i want to do is the first part just like it is now the camera is moving behind the player while the player is looking at the moving target. then when the camera is behind the player the moving object is getting out of the screen area. the player head is still looking at it but the camera keep looking and follow the player.
i want somehow to make that the camera will rotate looking at the moving object but from the player point of view and not that the camera will follow/lookat the moving object.
here is a screenshot at the point the moving object(in red circle) is start moving out of the screen :
now i'm not sure how to move to the next part.
at this point i want the camera to stay behind the player and rotate looking at the moving object "like" through the player eyes.
i tested it again now and while the object is moving and the camera is behind the player like in the screenshot i can rotate the camera with the mouse like rotating the camera with the player head looking at the moving object the question how to do it by script ?
i want to rotate the camera with the player head to look at the moving object through the player point of view so the camera will not get the moving object bouncing and other effects.
This screenshot explain what i mean that the camera is still looking at the player and following the player but viewing with the player at the moving object. in this case i'm rotating the freelook camera with my mouse but i want to do it in the script.
well not sure what you are using currently as there is no code provided here ... but instead of directly passing in the target object's position simply use a vector where x and z come from your target object while y comes from your player object like e.g.
// not necessarily the player itself but the usual target object for your camera
Transform player;
Transform camera;
Transform lookTarget;
camera.position = /*Just the usual way how you get your camera position*/;
var targetPosition = lookTarget.position;
targetPosition.y = player.position.y;
// Snapping see https://docs.unity3d.com/ScriptReference/Transform.LookAt.html
camera.LookAt(targetPosition);
// or if you are e.g. using smooth interpolation
var targetRotation = Quaternion.LookRotation(targetPosition - camera.position);
camera.rotation = Quaternion.Slerp(camera.rotation, targetRotation, 5f * Time.deltaTime);
Hey team I'm currently working on a 3rd person game where I would like to fire grappling hooks from the player to a point that is offset from the center of the camera.
I have a screen overlay canvas with Ui images for crosshairs. When the left shift is held down the crosshairs move outward from center along the x axis and return to center once shift is released, a bit like crosshair spread in games except I want to trigger the spread via the shift key. These crosshairs are meant to dictate the location the anchors of the grappling hook land, originating from the player and hitting whatever object is directly forward of the crosshairs. (imagine attack on titan ODM gear if you've seen it). I am looking for a way to ray cast from the player to the point forward of these crosshairs while they're offset from the center.
So far I have the grappling system set up and working but am having trouble with the direction parameter when I use the crosshair spread. It separates fine but where the hooks land in relation to the cross hairs are obviously out as I'm trying to use angle calculations at the moment instead of what is forward of these reticles.
I'm basically wondering if it is possible to use these screen overlay UI images to cast forward from or if there's a better way to accomplish the same thing. I have my doubts because they're screen overlay so I imagine their coordinates wont be attached to the camera as they appear.
Thanks in advance for your help.
What I would do is determine the location of the reticleson the screen, then (as Aybe suggested) use ScreenPointToRay or ViewportToRay (depending on if it's easier to get a pixel position or a fractional position of each reticle) to Physics.Raycast that ray from the camera into the scene to find where the rays collide. At this point, you have two world positions the player seems to want to shoot the hooks.:
Vector3 hookTarget1;
Vector3 hookTarget2;
So, now you actually have to fire the hooks - but as you know the hooks aren't being shot from the camera, they are being shot from the player, and they maybe offset by a bit. Let's call the originating points (which may have the same value):
Vector3 hookOrigin1;
Vector3 hookOrigin2;
So, then you can create Rays that originate from the hook origins and point at the targets:
Ray hookShot1 = new Ray(hookOrigin1, hookTarget1 - hookOrigin1);
Ray hookShot2 = new Ray(hookOrigin2, hookTarget2 - hookOrigin2);
Using these rays, you can do another Physics.Raycast if you would like, to confirm that there aren't any trees or other obstacles that are between the player and the target location - and if there are, that may be where the anchor should actually sink:
Vector3 anchorPoint1;
Vector3 anchorPoint2;
The segment between the origin of these rays and these anchor points would be appropriate for rendering the cable, calculating physics for how to move the player, as well as checking for collisions with world geometry that might cause the anchor to release.
I am trying to make a game with magnets. Each magnet has its own magnetic field which pulls the player towards the magnet.
The player should then be able to walk alongside the magnet on its entire side.
The player should not be pulled towards the center.
Right now I have done this by adding velocity to the player towards the magnet, this works fine.
The issue right now is that the player should rotate with his feet towards the magnet. Right now I can't figure out how to change the rotation of the player based on the rotation of the magnet. Which results in the player being on its side or upside down on the magnet in some case.
I am also using a third-person camera from unity standard assets, sometimes when the player is rotated the camera can only look up and down. To fix this I use a rotate function around World.Space.
You should get the position relative, the magnet as parent and your player as child. To do that you could create a different gameObject with the same position as the player, and set it as child to the magnet (or have a child on the magnet which you change its position (absolute position)).
Once you've done that, you normalize the localposition of the magnetChild and you make the player lookAt (there's a function in Unity) this vector + player.position. And you'll have to rotate the player 90ยบ from the x-axis (to make that not the front of the player is looking to it, but the head).
I haven't tried it, but give it a try maybe it works. Something like:
magnetChild.position = player.position;
player.LookAt(player.position + magnetChild.localPosition.normalized);
player.Rotate(90.0f, 0.0f, 0.0f);
I'm currently making an android game in unity3d. I want to make it so when you touch the screen the cube looks at the touch location and moves toward it. I tried doing a lookat script to the touch position but the rotation is weird and it doesn't move toward the touch.
You can use Unity's built in Navigation System to make your object move from one point to the other, use a Ray to get the point that the player clicked on the screen, and use Transform.LookAt() to make your player look at that point.
Navigation
Raycasting
Transform.LookAt
I am playing around with Unity3D and adding physics to the objects in my scene. I currently have a sphere (planet) in the center of the screen, and I have another sphere (moon) positioned outside of it that is not moving. When I run the game I want the moon to orbit the planet by applying a force to it. I have been able to get the force added by calling rigidbody.AddForce() and that will move it in the specified direction but it won't orbit. I'm not sure how to add force to it so that it will continuously orbit the sphere at a constant velocity.
I've tried some examples using a ConfigurableJoint and it orbits, but it starts out bouncing a little and then starts the orbit. My goal is to have a bunch of orbiting moons orbiting at their own speed that are able to bounce off eachother but not lose their velocity.
Any ideas?
Generally speaking you will fail, eventually, because rounding errors in your integration method will slowly throw you out of orbit. You can get very close in the ways suggested, but you could consider doing something more like the Kerbal Space Program, which seems to precalculate the orbit as an ellipse and then follow that ellipse until it has a reason to believe it should stop, rather than strictly "simulating" the orbit ...
If a collision occurs, allow normal physics to resolve the collision, and then recalculate your new orbit based on the result and start following that.
For the moon to orbit you would need to give the moon an initial velocity. Then have it accelerate towards the planet, that is a constant force.
gameObject.rigidbody.AddForce(1, 0, 0);
gameObject.constantForce.relativeForce = Vector3(0, 1, 0);