Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 spigo98 · Jul 21, 2017 at 06:57 PM · movement scriptsmoothcodepageendless runnerrunner

endless runner movement problem

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerMotor : MonoBehaviour {
 
     private CharacterController controller;
     private Vector3 moveVector;
 
     public float smooth = 2;
     
 
     private float speed = 5.0f;
 
     private float verticalVelocity = 0.0f;
     private float gravity = 12.0f;
     private float jumpForce = 5.0f;
 
     // Use this for initialization
     void Start () {
         controller = GetComponent<CharacterController>();
         
     }
     
     // Update is called once per frame
     void Update () {
         moveVector = Vector3.zero;
 
         if (controller.isGrounded)
         {
             verticalVelocity = -0.5f;
             verticalVelocity = -gravity * Time.deltaTime;
             if (Input.GetKeyDown (KeyCode.Space)) {
                 verticalVelocity = jumpForce;
             }
         }
         else
         {
             verticalVelocity -= gravity* Time.deltaTime;
         }
         //X
         if (Input.GetKeyDown(KeyCode.A))
         {
             
             Vector3 newPosition = controller.transform.position;
             newPosition.x--;
             controller.transform.position = Vector3.Lerp(transform.position, newPosition, smooth);
         }
 
         if (Input.GetKeyDown(KeyCode.D))
         {
             
             Vector3 newPosition = controller.transform.position;
             newPosition.x++;
             controller.transform.position = Vector3.Lerp(transform.position, newPosition, smooth);
         }
         //Y
         moveVector.y = verticalVelocity;
         //Z
         moveVector.z = speed;
 
 
         controller.Move(moveVector * Time.deltaTime);
         
     }
 }


I found a problem, the character moves to shoot even though I use the lepr code. How can I fix it?

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 RustyCrow · Jul 21, 2017 at 07:26 PM 0
Share

So when you press a key i assume A or D you are shooting ? from what i can see i am guessing there is another script on character, that use the same keys (A or D) then is shoots. Your question is a little hard to understand is it movement problem(like sliding or teleport) or wrong behavior(shooting) ?

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by RustyCrow · Jul 21, 2017 at 09:08 PM

Ok somthing like this then ?

 new Vector3 NewPos;
     // Update is called once per frame
     void Update () 
     {
         if (Input.GetKeyDown(KeyCode.D))
         {
               NewPos = transform.position;
               NewPos.x += 5;    
         }
 
         transform.position = Vector3.Lerp(transform.position, NewPos, 2f * Time.deltaTime);
     }
 }

See what you are doing is trying to move withing 1 frame when you press A||D. This way its doing it over many frames so its smooth but this way has an issue of rounding. You see LERP will goes towards target but never gets there 0->5 (Look in the inspector and you will ses what i mean). Now you can Code in Snap function so when object gets close to target position you can force it so from 0.49 to 5. This is how i fix it. Hopefully this helped you @spigo98 a little.

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 spigo98 · Jul 21, 2017 at 07:33 PM

The problem is that when I press A or D doesn't a fluid motion

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 cQuence · Jan 28, 2018 at 08:14 PM

Hello, sorry for a bit late response, but it's now that I'm working on a endless runner game for a school project.

I'm also posting this for anyone else who is checking the forums that might be having issues with this case and could benefit from my input, since I've looked and found an answer to my problems countless times and I just want to return to community.

I've written this code for moving a character quite smoothish(probably could be even smoother) from point A to B and B to C and vice versa . This code also responds if a player wants to change direction of character when its already moving towards a certain lane. For example he wants to go from lane A to B but while character is moving to B he wants to go back to A. I hope this will help u in anyway.

 //This is all in Update Function
 // I have a 3 lane runner { -1, 0, 1 }, checking so it doesn't gets over that
     if (Input.GetKeyDown(moveL)  && wantedRow > -1)
             {
                 centeringZero = false;
                 wantedRow--;
             }
             if (Input.GetKeyDown(moveR)  && wantedRow < 1)
             {
                 centeringZero = false;
                 wantedRow++;
             }
             if (wantedRow == -1)
                 wantedX = -horiDiff;
             else if (wantedRow == 0)
                 wantedX = 0;
             else if (wantedRow == 1)
                 wantedX = horiDiff;
     //bool centeringZero is necessary to prevent update of doing a multiple movement changes at once, since update is a bit unpredictable and same thing can be set multiple times. So I find bools quite useful in Update functions.
             if (centeringZero)
             {
                 transform.Translate(new Vector3(0, 0, moveSpeed) * Time.deltaTime);
                 Debug.Log("0, 0, moveSpeed");
             }
             else if (transform.localPosition.x < wantedX && !centeringZero)
             {
                 transform.Translate(new Vector3(3, 0, moveSpeed) * Time.deltaTime);
                 Debug.Log("translate 3,0,movespeed");
                 if (wantedRow == 0)
                 {
     //if statement to check if its close to zero if it is its sets it to zero.
                     if (transform.localPosition.x >= -0.1f && transform.localPosition.x <= 0.1f && !centeringZero)
                     {
                         Vector3 fixX = transform.localPosition;
                         fixX.x = 0;
                         transform.localPosition = fixX;
                         currRow = 0;
                         centeringZero = true;
                         Debug.Log("centering 0");
                     }
                 }
                 if(wantedRow == 1)
                 {
     //if statement to check if its close to horiDiff in my case its set to 1.25f and set it to 1.25f
                     if (transform.localPosition.x < horiDiff && transform.localPosition.x > horiDiff - 0.1f && !centeringZero)
                     {
                         Vector3 fixX = transform.localPosition;
                         fixX.x = horiDiff;
                         transform.localPosition = fixX;
                         currRow = 1;
                         centeringZero = true;
                         Debug.Log("centering " + horiDiff);
                     }
                 }
                 
             }
             else if(transform.localPosition.x > wantedX && !centeringZero)
             {
                 transform.Translate(new Vector3(-3, 0, moveSpeed) * Time.deltaTime);
                 Debug.Log("translate -3,0,movespeed");
                 if (wantedRow == 0)
                 {
     //if statement to check if its close to 0 and sets it to 0
                     if (transform.localPosition.x >= -0.1f && transform.localPosition.x <= 0.1f && !centeringZero)
                     {
                         Vector3 fixX = transform.localPosition;
                         fixX.x = 0;
                         transform.localPosition = fixX;
                         currRow = 0;
                         centeringZero = true;
                         Debug.Log("centering " + 0);
                     }
                 }
                 if (wantedRow == -1)
                 {
     //if statement to check if its close to horiDiff in my case its set to 1.25f and set it to 1.25f
                     if (transform.localPosition.x > -horiDiff && transform.localPosition.x < -horiDiff + 0.1f && !centeringZero)
                     {
                         Vector3 fixX = transform.localPosition;
                         fixX.x = -horiDiff;
                         transform.localPosition = fixX;
                         currRow = -1;
                         centeringZero = true;
                         Debug.Log("centering " + -horiDiff);
                     }
                 }
             }

If you want smoother transitions from one lane to another, only thing that could be causing uneven transition is when function fixes character position to its lane. You can play around with if statements where it checks if its close to desired character X position and then set its position.

 //try changing this to even closer to horiDiff, perhaps 0.01f - could be better but not sure
 if (transform.localPosition.x < horiDiff && transform.localPosition.x > horiDiff - 0.1f && !centeringZero)
                      {
                          Vector3 fixX = transform.localPosition;
                          fixX.x = horiDiff;
                          transform.localPosition = fixX;
                          currRow = 1;
                          centeringZero = true;
                          Debug.Log("centering " + horiDiff);
                      }

I've tried to explain the code and logic best as I can, but if there is still anything unclear, please tell me where and I'll try to explain it as best as I can. Again this could be probably done smoother/better and definitely cleaner, but I'm kinda rushing to finish it as fast as I can, since project due is closing in fast, so this will work for me.

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

72 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

smooth follow 2d camera runner ios 0 Answers

how to use instantiate 0 Answers

High score line in a runner ? 0 Answers

How can I make smooth movement ? 0 Answers

Script to Move different parts of Gantry robot in X,Y,Z axis in sequence 2 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