- Home /
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);
}
}
}
}
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);
}
}
}
}
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.