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 TrickShot_Lord · Dec 08, 2018 at 04:13 PM · transformpositionclickfindgameobjectswithtag

So here is some really complicated question.

I'm really sorry to lead you to a really complicated question like this. As you can see my enzyme character click to a substract and make it move actually follow the character. But with this kind of script it is actually moving every substract in game scene to it. I couldn't find any solution to actually reduce it to just one substract when clicked. Should I write a script for every substract in game, how. Or should I change my characters script. If you couldn't understand my question just respond me. Sorry for my high school level engIish. I'd be very pleased if you'd show some interest in my script. Thank you.

[RequireComponent(typeof(Rigidbody2D))] public class EnzymeController : MonoBehaviour {

 [SerializeField] private float v_MovementSpeed = 10f;
 [SerializeField] private float v_MovementSmoothing = .1f;
 [SerializeField] private float v_VerticalMove;
 [SerializeField] private float v_HorizontalMove;
 [SerializeField] private float v_SubstractSmoothing;

 [SerializeField] private Vector2 v_EnzymeDirection;
 [SerializeField] private Vector3 v_MousePosition;

 [Range(0, 2)] [SerializeField] private float v_ColliderRadius;
 [SerializeField] private bool b_CanAttach = false;
 [SerializeField] private bool b_Attach = false;   
 [SerializeField] private Transform r_ColliderPos;
 [SerializeField] private LayerMask lm_WhatIsSubstract;

 private Rigidbody2D r_Rigidbody;
 private Vector2 m_Velocity;
 //Substract r_Substract;
 private GameObject[] r_Substract;
 private Transform r_Pointer;
 private Transform r_SubstractTank;

 void Start () {
     r_Rigidbody = GetComponent<Rigidbody2D>();
     r_Substract = GameObject.FindGameObjectsWithTag("Substract");
     r_SubstractTank = GameObject.FindGameObjectWithTag("SubstractTank").GetComponent<Transform>();
 }

 private void Update()
 {
     AttachSubstract();
     MouseDirection();
 }

 void FixedUpdate () {
     v_HorizontalMove = Input.GetAxisRaw("Horizontal");
     v_VerticalMove = Input.GetAxisRaw("Vertical");

     Vector2 _TargetVelocity = new Vector2(v_HorizontalMove * v_MovementSpeed, v_VerticalMove * v_MovementSpeed);
   
     PerformMovement(_TargetVelocity);
     SubstractMovement();
 }

 private void PerformMovement(Vector2 targetVelocity)
 {
     r_Rigidbody.velocity = (Vector2.SmoothDamp(r_Rigidbody.velocity, targetVelocity, ref m_Velocity, v_MovementSmoothing));
 }

 private void MouseDirection()
 {
     v_MousePosition = Input.mousePosition;
     v_MousePosition = Camera.main.ScreenToWorldPoint(v_MousePosition);

     v_EnzymeDirection = new Vector2
         (v_MousePosition.x - transform.position.x,
          v_MousePosition.y - transform.position.y);

     transform.up = v_EnzymeDirection;
 }

 
 private void AttachSubstract()
 {
     

     b_CanAttach = Physics2D.OverlapCircle(r_ColliderPos.position, v_ColliderRadius, lm_WhatIsSubstract);

     if(b_CanAttach && Input.GetButtonDown("Fire1"))
     {
         b_Attach = true;
     }
     else if(b_Attach == true && Input.GetButtonDown("Fire1") || b_CanAttach == true && b_Attach == true &&Input.GetButtonDown("Fire1"))
     {
         b_Attach = false;
     }
 }

 private void SubstractMovement()
 {
     for (int i = 0; i <= r_Substract.Length; i++)
     {
         if (b_Attach == true)
         {
             Vector2 _DesiredPosition = r_SubstractTank.position;
             Vector2 _TargetPosition = Vector2.Lerp(r_Substract[i].transform.position, r_SubstractTank.position, v_SubstractSmoothing);

             r_Substract[i].transform.position = _TargetPosition;
         }
     }
 }
 

}

Comment
Add comment · Show 2
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 TrickShot_Lord · Dec 08, 2018 at 12:23 PM 0
Share

in short when character clicks to an object by its overlapcircle I want to make it follow me. but with this script every objects tagged "Substract" follows it. It must be just one object. I just couldn't find any solution.

avatar image Vega4Life TrickShot_Lord · Dec 08, 2018 at 04:37 PM 1
Share

So once you have an attached follower, you don't want anymore to follow?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Vega4Life · Dec 08, 2018 at 04:56 PM

Here is what I think you want to do. Create a Collider2D member variable:


 Collider2D currentAttached;


Then change this:


 b_CanAttach = Physics2D.OverlapCircle(r_ColliderPos.position, v_ColliderRadius, lm_WhatIsSubstract);


To actually storing the collider2D that was found overlapped:


 currentAttached = Physics2D.OverlapCircle(r_ColliderPos.position, v_ColliderRadius, lm_WhatIsSubstract);
  

Then we change to a null check with our Fire button:


 if(currentAttached != null && Input.GetButtonDown("Fire1"))
  {
      b_Attach = true;
  }


Then, we don't use the array of subtracts anymore, just the one collider we found:


     private void SubstractMovement()
     {
         if (b_Attach == true && currentAttached != null)
         {
             Vector2 _DesiredPosition = r_SubstractTank.position;
             Vector2 _TargetPosition = Vector2.Lerp(currentAttached.transform.position, _DesiredPosition, v_SubstractSmoothing);
             currentAttached.transform.position = _TargetPosition;
         }
     }


It doesn't need to be exactly like all this code I suggested, but its enough to get you going I think. Good luck.


Comment
Add comment · 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

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

136 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

Related Questions

how can do click then move on a gameobject? 1 Answer

Line render from an object to a click 0 Answers

Find a gameobject with the same position 2 Answers

Getting to move object with mouse position? 0 Answers

Storing transforms from objects in Array 1 Answer


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