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 /
  • Help Room /
avatar image
0
Question by galaboy · Aug 21, 2015 at 06:45 AM · movementscriptingbasicsbasic programming

need help on character movement

hi, doing a subwaysurfer like game, but with car. i got stuck with the character's movement.my character is moving teleporting from one point to another, but i want a continues slow movement, where the player can see the character moving. need help on where iam making mistake.please explain.

 using UnityEngine;
 using System.Collections;
 
 public class inputManager : MonoBehaviour {
 
     public float playerSpeed;
     public float playerStrifingValue;
     public GameObject player;
         
     public float playerTimeTakenToLerp;
 
     private Vector3 firstSwipe;
     private Vector3 secondSwipe;
     private Vector3 currentSwipe;
     private float movementDistance;
     
     //private float carMovement;
     
     //private float leftEndPosition = -3.17f;
     //private float rightEndPosition = 3.17f;
     //private float accleration;
     
     //float otherObjectYlocalScale;
     
     
     // Use this for initialization
     void Start () 
     { 
 
     }
     
     // Update is called once per frame
     void Update () 
     {
         player.transform.Translate((Vector3.forward * playerSpeed) * Time.deltaTime);
 
         if (Input.GetMouseButtonDown (0)) 
         {
             firstSwipe = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
         }
             
 
         if (Input.GetMouseButtonUp (0)) 
         {
             secondSwipe = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z);
             
             currentSwipe = new Vector3(secondSwipe.x - firstSwipe.x,
                                        secondSwipe.y - firstSwipe.y,
                                        secondSwipe.z - firstSwipe.z);
 
             currentSwipe.Normalize();
             //print(firstSwipe);
             //print(currentSwipe);
 
         }
         PlayerMovement ();
     }
     
     void PlayerMovement()
     {
         if (Input.GetMouseButtonUp (0)) 
         {
             if (currentSwipe.x < 0 & currentSwipe.y < 0.5f & currentSwipe.y > -0.5f) 
             {
                 player.transform.position = Vector3.Lerp(player.transform.position, 
                                                          new Vector3((currentSwipe.x * playerStrifingValue) + player.transform.position.x,
                             player.transform.position.y,
                             player.transform.position.z), 
                                                          playerTimeTakenToLerp * Time.deltaTime);
                 
             }
             
             if (currentSwipe.x > 0 & currentSwipe.y < 0.5f & currentSwipe.y > -0.5f) 
             {
                 player.transform.position = Vector3.Lerp(player.transform.position, 
                                                          new Vector3((currentSwipe.x * playerStrifingValue) + player.transform.position.x,
                             player.transform.position.y,
                             player.transform.position.z), 
                                                          playerTimeTakenToLerp * Time.deltaTime);
             }
             
             if (player.transform.position.x < -3f)
                 player.transform.position = new Vector3(-3f, player.transform.position.y,player.transform.position.z);
             
             if(player.transform.position.x > 3f)
                 player.transform.position = new Vector3(3f, player.transform.position.y,player.transform.position.z);
         }
 }
 }

   




Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Wolfshadow · Aug 21, 2015 at 12:16 PM

try using addForce. As seen in unity scripting reference:

  using UnityEngine; using System.Collections;
 
  public class ExampleClass : MonoBehaviour {
      public float thrust;
      public Rigidbody rb;
      void Start() {
          rb = GetComponent<Rigidbody>();
      }
      void FixedUpdate() {
          rb.AddForce(transform.forward * thrust);
      }
  }

AddForce is very smooth. Think of pushing an object. I use it for my movement and jumping

Comment
Add comment · Show 1 · 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 galaboy · Aug 22, 2015 at 12:57 PM 0
Share

thanks for the reply, but i dont want to use physics.

avatar image
0

Answer by moriggi · Aug 22, 2015 at 07:52 PM

AddForce is ok but multiply x Time.DeltaTime x speed that must be a public float speed =10f; and set well in the inspector by

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

27 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

Related Questions

How do I do movement clamps but with localPositions of an object? 0 Answers

Reversing movement directions 1 Answer

How do I make my movement script move the character UNTIL I let go of the key? 1 Answer

How can I implement an object placement system ? 0 Answers

First person controller 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