Jump to content
  • Advertisement
Sign in to follow this  
Demion

Unity Click to move

This topic is 1411 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I need to make realistic human movement (3D) using mouse click.
 
1. Get mouse click point using RaycastAll.
2. Smoothly Slerp to LookRotation.
 
Everything works fine, except I have problem with circular movement (close radius).
 
1. 
">Example If dont move transform.forward while Slerp to LookRotation or MoveTowards instead of transform.forward it will be spinning like whirligig on circular movement.
2. 
">Example If do move transform.forward while while Slerp to LookRotation it will distort trajectory of straight direct movement.
 
Is there any solution to move circular realistic (not spin like whirligig) and move straight direct when needed (not distort trajectory)?
 
I need straight movement from first example and circular movement from second.
 
void Update()
{
    if (Input.GetMouseButtonDown(1))
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
        RaycastHit[] hits = Physics.RaycastAll(ray);
 
        foreach (RaycastHit hit in hits)
        {
            if (hit.transform.tag == "Ground")
            {
                DestinationPosition = new Vector3(hit.point.x, transform.position.y, hit.point.z);
 
                DestinationRotation = Quaternion.LookRotation(DestinationPosition - transform.position);
 
                break;
            }
        }
    }
 
    if (Vector3.Distance(transform.position, DestinationPosition) > 1)
    {
        transform.rotation = Quaternion.Slerp(transform.rotation, DestinationRotation, RotationSpeed * Time.deltaTime);
 
        transform.position += transform.forward * MovementSpeed * Time.deltaTime;
 
        //transform.position = Vector3.MoveTowards(transform.position, DestinationPosition, MovementSpeed * Time.deltaTime);
 
        animation.CrossFade("run");
    }
    else
    {
        animation.CrossFade("idle");
    }
}

Share this post


Link to post
Share on other sites
Advertisement

It looks like two issues.

 

First issue, I cannot really comment on your movement style, but I find it suspicious. I much prefer a general purpose MoveTo() style command that can figure out how the character should animate without conflicting with other animations, can avoid collisions, can search for paths through the map to find bridges and doorways, and otherwise work with the map. Of course, if that works for you, then this is not an issue.

 

Second issue, it looks like you need a 'dead zone' around the character.  Any move requests within that zone should be discarded. That way when the player touches very near the player they won't jump and spin in place. The player will need to touch some distance away from the character in order to trigger any movement.  The player could still cause spinning, but it would require a large intentional movement.

Share this post


Link to post
Share on other sites

I think you might want an animation that drives the turning, instead of slerping.  You can take a look at some of the Unity Mecanim animation tree examples to see how they handle it.

Edited by ferrous

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!