- Home /
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;
}
}
}
}
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.
So once you have an attached follower, you don't want anymore to follow?
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.
Your answer
Follow this Question
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