Question by
Elegant_Princess · Aug 26, 2021 at 04:05 PM ·
positionmouseposition
How to make object move only when at a certain distance from the mouse,Block Breaker - 2 paddles
So I am making Block Breaker where I have a ball and paddle and I must destroy all the blocks. For one level, I want to make two paddles, however, I only want each to work if the mouse is a certain distance away from the specific paddle. I am having a hard time figuring out how to say this as I am new to coding.
public class Paddle : MonoBehaviour
{
[SerializeField] float screenWidthInUnits = 16f;
[SerializeField] float minX = 1f;
[SerializeField] float maxX = 13.5f;
[SerializeField] int numberOfPaddles;
public float distancePaddleCanWork = 4f;
//cached ref
GameStatus theGameStatus;
Rocket theRocket;
// Start is called before the first frame update
void Start()
{
theGameStatus = FindObjectOfType<GameStatus>();
theRocket = FindObjectOfType<Rocket>();
}
// Update is called once per frame
void Update()
{
Vector2 paddlePos = new Vector2(transform.position.x, transform.position.y);
if (numberOfPaddles > 1)
{
distancePaddleCanWork
}
else
{
paddlePos.x = Mathf.Clamp(GetXPos(), minX, maxX);
transform.position = paddlePos;
}
}
private float GetXPos()
{
if (theGameStatus.IsAutoPlayEnabled())
{
return theRocket.transform.position.x;
}
else
{
return Input.mousePosition.x / Screen.width * screenWidthInUnits;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
How to know if the mouse has moved 0 Answers
Inconsistent raycast update 0 Answers
BCE0019: 'position' is not a member of 'Object'. 0 Answers
Drag Object with Touching 0 Answers