- Home /
Object position is not changing on x-axis with mouse movement
I have an unity 3d game. Within that I have a ball game object on a wooden plank.I have also attached a rigid body to my ball and I am using ScreenToWorldPoint to covert coordinated to world.I am using prospective camera.
My requirement is I want to move my ball on x-axis along with my mouse click by keeping ball y and z coordinates as is. .After clicking mouse ball is moving little bit and after that it is not following my mouse
using UnityEngine;
public class GameManager : MonoBehaviour
{
public Camera mainCamera;
public GameObject ball;
Plane plane = new Plane(Vector3.forward,0);
public Transform target;
public float ballForce;
void Update()
{
Vector3 dir = target.position - ball.transform.position;
Vector3 mousePos = mainCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y,20.8f));
if (Input.GetMouseButtonDown(0)) {
ball.transform.position = new Vector3(mousePos.x, ball.transform.position.y, ball.transform.position.z);
ball.GetComponent<Animator>().enabled = false;
}
if (Input.GetMouseButtonUp(0))
{
//shoot ball
ball.GetComponent<Rigidbody>().AddForce(dir * ballForce, ForceMode.Impulse);
}
float dist;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (plane.Raycast(ray, out dist))
{
Vector3 point = ray.GetPoint(dist);
target.position = new Vector3(point.x, point.y, 0);
}
}
}
I could not find any errors on console but I can see ball.transform.position is changing only one time with initial click and after that it is not updating the ball position. Kindly help me as I am a newbie to Unity.,
Answer by Koyemsi · Jun 01 at 05:47 PM
Hi, I didn't try your script but I can tell that as you're using physics (Rigidbody with AddForce), you shouldn't try to move your ball's Transform directly, as this would create possible conflicts.
You could possibly act on the rigid body's position, but I think it can also lead to conflictual behaviours with the forces you apply.
One more important thing : you should absolutely avoid to use GetComponent inside an Update loop, as it is very memory expensive. Just store the required components in variables, like this :
Rigidbody ballRb;
void Awake() {
ballRb = ball.GetComponent<Rigidbody>();
}
void FixedUpdate() {
//shoot ball
ballRb.AddForce(dir * ballForce, ForceMode.Impulse);
}
Also, you should apply forces inside a FixedUpdate() loop rather than Update().
Good luck !
Answer by Caeser_21 · Jun 01 at 06:14 PM
This is the because it is only registering one click of the mouse button... You can either use`Input.GetMouseButton(0)` or try using some bools like :
public class GameManager : MonoBehaviour
{
public Camera mainCamera;
public GameObject ball;
Plane plane = new Plane(Vector3.forward,0);
public Transform target;
public float ballForce;
private bool IsPressed;
void Update()
{
if(IsPressed)
{
ball.transform.position = new Vector3(mousePos.x, ball.transform.position.y, ball.transform.position.z);
ball.GetComponent<Animator>().enabled = false;
}
Vector3 dir = target.position - ball.transform.position;
Vector3 mousePos = mainCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y,20.8f));
if (Input.GetMouseButtonDown(0))
{
IsPressed = true;
}
if (Input.GetMouseButtonUp(0))
{
IsPressed = false;
//shoot ball
ball.GetComponent<Rigidbody>().AddForce(dir * ballForce, ForceMode.Impulse);
}
float dist;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (plane.Raycast(ray, out dist))
{
Vector3 point = ray.GetPoint(dist); target.position = new Vector3(point.x, point.y, 0);
}
}
}
Your answer
Follow this Question
Related Questions
Multiple coroutines starting from Update() freeze/crash Unity 0 Answers
Player dies when it Hits/crushes a block 2 Answers
variable from another script doesn't update 0 Answers
I want to move the camera by swiping horizontaly (Mobile Input). 1 Answer
How do I change the camera automatically after a set timer? 1 Answer