Get difference between rotations and positions - unity3d

I've got two objects in World Space.
One is a cube has no parent.
The second is a triangle and it has a parent.
I change the cube's position and rotation.
And now I need to put cube at its first position but move triangle in the parent (local) in such position to fit the same position as if the cube won't be placed at the previous position.

Somwhere store the original position and rotation of cube
Vector3 origPosition = cube.transform.position;
Quaternion origRotation = cube.transform.rotation;
Get the offset values between cube and triangle
Vector3 posOffset = triangle.transform.position - cube.transform.position;
Quaternion rotOffset = Quaternion.Inverse(cube.transform.rotation) * triangle.transform.rotation;
(Re)Set cube and Triangle into place
cube.transform.position = origPosition;
cube.transform.rotation = origRotation;
triangle.transform.position = origPosition + posOffset;
triangle.transform.rotation = origRotation * rotOffset;
Example
public class CubeMover : MonoBehaviour
{
public Transform cube;
public Transform triangle;
private Vector3 origPosition;
private Quaternion origRotation;
// Start is called before the first frame update
private void Start()
{
origPosition = cube.transform.position;
origRotation = cube.transform.rotation;
}
[ContextMenu("Test")]
public void ResetCube()
{
Vector3 posOffset = triangle.transform.position - cube.transform.position;
Quaternion rotOffset = Quaternion.Inverse(cube.transform.rotation) * triangle.transform.rotation;
cube.transform.position = origPosition;
cube.transform.rotation = origRotation;
triangle.transform.position = origPosition + posOffset;
triangle.transform.rotation = origRotation * rotOffset;
}
}
(Had no Triangle so I used the Cylinder ... I hope that's fine for you ^^)

Related

Unity3D: World Rotation to Local Rotation

I'm really struggling with this. I have a turret that is mounted on a spaceship. It's a child object of the ship. The turret is allowed to point left, right, up, but not down, because that would be pointing through the spaceship.
So, I need to limit the rotation of the turret so it won't point down. I started with this code:
`
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TurretScriptTest : MonoBehaviour
{
public float rotateSpeedBase = 1f;
public float rotateSpeedCurr = 1f;
public float yMin = 0;
public float yMax = 1;
public Transform target;
// Start is called before the first frame update
void Start()
{
rotateSpeedCurr = rotateSpeedBase;
}
// Update is called once per frame
void Update()
{
if (target && target.gameObject.activeSelf)
{
// Rotate
RotateWithLock();
}
}
public virtual void RotateWithLock()
{
if (target)
{
Vector3 targetDir = target.position - transform.position;
float step = rotateSpeedCurr * Time.deltaTime;
Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, step, 0.0f);
newDir.y = Mathf.Clamp(newDir.y, yMin, yMax);
transform.rotation = Quaternion.LookRotation(newDir);
}
}
}
`
This works great except for one thing. The spaceship is also rotating all over the place. As soon as the parent spaceship goes off its original axis the above code is worthless. The turret will point through the spaceship or whatever else is in the way.
I'm assuming that what I need to do is convert everything to local rotation and position so that "down" for the turret will always be its base, not "down" in world space. So I try this code:
`
public virtual void RotateWithLock()
{
if (target)
{
Vector3 targetDir = target.position - transform.position;
float step = rotateSpeedCurr * Time.deltaTime;
Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, step, 0.0f);
Vector3 newDirLocal = transform.InverseTransformDirection(newDir);
newDirLocal.y = Mathf.Clamp(newDirLocal.y, yMin, yMax);
transform.localRotation = Quaternion.LookRotation(newDirLocal);
}
}
`
Now the turret doesn't move at all. What to do?
If your turret's gun is a child of the turret's base, then you can just use "transform.localRotation" to get the rotation relative to the base.
If not, then try this:
If you have the Transform of the base, then you can use "Quaternion.Inverse" to get the inverse of the base's rotation. Then just take the LookRotation and multiply it by this inverse (Quaternion1 * Quaternion2 just adds them together). To convert that to a Direction Vector for the clamp calculations, just do "Quaternion * new Vector3(0, 0, 1)"
Vector3 targetDir = target.position - transform.position;
//Gets rotation to the target
Quaternion rotToTarget = Quaternion.LookRotation(newDirLocal);
float step = rotateSpeedCurr * Time.deltaTime;
//Gets new rotation
Quaternion newRot = Quaternion.RotateTowards(transform.rotation, rotToTarget, step);
//Gets the local rotation by adding (yes adding) the inverse of the base rotation
//Make sure to set the turretBase variable to the turret base's GameObject
newRot = newRot * Quaternion.Inverse(turretBase.transform.rotation);
//Gets a vector that points to the direction
Vector3 dirFromRot = newRot * new Vector3(0, 0, 1);
//This sets the y-limit. The yLimit should be less than 1.
//I am using your code, but a more effective way would be to limit the angle of the quaternion.
dirFromRot.y = Mathf.Clamp(dirFromRot.y, -yLimit, yLimit);
//Gets the LookRotation of this clamped Vector3 and sets the result.
transform.localRotation = Quaternion.LookRotation(dirFromRot.normalized);

2D Rigidbody with Touchscript movement in Unity wont stay in boundaries

I'm trying to make a pong game where the player has to move both paddles. They are currently set as dynamic 2D Rigidbodies(collision detection continuous) and have a box collider (is NOT trigger) attached.
The problem is the paddles won't collide with the surrounding box colliders I set on the camera, using the following script :
using UnityEngine;
using System.Collections;
namespace UnityLibrary
{
public class EdgeCollider : MonoBehaviour
{
public float colDepth = 4f;
public float zPosition = 0f;
private Vector2 screenSize;
private Transform topCollider;
private Transform bottomCollider;
private Transform leftCollider;
private Transform rightCollider;
private Vector3 cameraPos;
// Use this for initialization
void Start () {
//Generate our empty objects
topCollider = new GameObject().transform;
bottomCollider = new GameObject().transform;
rightCollider = new GameObject().transform;
leftCollider = new GameObject().transform;
//Name our objects
topCollider.name = "TopCollider";
bottomCollider.name = "BottomCollider";
rightCollider.name = "RightCollider";
leftCollider.name = "LeftCollider";
//Add the colliders
topCollider.gameObject.AddComponent<BoxCollider2D>();
bottomCollider.gameObject.AddComponent<BoxCollider2D>();
rightCollider.gameObject.AddComponent<BoxCollider2D>();
leftCollider.gameObject.AddComponent<BoxCollider2D>();
//Make them the child of whatever object this script is on, preferably on the Camera so the objects move with the camera without extra scripting
topCollider.parent = transform;
bottomCollider.parent = transform;
rightCollider.parent = transform;
leftCollider.parent = transform;
//Generate world space point information for position and scale calculations
cameraPos = Camera.main.transform.position;
screenSize.x = Vector2.Distance (Camera.main.ScreenToWorldPoint(new Vector2(0,0)),Camera.main.ScreenToWorldPoint(new Vector2(Screen.width, 0))) * 0.5f;
screenSize.y = Vector2.Distance (Camera.main.ScreenToWorldPoint(new Vector2(0,0)),Camera.main.ScreenToWorldPoint(new Vector2(0, Screen.height))) * 0.5f;
//Change our scale and positions to match the edges of the screen...
rightCollider.localScale = new Vector3(colDepth, screenSize.y * 2, colDepth);
rightCollider.position = new Vector3(cameraPos.x + screenSize.x + (rightCollider.localScale.x * 0.5f), cameraPos.y, zPosition);
leftCollider.localScale = new Vector3(colDepth, screenSize.y * 2, colDepth);
leftCollider.position = new Vector3(cameraPos.x - screenSize.x - (leftCollider.localScale.x * 0.5f), cameraPos.y, zPosition);
topCollider.localScale = new Vector3(screenSize.x * 2, colDepth, colDepth);
topCollider.position = new Vector3(cameraPos.x, cameraPos.y + screenSize.y + (topCollider.localScale.y * 0.5f), zPosition);
bottomCollider.localScale = new Vector3(screenSize.x * 2, colDepth, colDepth);
bottomCollider.position = new Vector3(cameraPos.x, cameraPos.y - screenSize.y - (bottomCollider.localScale.y * 0.5f), zPosition);
}
}
}
The ball collides with the paddles and bounces off as it should as well as collides with the surrounding box colliders and bounces off perfectly. However, the paddles move right through the surrounding box colliders (EdgeColliders). Please note that I use Touchscript package from the unity asset store to control movement. It does not move the Rigidbody it uses transform. Another thing to note is that when I make the paddles very light (0.0001 mass) and add gravity to them, they do collide with the edge colliders and don't go through the screen.
If you want the paddle to collide with the surrounding box, you should move the Rigidbody with AddForce(), not move the transform.
Or you can limit the movement of the paddles like this :
if (transform.position.x < LeftScreenEdge)
{
transform.position = new Vector3(LeftScreenEdge, transform.position.y);
}
Delete Rigidbody2D attached to paddles.
Get the mouse pos int world coordinates:
Vector2 mP = Camera.main.ScreenToWorldPoint(Input.mousePosition);
And to prevent it from going out of boundaries just clamp It's position:
Vector3 screenBounds;
void Start()
{
screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
}
void SetPaddlePos()
{
Vector2 clampedPos;
clampedPos.x = Mathf.Clamp(mP.x, screenBounds.x, -screenBounds.x);
clampedPos.y = Mathf.Clamp(mP.y, screenBounds.y, -screenBounds.y);
paddle.transform.position = clampedPos;
}

Transforming Unity coordinates

I have three objects at A,B,C in Unity. I want to transform their coordinates to a new coordinate system which has point A as the new origin (0, 0, 0), AB as the line along the new x-axis, and AC as the line along the new y-axis. How do I get the new coordinates of A, B, C in this new coordinate space?
I looked at transform.InverseTransformPoint(), but I'm not sure how to make it work here.
The simplier - non-maths - solution would be
Place the objects A,B,C under the same parent object
Translate and rotate the parent object instead of all objects individually => Let Unity do the maths for you
Optionally when done place them again where there where before - or if you want to be able to still change their localPosition and localRotation keep them under the rotated parent.
Otherwise use e.g. Transform.TransformPoint without parenting
I added both to an example manager
public class CoordinateManager : MonoBehaviour
{
[Header("References")]
public Transform objectA;
public Transform objectB;
public Transform objectC;
[Header("Coordinate-System Settings")]
public Vector3 A;
public Vector3 AB;
public Vector3 AC;
[ContextMenu("Apply New Coordinates")]
public void ApplyNewCoordinates()
{
// just for making sure this transform is reset
transform.position = Vector3.zero;
transform.rotation = Quaternion.identity;
transform.localScale = Vector3.one;
// Make this object parent of objectA,objectB and objectC keeping their current transforms
// For reverting it later store current parents
var parentA = objectA.parent;
var parentB = objectB.parent;
var parentC = objectC.parent;
objectA.SetParent(transform);
objectB.SetParent(transform);
objectC.SetParent(transform);
// place this object to the new pivot point A
// and rotate it to the correct axis
// so that its right (X) vector euqals AB
// and its up (Y) vector equals AC
// Unity will handle the rotation accordingly
transform.position = A;
transform.right = AB;
transform.up = AC;
// Optionally reset the objects to the old parents
objectA.SetParent(parentA);
objectB.SetParent(parentB);
objectC.SetParent(parentC);
}
// Use this method to place another object in the coordinate system of this object
// without any parenting
public void SetPosition(Transform obj, Vector3 relativePosition)
{
// sets the obj to relativePosition in the
// local coordinate system of this rotated and translated manager
obj.position = transform.TransformPoint(relativePosition);
// adjust the rotation
// Quaternions are added by multiplying them
// so first we want the changed coordinate system's rotation
// then add the rotation it had before
obj.rotation = transform.rotation * obj.rotation;
}
// Only for visualization of the pivot point A and the
// AB(red) and AC(green) axis in the SceneView
private void OnDrawGizmos()
{
Gizmos.color = Color.white;
Gizmos.DrawWireSphere(A, 0.1f);
Gizmos.color = Color.red;
Gizmos.DrawLine(A, A + AB);
Gizmos.color = Color.green;
Gizmos.DrawLine(A, A + AC);
}
}

how to limit and clamp distance between two points in a Line renderer unity2d

I am making a game which let you click on a ball and drag to draw a line renderer with two points and point it to a specific direction and when release I add force to the ball,
for now, I just want to know how can I limit the distance between those two points like give it a radius.
You can simply clamp it using a Mathf.Min.
Since you didn't provide any example code unfortunately here is some example code I made up with a simple plane with a MeshCollider, a child object with the LineRenderer and a camera set to Orthographic. You probably would have to adopt it somehow.
public class Example : MonoBehaviour
{
// adjust in the inspector
public float maxRadius = 2;
private Vector3 startPosition;
[SerializeField] private LineRenderer line;
[SerializeField] private Collider collider;
[SerializeField] private Camera camera;
private void Awake()
{
line.positionCount = 0;
line = GetComponentInChildren<LineRenderer>();
collider = GetComponent<Collider>();
camera = Camera.main;
}
// wherever you dragging starts
private void OnMouseDown()
{
line.positionCount = 2;
startPosition = collider.ClosestPoint(camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, transform.position.z)));
var positions = new[] { startPosition, startPosition };
line.SetPositions(positions);
}
// while dragging
private void OnMouseDrag()
{
var currentPosition = GetComponent<Collider>().ClosestPoint(camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, transform.position.z)));
// get vector between positions
var difference = currentPosition - startPosition;
// normalize to only get a direction with magnitude = 1
var direction = difference.normalized;
// here you "clamp" use the smaller of either
// the max radius or the magnitude of the difference vector
var distance = Mathf.Min(maxRadius, difference.magnitude);
// and finally apply the end position
var endPosition = startPosition + direction * distance;
line.SetPosition(1, endPosition);
}
}
This is how it could look like
I've written the following pseudo code, which may help you
float rang ;
Bool drag=true;
GameObject ball;
OnMouseDrag () {
if(drag) {
//Put your dragging code here
}
if (ball.transform.position>range)
Drag=false;
else Drage=true;
}

How do I add the rotation, scale and translation of my object to my vertex in Unity 3D

I want to get a vertex and store it in a variable. Then I want to keep updating the position, rotation and translation. Ik know I can just add up the position of the object to the position of the vertex to get the new vertex position. But I don't know how to do this with the scale and rotation. Does anyone know?
using UnityEngine;
using System.Collections;
public class FollowVertex : MonoBehaviour {
public Transform testSphere;
public Transform indicator;
void Update()
{
indicator.position = GetVertex();
}
Vector3 GetVertex()
{
Mesh TMesh = testSphere.GetComponent<MeshFilter>().mesh;
Vector3 Tvertex = TMesh.vertices[1];
return Tvertex + testSphere.position ; // + the rotation, scale and translation of "testSphere"
}
}
Thanks in advance!
Edit 1:
I think I can modify this RotateAround funtion for the rotation.
function RotateVertexAround(center: Vector3, axis: Vector3, angle: float){
var pos: Vector3 = transform.position;
var rot: Quaternion = Quaternion.AngleAxis(angle, axis); // get the desired rotation
var dir: Vector3 = pos - center; // find current direction relative to center
dir = rot * dir; // rotate the direction
transform.position = center + dir; // define new position
// rotate object to keep looking at the center:
var myRot: Quaternion = transform.rotation;
transform.rotation *= Quaternion.Inverse(myRot) * rot * myRot;
}
Now the Only thing I need to know is how to do the scale, can I do that with a matrix?
You should be able to do that via the TransformPoint() method, which transforms a vertex from local to world space.
i.e.
Vector3 GetVertex()
{
Mesh TMesh = testSphere.GetComponent<MeshFilter>().mesh;
Vector3 Tvertex = TMesh.vertices[1];
return testSphere.TransformPoint(Tvertex);
}