- Home /
Question by
Binnebro · Feb 02, 2021 at 12:29 PM ·
scripting problemscripting beginnerscriptingbasicsspherecast
How to add force if spherecast is true and how to set direction of sphere
I have made a script but it doesnt seem to work
public float sphereRadius = 1;
public float pushSphere = 5;
public float maxDistance = 10;
public LayerMask layerMask;
public GameObject hitObject;
private Vector3 origin;
private Vector3 dir;
public Transform player;
public float pushForce = 10;
public float pushRadius;
public GameObject Cilinder;
void Update()
{
if(Input.GetKeyDown(KeyCode.R))
{
ForcePush();
}
}
void ForcePush()
{
origin = transform.position;
RaycastHit hit;
dir = player.localPosition;
if(Physics.SphereCast(origin, sphereRadius, dir, out hit, maxDistance, layerMask,
QueryTriggerInteraction.UseGlobal))
{
GameObject hitObject = hit.collider.gameObject;
Rigidbody hitBody = hitObject.transform.GetComponent<Rigidbody>();
if(hitObject.gameObject.CompareTag("Enemy"))
{
hitBody.AddExplosionForce(pushForce, Vector3.up, pushRadius);
}
}
}
}
Comment
Cleaning up your formatting will make it much easier for people to read. If you are using visual studio press ctrl+k+d
Your answer
Follow this Question
Related Questions
looking for a way to set an integer to another number based on a condition. 2 Answers
Attacking mulitple targets simultanously 2 Answers
How to return control back to the original script? 1 Answer
Trying to find the highest number than add it to itself. 2 Answers
Having trouble using SendMessage 1 Answer