Page 1 of 2 12 LastLast
Results 1 to 10 of 18
  1. #1
    Join Date
    Feb 2011
    Posts
    27

    Question Click To Move Character Script

    I am new to programming so do not think I am stupid but I am looking to be able to put my character at a scene, then attach it to a macro that will have mouse be visible and click a point and walk towards it...sort of like they have in rsclegacy. Can anyone help me with this?
    Last edited by BrumDogPro; 04-04-2017 at 09:02 PM.

  2. #2
    Join Date
    Jan 2011
    Posts
    102
    Drag the Cube onto your scene and create a plane for it to move on. You can Raycast and take the position(Vector3(x, y, z)) of where the mouse is clicked on the plane. Then you can Lerp (http://unity3d.com/support/documenta...tor3.Lerp.html). Just plug in cubes current position into start position, and mouse position into end position. Using Linear interpolation will make it move smoothly to that position as well, and not just jump right there. Start with moving first, then move onto rotation.

    I'm not sure what kinda new to programming means, I apologize if I threw too much at you or confused you. If you're new to Unity check out the free videos here first and you'll understand a lot more quickly.

  3. #3
    Join Date
    Apr 2007
    Location
    Seattle
    Posts
    214
    If you put this little script into the Update method and attach that to your main camera, when you press the left mouse button it will draw a line from the camera to the point in at which it contacts any GameObject named "Ground".

    hit.point = location you want to move to. That is the location you would want to Lerp to using jimmiduff21's example above.

    Code:
     if (Input.GetMouseButton(0))
                {
                    RaycastHit hit;
                    Ray ray = (Camera.main.ScreenPointToRay(Input.mousePosition));
                                    
                    if (Physics.Raycast(ray, out hit))
                    {
                        Debug.Log("Hit: " + hit.transform.name);
                        if (hit.transform.name == "Ground")
                        {
                            Debug.DrawLine(Camera.main.transform.position, hit.point);
                        }
                    }                   
                }

  4. #4
    Join Date
    Feb 2011
    Posts
    27
    Quote Originally Posted by jimmiduff21 View Post
    Drag the Cube onto your scene and create a plane for it to move on. You can Raycast and take the position(Vector3(x, y, z)) of where the mouse is clicked on the plane. Then you can Lerp (http://unity3d.com/support/documenta...tor3.Lerp.html). Just plug in cubes current position into start position, and mouse position into end position. Using Linear interpolation will make it move smoothly to that position as well, and not just jump right there. Start with moving first, then move onto rotation.

    I'm not sure what kinda new to programming means, I apologize if I threw too much at you or confused you. If you're new to Unity check out the free videos here first and you'll understand a lot more quickly.
    Thanks and I'm not really new to Unity. I just don't understand all of what I can do with programming yet. I can make sense of what you told me though. I'll try it out thanks

    Quote Originally Posted by Lerich View Post
    If you put this little script into the Update method and attach that to your main camera, when you press the left mouse button it will draw a line from the camera to the point in at which it contacts any GameObject named "Ground".

    hit.point = location you want to move to. That is the location you would want to Lerp to using jimmiduff21's example above.

    Code:
     if (Input.GetMouseButton(0))
                {
                    RaycastHit hit;
                    Ray ray = (Camera.main.ScreenPointToRay(Input.mousePosition));
                                    
                    if (Physics.Raycast(ray, out hit))
                    {
                        Debug.Log("Hit: " + hit.transform.name);
                        if (hit.transform.name == "Ground")
                        {
                            Debug.DrawLine(Camera.main.transform.position, hit.point);
                        }
                    }                   
                }
    Thanks but I could never get this to work. I kept getting errors saying insert semicolin on everyline where there already was one. I tried restarting Unity and typing it out by hand and still nothing.

    BTW can you remove the draw line? jw
    Last edited by fatgav; 03-01-2011 at 12:50 AM. Reason: DOUBLE POST

  5. #5
    Join Date
    Jan 2011
    Posts
    102
    The Debug.DrawLine is just for visual purposes so you can see it in the editor. Yes you could take it out.

  6. #6
    Join Date
    Feb 2011
    Posts
    27
    Do you know what my problem is?

  7. #7
    Join Date
    Dec 2007
    Posts
    70
    i did some tests and got this running, its still not robust enough to use inn production, but its a start. There is lots of hard-coded values to make it work a.t.m, but hope this will be helpful to some.



    here is the code for the MoveByMouseClass

    Code:
    using UnityEngine;
    
    public class TP_MoveByMouse
    {
        private static TP_MoveByMouse singelton = null;
    
        private Vector3 movePoint = new Vector3(0,0.9f,0);
        private float distance = 0;
    
        private TP_MoveByMouse()
        {
        }
    
        public static TP_MoveByMouse GetInstance()
        {
            if(singelton != null)
            {
                return singelton;
            }
            var x = new TP_MoveByMouse();
            return x;
        }
    
        public void MoveByMouseKlick()
        {
            var player = GameObject.Find("Capsule");
            var playerCurrentPos = player.transform.position;
    
            if (Input.GetMouseButton(0))
            {
                movePoint = RayCastLineTest(playerCurrentPos);
                GetDistance(player);
                playerCurrentPos = player.transform.position;
    
                if (!player)
                    return;
            }
    
            if(distance > 0.1f)
            {
                var speed = 10f;
                var moveSpeed = speed * Time.deltaTime;
                var moveStep = movePoint - player.transform.position;
                moveStep.Normalize();
                
                player.transform.LookAt(movePoint);
                player.transform.position = playerCurrentPos + (moveStep * moveSpeed);
                playerCurrentPos = player.transform.position;
    
                GetDistance(player);
            }
        }
    
        private void GetDistance(GameObject player)
        {
            distance = Vector3.Distance(player.transform.position, movePoint);
            Debug.Log(string.Format("Distance: {0}", distance));
        }
    
        public Vector3 RayCastLineTest(Vector3 playerLoc)
        {
            var hit = new RaycastHit();
            Ray ray = (Camera.main.ScreenPointToRay(Input.mousePosition)  );
    
            
                Physics.Raycast(ray, out hit);
    
            Debug.DrawLine(Camera.main.transform.position, hit.point);
            
            return hit.transform.tag == "CanMoveOn" ? new Vector3(hit.point.x,hit.point.y + 0.9f,hit.point.z) : playerLoc;
        }
    }
    to call the function you need to call/instantiate the class with
    Code:
    TP_MoveByMouse move = TP_MoveByMouse.GetInstance();
    and then you can use it like this
    Code:
     move.MoveByMouseKlick();

  8. #8
    Join Date
    Apr 2007
    Location
    Seattle
    Posts
    214
    Quote Originally Posted by zflowerz View Post
    here is the code for the MoveByMouseClass
    It looks like the way you did it works just fine but I think your're doing way more work then you need to here. Maybe it was just to get it to work inside the TPC system, but just to humor me try this.

    Start a brand new project. Don't import any packages.
    Create a plane and name it Ground. Position it at 0,0,0 and scale x & z = 5.
    Create a cube. Position it at 0,0,0.
    Create a new C# file inside Unity. Open it up in Visual Studio or whichever one you use.
    Copy this code into it and name the file Click2Move.
    Code:
    using UnityEngine;
    using System.Collections;
    
    public class Click2Move : MonoBehaviour 
    {
    
        public Transform mover; //the object being moved
        public float SnapTo = 0.5f; //how close we get before snapping to the desination
        private Vector3 destination = Vector3.zero; //where we want to move
    
    	// Use this for initialization
    	void Start ()
        {
            destination = mover.position; //set the destination to the objects position when the script is run the first time
    	}
    	
    	// Update is called once per frame
        void Update()
        {
            //when left mouse button is pressed
            if (Input.GetMouseButtonDown(0))
            {
                Ray ray = (Camera.main.ScreenPointToRay(Input.mousePosition)); //create the ray
                RaycastHit hit; //create the var that will hold the information of where the ray hit
                
                if (Physics.Raycast(ray, out hit)) //did we hit something?
                    if (hit.transform.name == "Ground") //did we hit the ground?
                        destination = hit.point; //set the destinatin to the vector3 where the ground was contacted
            }
    
            // move the object toward the destination
            if (Vector3.Distance(mover.position, destination) < SnapTo) //are we within snap range?
                mover.position = destination; //snap to destination
            else 
                mover.position = Vector3.MoveTowards(mover.transform.position, destination, Time.deltaTime * 10); //move toward destination
    	}
    }
    Attach the script to the camera in the scene.
    Attach the cube to the Mover property on the Click2Move script.
    Hit play.

    What should happen is the cube should move to the mouse click and stay there until you click again.
    You may have to move the camera around so you can see it from above and add a light to the scene.

    I have tried this 3 times with 3 different new projects and it has worked every time. So I am curious to know if anybody has a problem with it not working.

    @BrumDogPro: I couldn't tell you what the semi colon error is coming from I have not seen an error like that before, Sorry.

  9. #9
    Join Date
    Feb 2011
    Posts
    27
    I tried out your code and worked just fine for me now.

    It seems to be that all javascript files no longer work for me. Only C#. Just saying.

    Quote Originally Posted by Lerich View Post
    It looks like the way you did it works just fine but I think your're doing way more work then you need to here. Maybe it was just to get it to work inside the TPC system, but just to humor me try this.

    Start a brand new project. Don't import any packages.
    Create a plane and name it Ground. Position it at 0,0,0 and scale x & z = 5.
    Create a cube. Position it at 0,0,0.
    Create a new C# file inside Unity. Open it up in Visual Studio or whichever one you use.
    Copy this code into it and name the file Click2Move.
    Code:
    using UnityEngine;
    using System.Collections;
    
    public class Click2Move : MonoBehaviour 
    {
    
        public Transform mover; //the object being moved
        public float SnapTo = 0.5f; //how close we get before snapping to the desination
        private Vector3 destination = Vector3.zero; //where we want to move
    
        // Use this for initialization
        void Start ()
        {
            destination = mover.position; //set the destination to the objects position when the script is run the first time
        }
        
        // Update is called once per frame
        void Update()
        {
            //when left mouse button is pressed
            if (Input.GetMouseButtonDown(0))
            {
                Ray ray = (Camera.main.ScreenPointToRay(Input.mousePosition)); //create the ray
                RaycastHit hit; //create the var that will hold the information of where the ray hit
                
                if (Physics.Raycast(ray, out hit)) //did we hit something?
                    if (hit.transform.name == "Ground") //did we hit the ground?
                        destination = hit.point; //set the destinatin to the vector3 where the ground was contacted
            }
    
            // move the object toward the destination
            if (Vector3.Distance(mover.position, destination) < SnapTo) //are we within snap range?
                mover.position = destination; //snap to destination
            else 
                mover.position = Vector3.MoveTowards(mover.transform.position, destination, Time.deltaTime * 10); //move toward destination
        }
    }
    Attach the script to the camera in the scene.
    Attach the cube to the Mover property on the Click2Move script.
    Hit play.

    What should happen is the cube should move to the mouse click and stay there until you click again.
    You may have to move the camera around so you can see it from above and add a light to the scene.

    I have tried this 3 times with 3 different new projects and it has worked every time. So I am curious to know if anybody has a problem with it not working.

    @BrumDogPro: I couldn't tell you what the semi colon error is coming from I have not seen an error like that before, Sorry.
    I tried your script out. It's pretty good. The only problem was the cube ran through the terrain if it was raised haha
    Last edited by fatgav; 03-03-2011 at 03:29 PM. Reason: TRIPLE! posting ffs...

  10. #10
    Join Date
    Aug 2010
    Location
    Aegina
    Posts
    36
    Quote Originally Posted by BrumDogPro View Post
    I tried your script out. It's pretty good. The only problem was the cube ran through the terrain if it was raised haha
    if u really want that to work on a terrain you should do it with a CharacterCollider and gravity applied and some changes on the script,
    because the click to move system in most games use AI to avoid things.. if u really want to get it in that level

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •