Unity: Gravity in all directions - c#

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

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

Make GameObject's Y Rotation be the same as the camera's

I have a Cinemachine Freelook camera and i want it similar to Skyrim, where the character's rotation follows where the camera is pointing, but only on the Y axis so that i can move my mouse and look at the character from up and down;
This video demonstrates how MrKrabs can move in 3 dimensions, but won't turn.
I already tried creating a blank object and putting it at the center of the character, then using transform.LookAt(Camera) to make it point to where the camera is, and finally getting the Y value of the object's rotation, inverting it with -transform.position.y and applying it to MrKrabs, but it didn't work: it was jittery and overall just a bad user experience, is there a way to turn the gameobject based on the camera's rotation? (having the camera always watching his back)
Something like this should do the trick.
transform.rotation = Quaternion.Euler(0, Camera.main.transform.eulerAngles.y, 0);
Beware that if your camera is a child of your object it will rotate with it.

Unity Sub Camera following Player with rotation problem

Here is an image of what I'm trying to achieve
As you can tell it's a split screen game, the player is on the left, computer on the right.
there are 3 cameras in the game, main and two player cameras
the player camera MUST be independent of the player and CAN NOT be a child object of the player object, because the ball bounces and rotates while moving, the cameras must not.
when the balls change direction the camera must remain behind the player so the visual appears to show the landscape rotating with the player.
I've searched high and low for anything to put me on the right path but nothing seems to work right.
It should be a smooth transition so lerp and slerp are to slow for instant moving. I know LateUpdate will help with this.
If anyone can point me in the right direction I'd appreciate it.
Many thanks,
Paul
Have a script which takes in an object's position, in this case the player's ball, so you can code the camera as if it was a child of the object.
An simple example code for having a following camera would be something like...
FollowObject.cs
public Transform exampleObject;
private int offset = 5; //How far back the camera will be
void LateUpdate()
{
transform.position = new Vector3(exampleObject.transform.position.x,
exampleObject.transform.position.y,
exampleObject.transform.position.z - offset)
}

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.

Unity3d Make Object look at touch location

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

Categories