Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by itvarth · Jan 03, 2020 at 03:20 PM · scripting problemscript.timesame

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();
      }
  }







Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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);
           }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image itvarth · Jan 03, 2020 at 04:27 PM 0
Share

I made some changes to the code you gave me but it worked: D thank you very much

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

221 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges