Use MoveRotation to Look At Another Object Unity3d - c#

Basically I am looking for a simple way for using the rigidbody/physics engine to have my ship look at another object (the enemy). I figured getting the direction between my transform and the enemy transform, converting that to a rotation, and then using the built in MoveRotation might work but it is causing this weird effect where it just kind of tilts the ship. I posted the bit of code as well as images of before and after the attempt to rotate the ship (The sphere is the enemy).
private void FixedUpdate()
{
//There is a user on the ship's control panel.
if (user != null)
{
var direction = (enemyOfFocus.transform.position - ship.transform.position);
var rotation = Quaternion.Euler(direction);
ship.GetComponent<Rigidbody>().MoveRotation(rotation);
}
}
Before.
After.

Well the Quaternion.Euler is a rotation about the given angles, for convenience provided as a Vector3.
Your direction is rather a vector pointing from ship towards enemyOfFocus and has a magnitude so the x, y, z values also depend on the distance between your objects. These are no Euler angles!
What you rather want is Quaternion.LookRotation (the example in the docs is basically exactly your use case ;) )
var direction = enemyOfFocus.transform.position - ship.transform.position;
var rotation = Quaternion.LookRotation(direction);
ship.GetComponent<Rigidbody>().MoveRotation(rotation);

Related

How To Rotate An Object Around Pivot Point in Unity?

I'm currently using this code:
public Transform Target;
public float speed = 20f;
public GameObject ME;
private void Update()
{
if (Vector3.Distance(ME.transform.position, Target.transform.position) < 5)
{
Vector3 direction = Target.position - transform.position;
Quaternion rotation = Quaternion.LookRotation(-direction);
transform.rotation = Quaternion.Lerp(transform.rotation, rotation, speed * Time.deltaTime);
}
}
Pretty much what it's doing is rotating an object based on where the target is. So if the target goes to the right the object is always facing it.
Now, I'm trying to achieve something like in this video, I'm trying to add edge fixed objects, where they rotate based on where the target is, but one of the edges is fixed to the wall, almost like a page in a book. I've found code to use another object, and use that as a pivot point, but I was hoping for a more straightforward solution that will allow me to use a boolean. For example, if the edge was on the right, rightFixed would be true, if left then leftFixed would be set to true. I figured moving the pivot point would be simplest solution, but now I am not exactly sure... how could I get that sort of functionality implemented?
Huge thanks for any help guy!

Attempting to add a Dash movement option using Unity and C#

I'm making a very simple platformer game, not to publish or anything like that but rather to experiment with Unity and C#, and I've been trying to make a dash mechanic. two ways that I tried to go about this were
Getting the players position and teleporting them in any one direction, depending on the direction of the dash, didn't work because I couldn't figure out how to find the player's position
Making the player move fast in any one direction, didn't work because of how the rest of the movement script works.
I would prefer to use the first option, does anyone know how to find the players location? I think I was able to find the transform position, but I didn't know how to use it since it was 3 values, x, y, and z, rather than one, and I didn't know how to only get 1. Thanks in advance!
Not a definitive answer, since this depends on the code you are using and i have not shown how to dash, there is a lot of camera code and i am not coding unity anymore, so guessing this out without tests seem wrong, i would recommend adding the code, but the first option is simple enough to an answer.
In the player script, use transform.position, this will not fail since all Unity GameObjects have a world position, and therefore a transform.
// not sure if i spelled correctly
public class Player: Monobehaviour {
/* ... */
void Dash () {
// transform.position is the current position as a 3D vector
var pos = transform.position;
// to access its x, y and z do this:
var x = pos.x;
var y = pos.y;
var z = pos.z;
}
}

Constrain rigidbody movement to spline at high speeds / sharp curves

I have a 2.5d platformer game. The character is using rigidbody movement on a spline (using the curvy splines asset) which curves into 3d space in all sorts of ways, while the camera stays fixed to the side so that you see the path and background turning, but maintain a 2d side scrolling perspective.
I'm essentially creating a look rotation based on the spline, then moving the player using that forward vector, and making sure to remove any velocity perpendicular to the path so that the player stays centered on the path even when curving. I'm removing the velocity on that vector instead of projecting all the velocity in the direction of the path so that the player can still jump and fall like normal.
void SetLookRotation()
{
// get nearest TF and point on spline
Vector3 p;
mTF = Spline.GetNearestPointTF(transform.localPosition, out p);
// Get forward and up vectors of point on spline
_localHorizontal = Spline.GetTangentFast(mTF);
_localVertical = Spline.GetOrientationUpFast(mTF);
// Set look rotation to path
transform.rotation = Quaternion.LookRotation(Vector3.Cross(_localHorizontal, _localVertical), _localVertical);
}
void Movement()
{
Vector3 m = transform.right * groundAcceleration * moveInput;
rb.AddForce(RemoveCrossVelocity(m));
rb.velocity = RemoveCrossVelocity(rb.velocity);
Vector3 localVelocity = transform.InverseTransformDirection(rb.velocity);
localVelocity.z = 0;
rb.velocity = transform.TransformDirection(localVelocity);
}
Vector3 RemoveCrossVelocity(Vector3 v)
{
// get magnitude going in the cross product / perpindicular of localHorizontal and localVertical vector
// (essentially the magnitude on "local Z" or to the sides of the player)
Vector3 crossVelocity = Vector3.Project(v, Vector3.Cross(transform.right, transform.up));
// and remove it from the vector
return v -= crossVelocity;
}
The first 2 functions are happening in FixedUpdate() in the order shown.
The problem is, when hitting sharp corners at high speeds, some inertia causes the player to deviate off the center of the path still just ever so slightly, and a lot of that momentum turns into upward momentum, launching the player upwards. Eventually the player can fall off the path completely (I do have a custom gravity acting towards the spline though). It works perfectly at lower speeds though, even when dealing with sharp corners. At least as far as I can tell.
I tried a bit of code from https://answers.unity.com/questions/205406/constraining-rigidbody-to-spline.html too but no luck.
Is there a way I could constrain the player rigidbody on a vector that is not one of the global x/y/z axes? I've tried a host of other solutions like setting the transform of the player towards at the center of the spline but I can't seem to get it without feeling very jerky. Using forces makes the player "rubber band" towards and past the center back and forth. Maybe there is something in my math wrong. In any case, I'm hoping someone could help me make sure that the player will always stay on the center of the spline but only on the vector to the sides of the player's face direction, so that it doesn't mess with jumping. Thank you very much in advance!
For potential future visitors, I have figured this out. There are a few components (and a lot more if you're trying to do full spline based physics, but just to start with movement...)
First we must orient our character, so that our local coordinate system can be referenced with transform.right etc. Luckily this package provides these functions which return useful vectors. I'm sure there is math beyond me to do this otherwise if you are building your own spline system.
void SetLookRotation()
{
// get nearest TF and point on spline
Vector3 p;
playerTF = currentSpline.GetNearestPointTF(transform.localPosition, out p);
// Get forward and up vectors of point on spline
_localHorizontal = currentSpline.GetTangentFast(playerTF);
_localVertical = currentSpline.GetOrientationUpFast(playerTF);
// Set look rotation to path
transform.rotation = Quaternion.LookRotation(Vector3.Cross(_localHorizontal, _localVertical), _localVertical);
}
Here I am setting a velocity directly but if you're using forces it's the same principle.
if (Mathf.Abs(localVelocityAs_X) <= maxDashSpeed * Mathf.Abs(moveInput))
{
Vector3 m = transform.right * maxDashSpeed * moveInput;
rb.velocity = RemoveCrossVelocity(m);
}
localVelocityAs_X is defined as (ran in fixedUpdate/ physics step):
float currLocalVelocityX = (playerTF - prevPositionX) / Time.deltaTime;
localVelocityAs_X = Mathf.Lerp(localVelocityAs_X, currLocalVelocityX, 0.5f);
prevPositionX = playerTF;
Where playerTF is your position on a spline (in this case, using the curvy spline package from the unity asset store. Those spline positions return very small floats so in my case I multiplied playerTF by around 10,000 to make it a more easily readable metric). This is essentially just manually calculating velocity of the player each frame by comparing current position on the spline to last frame's.
RemoveCrossVelocity is the same as above. Comment explanations should suffice.
Vector3 RemoveCrossVelocity(Vector3 v)
{
// get magnitude going in the cross product / perpendicular of local horizontal and local vertical vectors
// (essentially the magnitude on "local Z" of the player)
Vector3 crossVelocity = Vector3.Project(v, Vector3.Cross(transform.right, transform.up));
// and remove it from the vector
return v -= crossVelocity;
}
Finally the solution to the drift. My crude fix was essentially to just adjust the player to the center of the spline every frame. Horizontally, there is no change because it grabs the closest spline point which is calculated by this package to be sort of a float clamped between the start and end of the spline. Vertically, we are being set to the distance the player is from the spline in the local up direction - a fancy way of saying we're not moving vertically at all. The reason this must be done is to avoid the spline vertical position overwriting the players, and we obviously can't set this vector back to playerPos.y in our local coordinate space, so we must resort to using a direction vector * the distance from our everchanging floor. This isn't absolutely ideal at the end of the day, but it works, and there isn't any extra jitter from it (interpolate on your player's rigidbody and some camera dampening helps). All in all these together combine to make a player able to accelerate quickly around sharp corners of a spline with physics and intertia will never cause the player to fly off or drift from the center. Take that, rocket physics!
void ResetPlayerToSpline()
{
Vector3 P; //closest spline point to player
float pTf = currentSpline.GetNearestPointTF(transform.position, out P);
playerHeight = Vector3.Distance(transform.position, P);
transform.position = P + (transform.up * Vector3.Distance(transform.position, P));
}
Ultimately for those possibly looking to do some kind of implementation in the future, the biggest thing you'll run into is a lack of cardinal direction, global oriented axis-based functions and properties normally provided by a game engine. For a primer, here are a few I would use (not including gravity, which is simply opposite your up vector times whatever magnitude):
This one allows you to create a vector using x and y like normal (and z in theory) and run this function to convert it when you actually use the vector in a local space. That way, you don't have to try and think in directions without names. You can still think of things in terms of x and y:
Vector3 ConvertWorldToLocalVector(Vector3 v)
{
Vector3 c;
c = transform.right * v.x + transform.up * v.y;
return c;
}
This is basically the same as what is happening in RemoveCrossVelocity(), but it's important to reiterate this is how you set velocity in a direction to 0. The second part shows how to get velocity in a certain vector.
void Velocity_ZeroY()
{
rb.velocity -= GetLocalVerticalVelocity();
}
public Vector3 GetLocalVerticalVelocity()
{
return Vector3.Project(rb.velocity, _localVertical);
}
Getting height, since you cannot just compare y positions:
height = Vector3.Distance(transform.position, P);
I think that's all the good stuff I can think of. I noticed a severe lack of resources for created spline based physics movement in games, and I'm guessing now it's based on the fact that this was quite an undertaking. It has since been brought to my attention that the game "Pandemonium"(1996) is a curvy 3d spline based sidescrolling platformer - just like mine! The main difference seems to be that it isn't at all based on physics, and I'm not sure from what I can tell if it has pitch changes and gravity to compliment. Hope this helps someone someday, and thank you to those who contributed to the discussion.

Projectile Trajectory : Reaching specific coordinates

I am trying to implement a function in my game which will auto-lock a target and throw a projectile so that it lands perfectly on it.
I did the maths to calculate a parabola from Player 1 -> Target wherever their positions are but realised I wanted to use Unity's physics system rather than having the ball follow a path.
The throw velocity is constant, Player 1 and Target are moving objects but their positions will be registered once only to calculate the initial angle of the throw.
I believe this is the formula I need to use:
But how can I apply it for my Player and Target both having 3D coordinates?
Here is the pseudo-code of what I tried to write in Unity to make more easily readable.
float velocity = 100f;
float g = Physics.gravity;
Transform x = Target.position.x - Player.position.x;
Transform y = Target.position.z - Player.position.z;
double theta;
theta = **big formula using the values above**
And after that I do not know how to use this value to add force to the projectile.
I wanted to use AddForce(x,y,z, ForceMode.Impulse); but I clearly cannot use an initial angle here, only an x and y value.
Using RigidBody.velocity = Vector3(vx, vy, vz); gives me the same problem.
I believe I am missing something trivial but I really am stuck on this. Would anyone be able to help?

Affecting an object's position through another object's rotation in Unity

I have a floating gameobject (brickRot) that looks at what object is beneath it through a raycast, and then changes that object's y-position (floorY) based on brickRot's rotation. However, I'm having trouble converting rotation into a useful metric for changing Y-position. I want the current rotation of brickRot to be equal to whatever the y-position of floorY is, and then only change relative to the rotation of brickRot (if that makes any sense).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FloorRotation : MonoBehaviour {
private float brickRot;
private float floorY;
void Update () {
brickRot = transform.rotation.y;
RaycastHit hit;
Ray floorRay = new Ray(transform.position, Vector3.down);
if(Physics.Raycast(floorRay, out hit)) {
if(hit.collider.tag == "floor") {
floorY = hit.transform.position.y;
floorY = floorY + brickRot;
Debug.Log(floorY);
}
}
}
}
Think of brickRot as a plate you put down on the table, and when you rotate the plate, you change the table's y-position (if that analogy makes it any less confusing). The reason why I'm using a raycast is because there are multiple "tables" that the brick can find, and it needs to differentiate between them. If anyone has an idea of how to make this any easier, I would be forever thankful!
The rotation values you get are in Quaternion angles, not the common 360 degree Euler rotations. Unity, or any other game engine, uses Euler only for representation for us, but it never uses it internally for any sort of rotation calculations.
I am going to suggest not delving into deeply understanding Quaternions. But instead, think of how else you could achieve it.
For a suitable metric, add a child gameobject, rotated by 90 in x or y or z(check this, it depends on how you've oriented your parent) and some distance away from parent, to brick gameObject. Now when you rotate the brick, the child will automatically move in y, following a circular movement around the parent. You can use the child's Y as a metric for updating floorY.
use "eulerAngles" value instead , it'll convert the quaternion to an vector3
then you use angle function of vector3 to get some float, you can multiply the float with another value if you need bigger/lower scale
brickRot = angle = Vector3.Angle(transform.rotation.eulerAngles, transform.forward);

Categories

Resources