So i’m trying to make an object rotate and move to a mouse clicked location on a plane.
So far I go it to move to that location but without rotation, and I also got it to rotate but it doesn’t move.
My problem is if i get Quanternion.Lerp to rotate the object, it doesn’t move the object. When I use Vector3.Lerp or .MoveToward it move the object but doesn’t rotate.
I try having the 2 code together, but it doesn’t seem to work, only 1 work and the other is skip.
***I have a separate ScreenToWorldPoint script that take the mouse input via Transform.
public Transform targetClick;
private Vector3 target = new Vector3(0, .5f, 0);
public float speed = 0.3F;
//Use this for initialization
void Start()
{
transform.rotation = Quaternion.Euler(0, 0.5f, 0);
}
void Update()
{
transform.rotation = Quaternion.Lerp(transform.rotation, targetClick.rotation, speed);
transform.LookAt(targetClick.position);
transform.position = Vector3.Lerp(transform.position, transform.position, speed);
}
I want it to either rotate and move to the clicked location OR moving steer rotate.
thanxs
OK, unfortunately you are abusing Lerp for both rotation and rotation. You want to use Vector3.MoveTowards, and Quaternion.RotateTowards, in place of both of these Lerps. Or, since it seems you want to rotate only around Y, you could just keep track of your Y rotation angle, and move this towards the target with Mathf.MoveTowardsAngle.
Next, there is no point in both setting the rotation (via Quaternion.RotateTowards or Quaternion.Lerp), and then calling LookAt. LookAt sets the rotation directly. If you want to smoothly rotate towards the clicked position, then don’t use LookAt.
Also, are you sure targetClick.rotation is what you think it is? If you use this as your rotation target, then you’re trying to match the rotation of the target. That’s not the same as rotating to face the target.
Here’s one solution:
public Vector3 target;
public float moveSpeed = 0.3f;
public float rotSpeed = 360f;
void Update() {
// rotate towards the target
Quaternion targetRot = Quaternion.LookRotation(target - transform.position);
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRot, rotSpeed * Time.deltaTime);
// move forwards
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
}
This turns (gradually, but quickly) towards the target, and moves forward the whole time. Since “forward” will quickly mean “toward the target,” this ends up homing in on the target. It looks pretty natural for something like a vehicle or guided missile.
Of course there are other variations you could do, which move forward only when facing the target, or move towards the target even when not facing it… I don’t know what sort of creature or machine you’re trying to make here, so I can’t say what’s best. But this ought to get you started.
1 Like
I tried this and replace the Vector3 Target is Transform target and take target.postion.
But it only spin the cube in 1 spot and doesn’t move.
This create an orbit around a radius oh the object.
The problem i’m doing is: in Unity
Let’s say i have an object (a cube) resting on the flat surface of a plane. I want to move the cube from from A to point B, point A being where the cube is resting. Point B is anywhere you click on the flat surface plane. I already have a working ScreenToWorldPoint script.
I just can’t get the cube to rotate and move forward to the spot. I can get it to rotate, or get it to move but without rotation using Vector3.MoveToward
I don’t understand how it can both spin without moving, and create an orbit around the object (i.e., moves). Which is it?
If it’s orbiting the object, that means that the turn rate is not fast enough relative to the movement speed. Increase the rotSpeed, or decrease movementSpeed.
Of course when it finally gets very close to the target, it will orbit it. That’s unavoidable unless you put in something to stop it when it’s close enough. For example, you might add:
if (Vector3.Distance(transform.position, target) < 0.1f) {
// we're there!
transform.position = target;
return;
}
…to the top of the Update method, so once it’s close enough it just snaps to position and stops.
how would you have this take the vectors from the ScreenToWorldPoint ? the x,y,z coordinates
P.S Thanks a lot of taking the time out to help me on this
**it or rotate base on speed changes
Somewhere you have code that’s detecting the mouse click, and converting that to a world point with ScreenToWorldPoint. The result of that is a Vector3. So then you just need to get a reference to the above movement script (perhaps with GetComponent), and then assign this Vector3 to its .target property.