- Home /
moving object while it has another movement
I want to code hypercasual mobile game "push'em all". I have 2 questions. However firstly, I couldnt figure out how pusher object moves. In game, if I release my finger, player stops and pusher object pushes its enemy. But the thing is ; while it is moving forward and backward, its both position and rotation also is changing according to player object. I mean after you release your finger, while pusher object is moving, you can change the rotation and position of player again, and somehow the pusher object moves accordingly. How can it manage 2 different rotation and position I am totally lost. In my code, it pushes forward and back when I release my finger, but the moment when I press again, the pusher object comes back to its original position immediately beacuse of the first2 line of if statement(Input.GetMouseButton(0)).
Secondly, my pusher object doesnt move forward every time when I release my finger. Someone said "use Input.GetMouseButtonUp(0)" so that it works only mouse release, but in this case, pusher object doesnt come back.
If you could give any suggestion, I really be appreciated. Thanks in advance:)
it is what I did so far;
private void Update()
{ **rb is rigidbody
if (Input.GetMouseButtonDown(0))
{
firstTouchPos = Input.mousePosition;
}
if (Input.GetMouseButton(0))
{
TransformMovingPusher.position = StablePusher.position;
TransformMovingPusher.rotation = StablePusher.rotation;
_pushing = false;
deltaTouchPos = Input.mousePosition - firstTouchPos;
direction = new Vector3(deltaTouchPos.x, 0f, deltaTouchPos.y);
rb.velocity = direction.normalized * Speed;
if (rb.velocity != Vector3.zero)
{
rb.rotation = Quaternion.LookRotation(rb.velocity);
}
}
//mouse released
else
{
_pushed = true;
rb.velocity = Vector3.zero;
StartCoroutine("Push", 2);
}
}
private IEnumerator Push(float duration)
{
if (!_pushing)
{
Debug.Log("forward!");
MovingPusher.AddForce(StablePusher.forward * 500);
MovingPusher.velocity = transform.TransformDirection(0, 0, 3);
yield return (new WaitForSeconds(2));
_returning = true;
_pushing = true;
}
else if (_returning)
{
Debug.Log("backward!");
TransformMovingPusher.position = Vector3.MoveTowards(TransformMovingPusher.position, StablePusher.position, 0.05f);
yield return (new WaitForSeconds(1.5f));
_returning = false;
}
else
{
TransformMovingPusher.position = StablePusher.position;
yield break;
}
}
Answer by SadeqSoli · May 31, 2021 at 03:25 PM
Hi @gkanonimo , I think you should check your booleans as I saw them you're giving them wrong values in the wrong places. and if you don't want any rotation use Quaternion.Identity and make sure using Fixed update since you are using rigidbody to avoid any glich or errors like that, BTW you can also use Input.GetMouseButtonDown(0) and Input.GetMouseButtonDown(1) to make sure there is no confusion for user input system. hope it hepls you, good luck, cheers.
Your answer
Follow this Question
Related Questions
Vertical Camera Orbit 0 Answers
Push/Pull Physics - Cube 1 Answer
gameobject still rotating over z axis 0 Answers