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 Darkkingdom · Jan 20, 2015 at 03:04 PM · c#rotationmovementmove

Move forward to the endposition

Hey guys,

I wrote a script which should move the player to a target point. My player consists of a invisible parent with all scripts and colliders attached and a child which has only the model and the rotation script attached. The game uses the 2D physics but the model is 3D.

Let's start with the "RotationScript" in the child:

 private float horizontal;
     private float vertical;
     
     public bool rotateOn;
     public float angle;
     // Use this for initialization
     void Start () {
         rotateOn = true;
     }
 
     // Update is called once per frame
     void LateUpdate () { 
         
         if (rotateOn){
 
                 if (Input.GetButton ("Vertical") || Input.GetButton ("Horizontal")) {
                 horizontal = Input.GetAxisRaw ("Horizontal");
                 vertical = Input.GetAxisRaw ("Vertical");
 
                 //this is for invert horizontal
                     if(horizontal > 0){
                         horizontal = -horizontal;
                     } else if(horizontal < 0){
                         horizontal = horizontal - horizontal - horizontal;
                     }
 
                 }            
             }                
 
             if (horizontal != 0 || vertical != 0) {
                 angle = Mathf.Atan2 (horizontal, vertical) * Mathf.Rad2Deg;
                 transform.rotation = Quaternion.Lerp (transform.rotation, Quaternion.Euler (new Vector3 (0, 0, angle)), Time.deltaTime * 10);
             }
         }


I take the Input from the axes and lerp it so my model look in the right direction. This works pretty well, the only thing is that I have to invert my horizontal.

And here is my "MoveScript" in the parent:

     Vector3 moveVec;
     Vector3 dirVec;
 
 
     void Update () {        
         dirVec = transform.FindChild("Modell").localRotation * new Vector3(0,1,1);
         dirVec.z = 0;    
     }
 
 
 public void MovePlayer(Vector3 endPos, float speed) {
         rotate.rotateOn = false;
         moveVec = new Vector2 (dirVec.x * speed, dirVec.y * speed);
         rigidbody2D.velocity = moveVec;
          
         
         if (Vector3.Distance(endPos, transform.position) <= 0.1f) {
             rigidbody2D.velocity = Vector2.zero;
             targetPos = true;
             
         } else if (Vector3.Distance(endPos, transform.position) > 0.1f){
             targetPos = false;
         }
                 
     }


The dirVec is the converted vector from the rotation of the model. And moveVec is the same with speed so I can translate it to velocity. Oh and the MovePlayer function is called by FixedUpdate. The whole script work right as well, but without the rotation part.

Ok so my current problem is: The script works, but when the player is in rotation and something starts the movePlayer function my vec's aren't what the should and the player past the endpoint and moves to far. After hours of testing I give up and hope somebody can help me :)

Thank you so much for your time^^!

Comment
Add comment · Show 3
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 Glurth · Jan 20, 2015 at 03:15 PM 0
Share

You are using the LERPed direction (transform.FindChild("$$anonymous$$odell").localRotation), rather than the "final" direction ( Quaternion.Euler (new Vector3 (0, 0, angle))).

If the LERPed direction does not change fast enough, you could certainly miss and overshoot your destination mark.

To test if this is the issue: replace the LERP with a direct assignment (it wont look good, but it's just for testing).

avatar image Darkkingdom · Jan 20, 2015 at 06:38 PM 0
Share

Hey Glurth,

ok I replaced lerp the line with:

 transform.rotation =  Quaternion.Euler (new Vector3 (0, 0, angle));

Now the problem actually happens rarer, but it still happens. So I made a Debug.Log with all important informations:

 dirVec: (1.0, 0.0, 0.0)
 moveVec: (7.0, 0.0, 0.0)
 Rotation: (0.0, 0.0, 1.0, 0.0)
 CurPos: (-10.8, -8.8, 0.4)
 EndPos: (-8.8, -8.8, 0.4)
 ----
 dirVec: (0.0, -1.0, 0.0)
 moveVec: (0.0, -7.0, 0.0)
 Rotation: (0.0, 0.0, 1.0, 0.0)
 CurPos: (-10.7, -8.8, 0.4)
 EndPos: (-8.8, -8.8, 0.4)
 ----
 dirVec: (0.0, -1.0, 0.0)
 moveVec: (0.0, -7.0, 0.0)
 Rotation: (0.0, 0.0, 1.0, 0.0)
 CurPos: (-10.7, -8.8, 0.4)
 EndPos: (-8.8, -8.8, 0.4)
 ----
 


Every further run's/frames look like the second and thrid. Something weird happens after the first run of the script.

Oh and to be sure that my endPos is not the problem here's the code(but I don't think there's something wrong):

 endPos = transform.position + dirVec * range;

range is a float like 5.0f.

Hope you can help me :)

And thanks!^^

avatar image Glurth · Jan 20, 2015 at 09:45 PM 0
Share

Hmm, I'm not sure why the position does not change after the first pass. When is $$anonymous$$ovePlayer() being called? EDIT: ah, I see now where you said - FixedUpdate.
Also, not sure why your velocity vector direction changes, on the second pass even though the "rotation" you are displaying dosn't.

The next question I would have is, how do you detect when the player is at the endpoint? It appears you are not using colliders. So, you will need to check yourself, if the player has moved PAST the endpoint, since the last call to update.(a common, ugly issue: most unity programmers use colliders for this)

 if (Vector3.Distance(endPos, transform.position) <= 0.1f)

e.g. this line is VERY dependent upon the velocity of the object and the timescale between calls.

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Flip over an object (smooth transition) 3 Answers

Making a bubble level (not a game but work tool) 1 Answer

Objects not always Spawning At Correct Location 2 Answers

How to Move an Object down until a certain point then activate a function 1 Answer

Problems after rotating character. 0 Answers


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