How to make a transform follow a target transform on it's local z axis only?
I'm trying to make Object A follow Object B, but I need Object A to only follow on it's local forward z axis, and retain it's initially set x and y axis. I thought this would be an easy solution, but I'm having difficulty.
I've only seen posts that say to use transform.forward * Time.deltatime. But this will not work in my case since I need it to follow Object B.
I have tried:
Vector3 followPosition = transform.InverseTransformPoint(targetPosition);
followPosition = new Vector3(transform.localPosition.x, transform.localPosition.y, followPosition.z);
transform.localPosition = followPosition;
Thank you!
Answer by Hellium · Mar 07 at 11:29 PM
transform.position += Vector3.Project(targetPosition - transform.position, transform.forward);
Your answer
Follow this Question
Related Questions
i have a problem 2 Answers
how do i fix the compiler errors on unity 5? i have tried everything there is to try. 0 Answers
my box movements look jitter, how to fix it 0 Answers
How to move both objects when my player is mounted in another moving object? (eg: Skate, Car, Bike) 0 Answers
Finger Swipe on Object 2 Answers