Let’s say I have a slingshot system that requires the player to pull to throw but I only manage to limit the player pull back distance by position on screen by using Mathf.Clamp which it will lock the mousePosition within the limited screen position.
Vector2 mosPos = Input.mousePosition;
Vector3 vel = GetForceFrom(_skills.transform.position, Camera.main.ScreenToWorldPoint(mosPos));
Vector3 newLimit = new Vector3 (Mathf.Clamp (vel.x, -10, 10),Mathf.Clamp (vel.y, -10, 0));
How do I write a code that does the limitation on the mousePosition based on the distance?
Which do I even use and how do I write it?
Vector3.Distance
Vector3.magnitude
Vector3.sqrMagnitude
I want the limitation to be half circle at the bottom and only able to pull to a certain distance.
PS: Sorry if the word I used here are bad or confusing.
UPDATE: to the above code, here’s what its look like in full for the Update()
since it’s void I had to modified the code to test around but it still doesn’t limit the pull back range. Any thoughts?
void Update ()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(isSkillsThrown)
return;
if (isPlayerTurn)
{
if (Input.GetMouseButtonDown(0))
{
if (Physics.Raycast(ray, out hit) && hit.collider.CompareTag("Player"))
{
isPressed = true;
if (!_skills)
createSkills();
isClickOnObj = true;
}
else
{
isClickOnObj = false;
}
}
else if (Input.GetMouseButtonUp(0))
{
isPressed = false;
GameObject[] trajDes;
trajDes = GameObject.FindGameObjectsWithTag("Trajectory");
for (int i = 0; i < trajDes.Length; i++)
{
Destroy(trajDes*.gameObject);*
}
if (isClickOnObj == true)
{
if (!isSkillsThrown)
{
throwSkills();
}
}
//isPressed = true;
//[Remove comment below for unlimited shots]
ManualStart();
}
}
-
// when mouse button is pressed, cannon is rotated as per mouse movement and projectile trajectory path is displayed.*
-
if(isPressed)*
-
{*
//I would like to add the limit here
Vector2 mosPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 vel = GetForceFrom(_skills.transform.position, mosPos);
-
Vector3 newLimit = new Vector3 (Mathf.Clamp (vel.x, -10, 10),Mathf.Clamp (vel.y, -10, 0));*
-
Debug.Log("VEL: "+vel+" newLimit: "+newLimit);*
_ Vector3 newVel = newLimit * -1;_
-
setTrajectoryPoints(transform.position, newVel/_skills.GetComponent<Rigidbody>().mass);*
-
}*
-
}*
private Vector2 GetForceFrom(Vector3 fromPos, Vector3 toPos)
- {*
_ return (new Vector2(toPos.x, toPos.y) - new Vector2(fromPos.x, fromPos.y))* power;_ - }*
I had all the formula correct already but all I need to do left is to place the limit in that specific location which I only know by using Mathf.Clamp and use screen position.