- Home /
Rotate object's local up towards a vector.
Hey I'm working on a 2D game and am trying to figure this out.
What Im trying to do is when an object collides with another Object A's local up (Y) axis points towards the position of Object B.
Any help with the math on this one :D?
Answer by whydoidoit · Feb 16, 2014 at 05:14 AM
You need to set the "up" to be the vector from one object to the other:
var direction = (objectHit.transform.position - objectA.transform.position).normalized;
objectA.transform.up = direction;
Or:
objectA.transform.rotation = Quaternion.FromToRotation(Vector3.forward, Vector3.up) * Quaternion.LookRotation(direction);
This worked perfectly, thank you! I used the first option you posted. Is there anyway to do this over time so it doesn't pop to that rotation? Thanks!
You need to Lerp towards the direction:
Using the first method this might work:
objectA.transform.up = Vector3.Lerp(objectA.transform.up, direction, Time.deltaTime * speedFactor);
I'd normally use the second method which would look like:
var targetRotation = Quaternion.FromToRotation(Vector3.forward, Vector3.up) * Quaternion.LookRotation(direction);
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, Time.deltaTime * speedFactor);
$$anonymous$$ight also be worth trying the Slerp method, too.
In this case it would be hard to quantify the difference since it's using the "fudgy" smoothing of a Lerp towards a value using the time step - if you were to ensure that the smooth movement happened over a fixed period of time (perhaps by using a coroutine) then Slerp would provide an easing effect rather than a hard start stop.
Thank you for this. I have been using NGUI for years and have often wanted to point a UI element not along the z axis at something in the scene and this works great for that. Here is some code to have an arrow follow the edge of the screen and point at a target using UGUI.
public Transform target;
public Camera mainCamera;
public Transform ship;
Image arrow;
void Start () {
arrow = GetComponent<Image> ();
}
void Update () {
//Check used to see if item is on screen
Vector3 screenPos = mainCamera.WorldToViewportPoint (target.position);
//Get Vector to target from ship
Vector3 distance = ship.position - target.position;
//Get left, right, up and down variance and use UI width and height to deter$$anonymous$$e position
float leftRight = Vector3.Dot (distance.normalized, target.right);
float upDown = Vector3.Dot (distance.normalized, target.up);
leftRight = $$anonymous$$athf.Clamp(-leftRight*480, -375, 375);
upDown = $$anonymous$$athf.Clamp(-upDown*800,-88, 215);
//Assign position
transform.localPosition = new Vector3 (leftRight ,upDown,transform.localPosition.z);
//Look at target using the right arrow. Invert the normalized vector because the arrow moves //around the screen area.
transform.right = -distance.normalized;
//If target is on screen show it if now hide it
if (screenPos.x > 0 && screenPos.x < 1 &&
screenPos.y > 0.2f && screenPos.y < 1) {
arrow.enabled = false;
} else {
arrow.enabled = true;
}
}
Your answer
Follow this Question
Related Questions
clamp rotation by mouse direction 0 Answers
Define circle using 3 points 0 Answers
Detect GUI rotation degree 1 Answer
How can I launch a ball given an angle in degrees and a magnitude? 2 Answers
Rotating camera around the sphere 2 Answers