- Home /
Dont throw balls at the same time
so I have the pickup scripts in the player and the addforce scripts in the balls and when i try to throw 1 ball, all the other ball They are also throwed TheAddForceScript is in the ball and the Pickup on the Player, Please makes me a example in a script, i am so beginer in C++
1.AddForceScript:
using UnityEngine;
public class AddRBForce : MonoBehaviour
{
public PickableObject PickableScript;
public float throwForce = 600;
Rigidbody rg;
void Start()
{
rg = GetComponent<Rigidbody>();
}
void Update()
{
if (PickableScript.shootObject && PickableScript.hasPlayer)
{
rg.AddForce(Camera.main.transform.forward * throwForce);
}
}
}
2.PickUpScript :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PickableObject : MonoBehaviour
{
public LayerMask RayMask;
private RaycastHit hit;
public float rayLenght = 1;
public GameObject HandPrefab;
[HideInInspector]public bool hasPlayer,shootObject;
bool useAnim;
Animator anim;
private Transform currentTransform;
private float lenght;
InputManager inputManager;
private void Start()
{
anim = GetComponent<Animator>();
inputManager = FindObjectOfType<InputManager>();
}
void Update()
{
anim.SetBool("Use", useAnim);
shootObject = inputManager.GetButtonDown("Fire2");
if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, rayLenght, RayMask))
{
if (hit.transform.tag == "PickableObject")
{
if(!hasPlayer)
useAnim = true;
if (inputManager.GetButtonDown("Use"))
{
SetNewTransform(hit.transform);
currentTransform.gameObject.layer = 15;
hasPlayer = true;
useAnim = false;
}
}
else
{
if(!hasPlayer)
useAnim = false;
}
}
else
{
if(!hasPlayer)
useAnim = false;
}
if (inputManager.GetButtonDown("Fire1") && hasPlayer)
{
KinematicsAndColliders();
RemoveTransform();
}
if (Input.GetKeyDown(KeyCode.B))
{
currentTransform.GetComponent<Rigidbody>().AddForce(currentTransform.transform.forward * 600);
RemoveTransform();
}
if (shootObject && hasPlayer)
{
KinematicsAndColliders();
StartCoroutine(RemoveDelay());
}
if (currentTransform)
MoveTransformAround();
}
public void SetNewTransform(Transform newTransform)
{
if (currentTransform)
return;
currentTransform = newTransform;
lenght = Vector3.Distance(Camera.main.transform.position, newTransform.position);
currentTransform.GetComponent<Rigidbody>().isKinematic = true;
currentTransform.GetComponent<Collider>().isTrigger = true;
}
private void MoveTransformAround()
{
currentTransform.position = Camera.main.transform.position + Camera.main.transform.forward * lenght;
}
public void RemoveTransform()
{
hasPlayer = false;
useAnim = false;
if (!currentTransform)
return;
currentTransform = null;
}
void KinematicsAndColliders()
{
currentTransform.gameObject.layer = 0;
currentTransform.GetComponent<Rigidbody>().isKinematic = false;
currentTransform.GetComponent<Collider>().isTrigger = false;
}
IEnumerator RemoveDelay()
{
yield return new WaitForSeconds(0.001f);
RemoveTransform();
}
}
Answer by unity_ek98vnTRplGj8Q · Jan 03, 2020 at 03:59 PM
Okay there are a few issues with your code but to address your specific problem, all of your balls are being thrown because nowhere are you telling them which ball needs to be thrown. Each ball has the throw script, and the force is added when hasPlayer
and shootObject
are true, but these are only true when the player wants to shoot any object, not just the current one.
The best way to fix this would be to completely reorganize your code, but that will be a task left to you. The quickest and easiest fix I can see would be this, following how you are currently handling everything:
In your player script----------
public LayerMask RayMask;
private RaycastHit hit;
public float rayLenght = 1;
public GameObject HandPrefab;
[HideInInspector]public bool hasPlayer,shootObject;
[HideInInspector] public GameObject objectToThrow; //ADD THIS LINE
And
if (hit.transform.tag == "PickableObject")
{
if(!hasPlayer)
useAnim = true;
objectToThrow = hit.gameObject; //ADD THIS LINE
if (inputManager.GetButtonDown("Use"))
{
SetNewTransform(hit.transform);
currentTransform.gameObject.layer = 15;
hasPlayer = true;
useAnim = false;
}
}
In your ball script---------------------
//EDIT THIS LINE
if (PickableScript.shootObject && PickableScript.hasPlayer && (PickableScript.objectToThrow == gameObject))
{
rg.AddForce(Camera.main.transform.forward * throwForce);
}
I made some changes to the code you gave me but it worked: D thank you very much
Your answer
Follow this Question
Related Questions
Wanting timer to run after single click 2 Answers
How can i make both two cameras to follow the player but only one with control on player ? 0 Answers
When the mouse is moving and the player is walking why the player is stuttering ? 0 Answers
Slowing or putting a timer on script 0 Answers
Wait for animation to finish 2 Answers