• 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 yerahmat · Jan 06, 2020 at 07:31 AM · movementrigidbodytransformpositionobject

Object not moving,Object not moving in any direction

Object not moving

I am trying to move a object in a direction, but it not seems to work. I have tried reinstalling visual studio community, trying different methods to move object but they didn't help.

Heres the code

 using UnityEngine;
 
 public class BallMovement : MonoBehaviour
 {
     public Rigidbody rb;
     public Rigidbody camRb;
     public Camera mainCamera;
     public Vector3 lastPos;
     public float wallDistance = 0.01f;
     public float minCamDistance;
     public float lerpSpeed;
     public float ballSpeed;
     public float camSpeed;
     public float bonusBallSpeed;
     public float maxBallDistance;
     public float minBallDistance;
     public float currentBallDistance;
 
     public Vector2 firstPressPos;
     public Vector2 secondPressPos;
     public Vector2 currentSwipe;
 
     public Vector3 pressedPos;
     public Vector3 leftPos;
 
     public bool pressed=false;
 
     private void Start()
     {
         rb = GetComponent<Rigidbody>();
     }
 
     private void Update()
     {
 
         //  Debug.Log("Controls from editor");
         // Swipe();
 
 #if UNITY_EDITOR
         // Debug.Log("Controls from editor");
         if (Input.GetMouseButton(0))
         {
             Debug.Log("Mouse pressed");
             if (!pressed)
             {
                 Debug.Log("Mouse pressed, left");
                 rb.AddForceAtPosition(Vector3.left, new Vector3(-1.5f, 0, 0),ForceMode.VelocityChange);
                 pressed = true;
             }
             else
             {
                 Debug.Log("Mouse pressed, right");
                 rb.AddForceAtPosition(Vector3.left, new Vector3(1.5f, 0, 0), ForceMode.VelocityChange);
                 pressed = false;
             }
         }
 #elif UNITY_ANDROID
          Debug.Log("From android device or emulator");
         if (Input.touchCount > 0)
         {
             Touch touch = Input.GetTouch(0);
             Vector3 touchPos = Camera.main.ScreenToWorldPoint(touch.position);
 
             if (touchPos.x < 0)
             {
                 //left
          float pos = transform.position.x;
                 pos = -1.5f;
 
             }else if (touchPos.x > 0)
             {
                 //right
          float pos = transform.position.x;
                 pos = 1.5f;
             }
         }
 #endif
 
         ballCameraDistance();
     }
 
     void HorizontalMovemnent()
     {
         
         Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
 
         RaycastHit hit;
         if (Physics.Raycast(ray, out hit))
         {
             //  Debug.Log("Ray cast success");
             transform.position = Vector3.Lerp(transform.position, new Vector3(hit.point.x, transform.position.y, transform.position.z), lerpSpeed * Time.deltaTime);
         }
     }
 
     void forwardMovemnent()
     {
         
         if (currentBallDistance < maxBallDistance && currentBallDistance > minBallDistance)
         {
             //  Debug.Log("Ball is between max and min distance");
             rb.velocity = Vector3.forward * ballSpeed;
 
         }
         else
         {
             //  Debug.Log("Ball is outside max and min distance");
             rb.velocity = Vector3.forward * camSpeed;
         }
     }
 
     private void LateUpdate()
     {
         Vector3 pos = transform.position;
 
         if (transform.position.x < -wallDistance)
         {
             pos.x = -wallDistance;
         }
         else if (transform.position.x > wallDistance)
         {
             pos.x = wallDistance;
         }
 
         if (transform.position.z < mainCamera.transform.position.z + minCamDistance)
         {
             pos.z = mainCamera.transform.position.z + minCamDistance;
         }
 
         transform.position = pos;
 
     }
 
     private void FixedUpdate()
     {
         forwardMovemnent();
         cameraMovement();
     }
 
     private void cameraMovement()
     {
         camRb.velocity = Vector3.forward * camSpeed;
     }
 
     private void ballCameraDistance()
     {
         // currentBallDistance = Vector3.Distance(mainCamera.transform.position,transform.position);
         currentBallDistance = transform.position.z;
     }
 
 
     public void Swipe()
     {
         if (Input.GetMouseButtonDown(0))
         {
             //save began touch 2d point
             firstPressPos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
             float posX = 1.5f;
 
             Vector3.Lerp(transform.position, new Vector3(-1.5f,0,0), lerpSpeed * Time.deltaTime);
         }
         if (Input.GetMouseButtonUp(0))
         {
             //save ended touch 2d point
             secondPressPos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
 
             //create vector from the two points
             currentSwipe = new Vector2(secondPressPos.x - firstPressPos.x, secondPressPos.y - firstPressPos.y);
 
             //normalize the 2d vector
             currentSwipe.Normalize();
 
             //swipe upwards
             if (currentSwipe.y > 0  && currentSwipe.x > -0.5f  && currentSwipe.x < 0.5f)
          {
                 Debug.Log("up swipe");
             }
             //swipe down
             if (currentSwipe.y < 0  && currentSwipe.x > -0.5f  && currentSwipe.x < 0.5f)
         {
                 Debug.Log("down swipe");
             }
             //swipe left
             if (currentSwipe.x < 0 && currentSwipe.y > -0.5f && currentSwipe.y < 0.5f)
         {
                 Debug.Log("left swipe");
 
                 rb.AddForce(transform.up*100*Time.deltaTime);
 
 
             }
             //swipe right
             if (currentSwipe.x > 0  && currentSwipe.y > -0.5f  && currentSwipe.y < 0.5f)
         {
                 Debug.Log("right swipe");
 
                 rb.AddForce(transform.right * 100*Time.deltaTime); 
 
             }
         }
     }
 }

alt text

I am trying to move a object in a direction, but it not seems to work. I have tried reinstalling visual studio community, trying different methods to move object but they didn't help.

Heres the code

 using UnityEngine;
 
 public class BallMovement : MonoBehaviour
 {
     public Rigidbody rb;
     public Rigidbody camRb;
     public Camera mainCamera;
     public Vector3 lastPos;
     public float wallDistance = 0.01f;
     public float minCamDistance;
     public float lerpSpeed;
     public float ballSpeed;
     public float camSpeed;
     public float bonusBallSpeed;
     public float maxBallDistance;
     public float minBallDistance;
     public float currentBallDistance;
 
     public Vector2 firstPressPos;
     public Vector2 secondPressPos;
     public Vector2 currentSwipe;
 
     public Vector3 pressedPos;
     public Vector3 leftPos;
 
     public bool pressed=false;
 
     private void Start()
     {
         rb = GetComponent<Rigidbody>();
     }
 
     private void Update()
     {
 
         //  Debug.Log("Controls from editor");
         // Swipe();
 
 #if UNITY_EDITOR
         // Debug.Log("Controls from editor");
         if (Input.GetMouseButton(0))
         {
             Debug.Log("Mouse pressed");
             if (!pressed)
             {
                 Debug.Log("Mouse pressed, left");
                 rb.AddForceAtPosition(Vector3.left, new Vector3(-1.5f, 0, 0),ForceMode.VelocityChange);
                 pressed = true;
             }
             else
             {
                 Debug.Log("Mouse pressed, right");
                 rb.AddForceAtPosition(Vector3.left, new Vector3(1.5f, 0, 0), ForceMode.VelocityChange);
                 pressed = false;
             }
         }
 #elif UNITY_ANDROID
          Debug.Log("From android device or emulator");
         if (Input.touchCount > 0)
         {
             Touch touch = Input.GetTouch(0);
             Vector3 touchPos = Camera.main.ScreenToWorldPoint(touch.position);
 
             if (touchPos.x < 0)
             {
                 //left
          float pos = transform.position.x;
                 pos = -1.5f;
 
             }else if (touchPos.x > 0)
             {
                 //right
          float pos = transform.position.x;
                 pos = 1.5f;
             }
         }
 #endif
 
         ballCameraDistance();
     }
 
     void HorizontalMovemnent()
     {
         
         Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
 
         RaycastHit hit;
         if (Physics.Raycast(ray, out hit))
         {
             //  Debug.Log("Ray cast success");
             transform.position = Vector3.Lerp(transform.position, new Vector3(hit.point.x, transform.position.y, transform.position.z), lerpSpeed * Time.deltaTime);
         }
     }
 
     void forwardMovemnent()
     {
         
         if (currentBallDistance < maxBallDistance && currentBallDistance > minBallDistance)
         {
             //  Debug.Log("Ball is between max and min distance");
             rb.velocity = Vector3.forward * ballSpeed;
 
         }
         else
         {
             //  Debug.Log("Ball is outside max and min distance");
             rb.velocity = Vector3.forward * camSpeed;
         }
     }
 
     private void LateUpdate()
     {
         Vector3 pos = transform.position;
 
         if (transform.position.x < -wallDistance)
         {
             pos.x = -wallDistance;
         }
         else if (transform.position.x > wallDistance)
         {
             pos.x = wallDistance;
         }
 
         if (transform.position.z < mainCamera.transform.position.z + minCamDistance)
         {
             pos.z = mainCamera.transform.position.z + minCamDistance;
         }
 
         transform.position = pos;
 
     }
 
     private void FixedUpdate()
     {
         forwardMovemnent();
         cameraMovement();
     }
 
     private void cameraMovement()
     {
         camRb.velocity = Vector3.forward * camSpeed;
     }
 
     private void ballCameraDistance()
     {
         // currentBallDistance = Vector3.Distance(mainCamera.transform.position,transform.position);
         currentBallDistance = transform.position.z;
     }
 
 
     public void Swipe()
     {
         if (Input.GetMouseButtonDown(0))
         {
             //save began touch 2d point
             firstPressPos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
             float posX = 1.5f;
 
             Vector3.Lerp(transform.position, new Vector3(-1.5f,0,0), lerpSpeed * Time.deltaTime);
         }
         if (Input.GetMouseButtonUp(0))
         {
             //save ended touch 2d point
             secondPressPos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
 
             //create vector from the two points
             currentSwipe = new Vector2(secondPressPos.x - firstPressPos.x, secondPressPos.y - firstPressPos.y);
 
             //normalize the 2d vector
             currentSwipe.Normalize();
 
             //swipe upwards
             if (currentSwipe.y > 0  && currentSwipe.x > -0.5f  && currentSwipe.x < 0.5f)
          {
                 Debug.Log("up swipe");
             }
             //swipe down
             if (currentSwipe.y < 0  && currentSwipe.x > -0.5f  && currentSwipe.x < 0.5f)
         {
                 Debug.Log("down swipe");
             }
             //swipe left
             if (currentSwipe.x < 0 && currentSwipe.y > -0.5f && currentSwipe.y < 0.5f)
         {
                 Debug.Log("left swipe");
 
                 rb.AddForce(transform.up*100*Time.deltaTime);
 
 
             }
             //swipe right
             if (currentSwipe.x > 0  && currentSwipe.y > -0.5f  && currentSwipe.y < 0.5f)
         {
                 Debug.Log("right swipe");
 
                 rb.AddForce(transform.right * 100*Time.deltaTime); 
 
             }
         }
     }
 }

alt text

untitled.png (21.3 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by lgarczyn · Jan 06, 2020 at 08:20 PM

Too much code to fully debug, but if you have a rigidbody, don't use transform.position.


Also if your rigidbody is kinematic, use rigidbody.MovePosition.


If it is not kinematic, use rigidbody.velocity or rigidbody.AddForce.


Only use transform.position if you don't have a rigidbody or CharacterController.


Only use rigidbody.position if you want to teleport your rigidbody instead of moving it slowly.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

233 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 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 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 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

Making something move relative to something else, but slower/shorter ? 2 Answers

How to make object follow touch position?Pls Help 0 Answers

How can I move an object physically correct around an other object? 1 Answer

Get direction of moving sphere? 1 Answer

How to Align the pieces with different scale? 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