Object rotation and movement help

I’m working on some simple movement animation and need to know how best to perform the task.

I have an object move over time to a set vector3, once there it turns to face a second point and moves to that location. I’m happy will everything except the abrupt rotation. I’d like to rotate quickly but noticeably and avoid facing one direction while moving in another. I don’t want to halt movement while turning and the movement should be a natural change of direction as in driving a car. Start position, primary target position and secondary target positions are all random. I don’t understand how to break up rotate into smaller turn and move steps. Can someone shed some light on a good way to do this. I might be using the wrong search terms but can’t find anything useful. Don’t need code unless that is the easiest way to describe the solution, generalities are fine.

It sounds to me like you want to just turn quickly to face the target, while continuously moving forward. That means that at first (right after selecting a new target), you may be moving briefly in the wrong direction, but that will quickly be corrected. The result will be pretty natural-looking movement (which you can tweak by adjusting the turn rate).

So, how to do it?

If this is an essentially 2D case, I’d use one of those SignedAngleTo methods from the other thread to figure out which way your object “should” face in order to face the target. And then use Mathf.TurnTowardsAngle to turn a little bit (limited by turnRate * Time.deltaTime) each frame. And combine this with steadily moving forward.

If it’s a fully 3D case, then use Quaternion.LookRotation to work out the correct way to face the target, and Quaternion.RotateTowards to turn a little bit towards that on each frame.

Thanks once more Joe,

I’m not sure if I got off on the wrong foot again or what. I’m using this to move my object.

transform.position = Vector3.MoveTowards(transform.position, target.position, 10.0f * Time.deltaTime);

The object is moved to point A and given a target B. It’s rotated to point B (thanks for your help on that) and is set active. Once it reaches Point B it is given a new target Point C and is rotated to Point C as in the previous step. Once it reached point C it is set inactive and ready to use again.

This is technically a 2D game but in a 3D space, some game objects are 3D and others sprites. The object I’m animating is a sprite and is moved in 3D space. All Vector3.z values are set to the same value thus restricting it to 2D. In my previous post I stated I need to rotate around the z axis while keeping the others fixed.

If I’m reading you right, I need to toss all my current code and use rotate and move forward simultaneously. I’ll have a look at Mathf.TurnTowardsAngle maybe I can see where to go from there.

But, unless I’ve misunderstood, what you’re doing now is considerably more complex than what I thought you said you wanted to do. I’m basing this on:

So, this is easy: you TurnTowardsAngle the correct direction, and move forward.

What you’re doing now, it sounds like, involves a little state machine where you keep track of whether you should be moving or turning, so that you can do only one at a time. So, yeah, throw all that stuff out. :slight_smile: Your new Update method will be only a handful of lines, and need hardly any state at all (just the position you’re trying to pursue).

Oh, there is one “gotcha” to watch out for here though. MoveTowards is clever enough to not overshoot the target. Just moving forward (with something like transform.position += transform.right * 10 * Time.deltaTime) is not. So, you’ll need to check when you’re close enough…

// Move forward, but don't overshoot the target.
float step = 10 * Time.deltaTime;
if (Vector3.Distance(transform.position, target.position) < step) {
    // Made it!
    transform.position = target.position;
} else {
    // Not there yet, but keep moving forward!
    transform.position += transform.right * step;
}

Nothing goes down without a battle…

So, I swapped out my code to move forward. Had to adjust speed factor, and use localPositions as it is nested, and now I’m in my weird rotation issue from my previous post. Swapping to world works but my target is a Vector3 and is a local point. I now need to convert a Vector3 local to world but can’t find how to. Everything I’ve found uses transform. My target is a random Vector3 generated in local scale. Since I’m now in world space, my target is now incorrect. How the hell do you convert a local point to world? I really should be doing all in local but the random rotational issues I’m getting and my inability to solve them are preventing my from going down that ‘rabbit hole’ again. Every solution creates a new problem.

Hang in there, Bill!

You can’t ask “How the hell do you convert a local point to world?” unless you know “local to what?” The thing that it’s local to is a transform. And you convert a point local to that, into world coordinates, using thatTransform.TransformPoint.

But let’s back up. Everything I’ve described should work with local coordinates, too, as long as you’re consistent about it. You’d need to use localPosition and localRotation, of course, and the target would need to also be in the local coordinate system. But assuming you’re diligent about all that, it ought to work.

If still feeling lost and rabbit-holey, please post a snapshot of your scene hierarchy and your current code. Many things are worth losing sleep over, but this isn’t one of them!

This is the same object issue from the previous post, actually it is the object and just a continuation of the complete problem. In the previous post I was using lookAt to rotate my object and you explained that it assumed an up orientation and that I should be using eulerAngle to make my rotation to avoid the unwanted rotation on other axis. ‘transform.position+= transform.right* step;’ is also assuming the wrong up orientation. I’m still fiddling, I’m sure I’ll be back.

Yeah, I was taking a wild stab with that transform.right based on the assumption that you had a typical sprite where “forward” means +X. But I figured (correctly, I think) that you’d be sharp enough to replace that with whatever direction really does mean forward in your case.

Yeah, I have it moving forward but it also rotates other axis so sprite is on edge.

This is a basic and elemental problem that I know is easily solvable. Someone who knows what they’re doing could make this work in a NY minute. I kind of thought I’d made some progress over the last few months, but clearly not. I’m supposed to be learning and my inability to solve this simple problem, on my own reflects that I haven’t learned much at all. At this point too much time and conversation has passed for me to want to waste another second on this. I’m dumping the whole concept and have to figure out something else to replace it.

Thanks for the help, but I feel I’m loosing ground and things I thought I knew are now in question. If I don’t change course to something I can move forward on, I stagnate till I drag Unity to the trash. Maybe I’ve just hit the wall in Unity and this is as far as I get without a brain transplant.

I think you’re right; it sounds like time to take a break. We all hit a mental block sometimes. Taking a walk or working on something else for a while sometimes helps.

If it matters, I can tell, just from the quality of your questions and answers you’ve given to others, that you really have learned quite a lot in the last few months. (But I know that can be hard to see when in the depths of frustration.)

Ok, found the problem. And of course it was obscure and not where I was looking for the problem. Took the entire weekend off out of frustration, and spent the time trying to come up with another NPC with an animation to replace my headache maker. It became obvious the what I had was the best for the game, and I was going to have to solve the issues or dump the entire project. My primary mission for this project was to learn networking and this project was supposed to the vehicle to do that. I’m determined to get networking functional and I have put so much time into the single player portion that abandonment wasn’t an option either. Sadly the project isn’t that big but took me all day to track down why I was getting random rotations. Now I can focus on the next headache, networking.

1 Like