- Home /
Moving to points
Hi, I've got a simple problem. I'm struggling to create an RTS game. In it, one must be able to click a unit and move it to a set position. After working all day, I've gotten pretty far, and I'm almost done. What I've done is on mousedown, create an invisible sphere at hit.point (using raycast.hit). The unit targets the sphere, and walks towards it. However, since the sphere is on the ground, the unit will rotate towards it until it is directly over it, at which point, it will be directly over it, and to continue facing it, will rotate until it is horizontal to the ground. This constituted a major problem in my development, as it was completely unrealistic. I sought to counter it by placing a Configurable Joint on my unit. However, the unit simply could not get to the target, because it wanted to rotate, but couldn't because of the C. Joint. It would therefore stop at some distance from the target. How would I fix this?
Thanks in advance - Elliot Bonneville.
P.S. Thanks go to all the people who have already answered my previous (numerous) questions.
Answer by KvanteTore · Mar 29, 2010 at 06:41 AM
Could you, instead of looking directly at the sphere, do a lookat towards the x and z position of the sphere, and the y position of your object?
Edit: You could possibly use the following C# script to look straight ahead, but towards target:
Vector3 targetPos = target.transform.position;
targetPos.y = transform.position.y;
transform.lookAt(objectPos);
Well, I'm using the lookat function in the API, and I don't know how to modify that directly.
You wouldn't. You would do it just like in $$anonymous$$vanteTore's example.
I see. In fact, I think I commented before the example. Thanks for pointing it out, I didn't see it. (I don't know how I didn't) :)
I tried to convert it JS, but it gave me about 15 errors. I'm not familiar with C#, although I know C++, it's not the same at all.
@ $$anonymous$$vanteTore Thanks a ton! I finally got it working. Oh, victory is sweet! lol.
Answer by Sebas · Mar 29, 2010 at 02:20 AM
Putting the configurable joint just makes things more complicated. Why don't you disable the lookAt as soon as you enter the sphere and solve the rest by just approaching a location which is at a point which you calculate like [sphere center on ground] + [y-value of pivot point of the moving unit]. You just keep moving the last short distance towards that point above ground and no turning downwards will occur.
The problem is, it's turning from quite a while away, although just a little bit, it gets more and more noticable until I hit the trigger, at which point the unit stands straight up. As you can see, that's not very desirable at the moment.
the point to move towards (above your sphere) could be used as a lookat point from the very beginning.
Answer by Ricardo · Mar 29, 2010 at 07:44 AM
You could look into UnitySteer and the simple MovesToPoint vehicle as an example. Sounds like part of the problem is that the target you're attempting to look at is at ground level, when you should instead be looking at a point that is, so to speak, at "eye-level" for the unit. It's likely all you need is simply adjusting the Y of the target.
Update: Just noticed that's what Kvante said, and upvoted. Leaving the post up as the steering reference is still relevant.
Your answer
Follow this Question
Related Questions
Having trouble steering gameobject. 0 Answers
object following a path, like train - railroad 4 Answers
How to combine NavMesh Agent with Character Controller 0 Answers
Pathfinding help 1 Answer
A* pathfinder - next target 1 Answer