I run into the problem with moving object regarding to its rotation. The thing is that I'm changing object position by assigning new transform.localPosition
value. In specific cases I want to modify assigned value by slightly changing z
axis (height) of the object.
I tried to do it with use of transform.up
. I expected move along local z
axis (like in Unity scene preview), but it doesn't work as expected.
transform.Translate(Vector3.up, Space.Self)
works exactly as I want, but as I said I want to combine it with transform.localPosition
i already use. How should I modify passed Vector3 so it works correctly?
The class below is used to simulate move of an object around sphere. For example plane flying around Earth. I adjust plane rotation so it's chassis is facing Earth. Object is moving on the Earth surface though. I want to move it slightly up so it flies. That's why I want to move it along local z
axis as it is distance from the core of the Earth.
Full class snippet (I'm using LeanTouch package)
public Transform lookAtTarget;
RaycastHit hit;
RaycastHit lastHit;
public float rotationSmoothTime = 0.3F;
public float moveSmoothTime = 0.3F;
private Vector3 rotationVelocity = Vector3.zero;
private Vector3 moveVelocity = Vector3.zero;
private Vector3 lastNewRightDir;
protected virtual void Awake ()
{
Use.UpdateRequiredSelectable (gameObject);
lastNewRightDir = transform.right;
}
void Update ()
{
var fingers = Use.GetFingers ();
if (fingers.Count < 1)
{
Move (lastFingerScreenPosition, Vector2.zero);
return;
}
LeanFinger finger = fingers[0];
Move (finger.ScreenPosition, finger.ScreenDelta);
lastFingerScreenPosition = finger.ScreenPosition;
}
public void Move (Vector2 fingerScreenPosition, Vector2 scaledDelta)
{
var ray = Camera.main.ScreenPointToRay (fingerScreenPosition);
LayerMask mask = LayerMask.GetMask ("Interactable");
bool newHit = Physics.Raycast (ray, out hit, float.PositiveInfinity, mask);
Vector3 rightDir = transform.right;
Vector3 newRightDir = lastNewRightDir;
Vector3 targetPosition = lastHit.point;
if (newHit)
{
targetPosition = hit.point;
lastHit = hit;
}
else if (lastHit.distance == 0)
{
return;
}
Vector3 forwardDir = (targetPosition - lookAtTarget.position).normalized;
if (targetPosition != transform.position && scaledDelta != Vector2.zero)
{
newRightDir = (targetPosition - transform.position).normalized;
lastNewRightDir = newRightDir;
}
rightDir = Vector3.SmoothDamp (rightDir, newRightDir, ref rotationVelocity, rotationSmoothTime);
Vector3 upwardDir = Vector3.Cross (forwardDir, rightDir);
transform.rotation = Quaternion.LookRotation (forwardDir, upwardDir);
transform.localPosition = Vector3.SmoothDamp (transform.localPosition, targetPosition, ref moveVelocity, moveSmoothTime);
}}
Before transform.localPosition = Vector3.SmoothDamp (transform.localPosition, targetPosition, ref moveVelocity, moveSmoothTime);
the targetPosition
value should be adjusted so object moves 1 unit alongside local z axis.
transform.localPosition = Vector3.SmoothDamp (transform.localPosition, targetPosition, ref moveVelocity, moveSmoothTime);
The thing is thaty
axis is height of the object. During move object should be lets say 1 unit above ground. So i want to add this value totargetPosition
.Vector3.up
won't work correctly as I want to apply this 1 unit based on transform rotation, not global rotation. I thoughtransform.up
would work this way, but clearly I'm wrong. \$\endgroup\$