how do I make object face player? - c#

I've been following various StackOverflow answers, and I think I found code that works, but before I put any code, I've got to figure out whether or not how Unity knows which way is the "front" of the object.
transform.LookAt(target);
I'm trying to use above code for enemy object to face the player. When I place object into the scene, how do I let Unity know which way is the front of the object? (i.e. I place airplane object into the scene. "I" know which way is the front, but does "Unity" know which way is the front?)
Thank you for your help in advance.

From https://docs.unity3d.com/ScriptReference/Transform.LookAt.html
Rotates the transform so the forward vector points at /target/'s current position.
From https://docs.unity3d.com/ScriptReference/Vector3-forward.html
Shorthand for writing Vector3(0, 0, 1).
Front is same as forward vector and forward vector is pointing at object Z axis.

Related

Is it possible to move 2D character at a specific angle?

I have started developing 2D isometric game in Unity, but i'm struggling with the movement. I want to move my character at a specific angle. To be more specific with you and give you better idea what i like to achieve is to create movement like in "Bouncy Bits"
Here is a video of their gameplay: https://www.youtube.com/watch?v=A6xA4RzuRcM&t=19s
I would love to achieve that kind of moving to my player including him to move forward alone in that same angle without me pressing any keys.
Would really appreciate if someone could help me out, by giving me a code or explain me what i exactly need to do.
Move your character on vector.forward, which is equivalent of (0, 0, 1) on the Z axis. Put your camera on that front angle how they have it. It would look the same.

Unity 2D line collider

I try to do the following: I have a point A at -4x-4y and a point B at 4x 0y. I want to make a colliding line from position A to position B.
I tried to do it with a linerenderer but I can't get the line to collide with my other 2d objects.
My other tought was to calculate the center of the points and the rotation and do it with a box collider but that seems to be really complicated and hacky.
Is there a simple way to achieve this?
Thanks in advance
You can use PolygonCollider2D, it's automatically create collider for sprites, and if you are not satisfied results, you can edit it by clicking Edit Collider in inspector, or trought Unity's API.
I think you must have a Rigidbody2D attached with your other 2D object. Then this will work 100%. You can use any collider it doesn't matter.

Make object appear right in front of camera OCULUS

I am making a Unity game for Oculus VR using C#.
I want to test a simple jumpscare where the object appears suddenly right into your "face".
I have problems with setting the position of this object. Right now I pass players position and rotation to this function.
public void ScareMe(Vector3 pos, Quaternion rot){
girlSmiling.transform.position = new Vector3(pos.x, 0.9f, pos.z- 1.3f);
//girlSmiling.transform.LookAt (pos);
girlSmiling.transform.rotation = rot;
//other irrelevant stuff
}
I need to maintain y position for my scare (girl) because in Oculus your height is adjustable and it doesn't correspond to your environment, so I need to leave it at 0.9f.
I tried LookAt function but it doesn't work as good as I would like it to work.
The problem remains that girl appears right in front of me only when I look straight. When I move my head around, which is more likely to be a real in game situation for Oculus, she appears a bit to the right or left or even at the back.
I dont understand why it is happening. How can I set her position so she is always facing me right in front of me?
You always can try to make your scare girl being children of your camera-head object. Doing this the object will follow the camera head and rotate with it.
girlSmiling.transform.parent = ...Here you can put the transform of the Camera head.
You can position the object before parenting or after parenting them (with local position should be easier).
I hope this helps, good luck!

Unity2D - Why does the object rotate around the X/Y axis rather than the Z

I am trying to rotate an object to face another in Unity 2D. However i am having some trouble wrapping my head around the way unity uses 2D transformations and most examples I can find are designed for 3D.
Using the code below I can make the object rotate to face the right direction, however it also rotates the object so that it is the Z axis that is pointing towards it. Ideally the object would rotate around the Z axis.
rigidbody2D.transform.LookAt(currentlyTargeted.transform.position);
Any guidance would be much appreciated.
Make sure that your object's direction in the mesh/sprite is rotated correctly, or place the object in a dummy object used to offset the rotation to the X/y axes
I had a similar issue recently with LookAngle - please notice these functions take two arguments (the second one defaults to Vector3.up) - what did the trick for me is to call LookAngle like this:
LookAngle(Vector3.forward, myCalculatedPositionsDifference)
Maybe it will also help in your case?

Unity C# raycasting from center of an object to find mesh intersection

I'm trying to make a respawn system for a game in unity that starts the character back on the last platform they were on.
As it is currently, it keeps track of which platform they were last grounded on with onCollisionEnter and detects if onCollisionExit touches an out of bounds area.
I need to find the position of a face on the mesh with the y axis (assuming the best way is to do a raycast on the global y axis from the center of the platform) and add the height of the character/2 to determine where to respawn the character.
I'm very new to unity and c#, so I've never done a raycast before and I'm not sure if it's possible to raycast from inside an object to find it's mesh in a given direction, or if there is a better/more efficient way.
Thanks in advance :)
"if it's possible to raycast from inside an object to find it's mesh in a given direction"
You can place a empty gameobject at the center of your mesh (make it child of your mesh ) then pass the position of this empty gameobject to raycast origin.
I usually make re-spawn system with triggers. If you explain little bit more what actually you want to do. I'll try to guide you in that particular direction.

Categories

Resources