Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Garm · Aug 05, 2013 at 06:13 PM · vector3movement scriptmouse movement

Mouse click movement script issue.

Hello, I'm quite new to both coding and unity, yet I've decided to make an rpg game with some of my friends. I just started and I have an issue regarding character movement. I want to make my character move like in common isometric rpg games (like diablo, nvn etc).

So here's what I've done so far for this script:

 using UnityEngine;
 using System.Collections;
 
 public class Test : MonoBehaviour {
 public float _Distance;
 public float _Speed = 10f;
 
 
 
 
     // Use this for initialization
     void Start () {
 
 
 
 
     }
 
     void LateUpdate () {}
 
     // Update is called once per frame
     void Update () {
 
     Ray ray = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
     Debug.DrawRay(ray.origin, ray.direction * 9999999, Color.red);
 
     RaycastHit hit;
     Physics.Raycast(ray, out hit);
 
 
     if (Input.GetMouseButtonDown(0)){
 
                Vector3 _NewPosition = hit.point;
           if (hit.collider.tag == "passable"){    
 
              this.gameObject.transform.LookAt(hit.point);
              //  _Distance = Vector3.Distance(this.gameObject.transform.position, hit.point);
              this.gameObject.transform.position = Vector3.MoveTowards(this.gameObject.transform.position, _NewPosition, _Speed * Time.deltaTime);        
 
 
          } 
        }
 
     }
 } 

Thing is that it moves my character for 1 unit of _Speed, instead of moving it to the end point. I've tried to add "while _Distance <= n" cycle, but it just freezed the program. Could you please help me with this?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Garm · Aug 06, 2013 at 07:54 PM 0
Share

Thanks a lot, both of you. I just realized huge mistake I've made - I put "$$anonymous$$oveTowards" into "if" section, thus object moved only when key was pressed. Silly me, sometimes I should analyze own code more.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Adamcbrz · Aug 05, 2013 at 06:24 PM

I have gone through and adjusted your code. I have left comments to help explains some of the changes.

 using UnityEngine;
 using System.Collections;
  
 public class Test : MonoBehaviour
 {
     public float _Distance;
     public float _Speed = 10f;
     
     //Declare out side of update so we can retain it between frames
     private Vector3 _NewPosition;
  
     void Start ()
     {
         // set _NewPosition to the character position so we don't start moving torwards (0,0,0)
         _NewPosition = transform.position;
     }
  
     void Update ()
     {
  
         if (Input.GetMouseButtonDown (0)) {
             
             //Don't raycast every frame but only when you need it 
             Ray ray = Camera.mainCamera.ScreenPointToRay (Input.mousePosition);
             Debug.DrawRay (ray.origin, ray.direction * 9999999, Color.red);
  
             RaycastHit hit;
             // put in if statement just in case we don't hit anything
             if (Physics.Raycast (ray, out hit)) {
                 if (hit.collider.tag == "passable") {    
                     //move into check so we don't change position unless it meets your rules
                     _NewPosition = hit.point;
       
                 } 
             }
         }
         
         
         // you don't need the this.gameObject. this is assumed and gameObject is a shortcut just like transform 
         // so you can use the transform shortcut
         transform.LookAt (_NewPosition);
         transform.position = Vector3.MoveTowards (transform.position, _NewPosition, _Speed * Time.deltaTime);        
  
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by nixcs2512 · Aug 05, 2013 at 06:25 PM

You need 1 Vector3 variable to store the hit point by RayCast to let your character move to it.The code maybe looks like:

 public Vector3 destination;
 ...
 void Update(){
 ...
 if (Input.GetMouseButtonDown(0))
 {
  destination = hit.point;
 }
 this.gameObject.transform.LookAt(destination);
 this.gameObject.transform.position = Vector3.MoveTorwards(this.gameObject.transform.position, _NewPosition, _Speed * Time.deltaTime);        
 }

Your while loop don't have an "exit" so it will continue loop forever.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Smooth touch for map 0 Answers

2D Vector Movement,Vectorel Movement with Buttons 2 Answers

Roll a ball to fixed distance 3 Answers

Movement on prefab objects 0 Answers

Click to move script help 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges