Unity3d Make Object look at touch location - c#

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

Related

How to make a freelook camera to lookat and follow object through what the player is looking at?

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);

Grappling hook ray cast direction problems in Unity

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.

Unity: Gravity in all directions

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);

Unity C# make camera sway back and forth while keeping direction

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.

making my player sprite land on top of my platform sprite

in my XNA game(im fairly new to XNA by the way) i would like to have my player sprite land on top of a platform. i have a player sprite class that inherits from my regular sprite class, and the regular sprite class for
basic non playable sprite stuff such as boxes, background stuff, and platforms. However, i am unsure how to implement a way to make my player sprite land on a platform.
My player Sprite can jump and move around, but i dont know where and how to check to see if it is on top of my platform sprite.
My player sprites jump method is here
private void Jump()
{
if (mCurrentState != State.Jumping)
{
mCurrentState = State.Jumping;
mStartingPosition = Position;
mDirection.Y = MOVE_UP;
mSpeed = new Vector2(jumpSpeed, jumpSpeed);
}
}
mStartingPosition is player sprites starting position of the jump, and Position is the player sprites current position. I would think that my code for checking to see whether my player sprite is on top of my platform sprite. I am unsure how to reference my platform sprite inside of the playersprite class and inside of the jump method.
i think it should be something like this
//platformSprite.CollisonBox would be the rectangle around the platform, but im not
//sure how to check to see if player.Position is touching any point
//on platformSprite.CollisionBox
if(player.Position == platformSprite.CollisionBox)
{
player.mDirection = 0;
}
Again im pretty new to programming and XNA, and some of this logic i dont quite understand so any help on any of it would be greatly appreciated:D
Thanks
If player.Position is a Point and CollisionBox is a Rectangle, you could use
if (platformSprite.CollisionBox.Contains (player.Position))
I was playing around with something like this just a few days ago.
What you're calling a CollisionBox I called a BoundingBox. A BoundingBox is a Rectangle which represents the area occupied by the sprite.
You'll probably find it helpful to define a BoundingBox for your sprites instead of just using their position.
You can easily test for the collision of Rectangles using the following code:
if (player.BoundingBox.Intersects(platform.BoundingBox)
{
// handle collision here...
}
For this to work, make sure that the X and Y coordinates of your BoundingBox are correctly reflecting your sprite's position.
You may want to look at the Platformer Starter Kit. It includes this as well as detecting when the player is touching an enemy, collecting gems, etc.
The answer about BoundingBox is right, but IMHO it is easier to use Rectangle if you are making a 2D game rather than BoundingBox, wich is designed for a 3D one.
Anyway both objects have intersection / collision test methods that takes Vector2 or Point as parameter.
Now you have to have a box for your player, and a box for your platform. If one collide with the other, you have to check from where (the player can hit the platform from up, down, left, right, or maybe up & left on the left up corner).
If the player is on top of the platform, then just stop his fall, probably by setting his Y-speed component to 0.
Maybe you will need the player to go through the platform when jumping (player hit from bottom) but not when falling (player hit from top).

Categories