0
\$\begingroup\$

Link:https://academo.org/demos/rotation-about-point/

    private bool isTiming = false;
    private float timeTillKeyIsPressed = 0;

    float time = 1;  
    float time2 = 4;

    void Update()
    {
       if (isTiming)
        {
           if (timeTillKeyIsPressed <= time)  //0<=1 1<=1 
            {
                if (time2 != 0) //4!=0 4!=1 4!=2 4!=3 4!=4
                {
                    time2 -= Time.deltaTime;

                    float angle = 360;   // reset here but not work properly


                    rb.transform.Rotate(Vector3.forward * angle * Time.deltaTime / time);

            }
        }
    }

    public void Rotate(float angle)  
    {
        float d = angle * Time.deltaTime;

        int movement = GetMovement();

        rb.transform.Rotate(Vector3.forward * d * (float)movement * speed);



    }

  public int GetMovement()
  {
        if (Input.touchCount > 0)                              //mobile     //how many count touch  
        {
            Touch touch = Input.GetTouch(0);


            float num = (float)(Screen.width / 2);


            var touchres = touch.position.x;
            //DebugPanel.Log("touchres", "touchres:" + touchres);

            var res = touchres > num;
            //DebugPanel.Log("res", "res" + res);

            if (res)//TRUE(Right)
            {


                return -1;
            }
            else//FALSE(Left)
            {


                return 1;
            }
        }
        else
        {
            return (int)Input.GetAxisRaw("Horizontal") * -1;     //pc 
            //return 0;
        }
    }
  void OnTriggerEnter2D(Collider2D other)  //here my obstacle touch my player
    {
        foreach (Transform child in transform)
        {
            if (child.tag == "point")
            {
                isTiming = true;
            }
        }
    }

Image:

when I m playing the game:

enter image description here

I want the:

enter image description here

Player Image:

enter image description here

I want the when my player touches my obstacle then I want to reset the position of my player??

I hope my question is understood.

\$\endgroup\$
  • \$\begingroup\$ Do yo mean when you play game in the editor its plays in portrait mode instead of landscape. or you mean when you play game on physical device then it plays in portrait mode instead of landscape. you question is confusing. \$\endgroup\$ – Swati Aug 31 '19 at 14:18
  • \$\begingroup\$ @Swati thanks. I edited my question. when my player touch obstacle then I want the reset position of my player. \$\endgroup\$ – Rahul Patil Aug 31 '19 at 14:23
  • \$\begingroup\$ I am sorry i am not really sure what do you mean. do you mean that when player 1 touches the obstacle then you want to reset the player 1 position to start position? And as you have two player then do you want to reset both player position when either player 1 or player 2 touches the obstacle? \$\endgroup\$ – Swati Aug 31 '19 at 14:30
  • \$\begingroup\$ @Swati I want both players position reset when touching the obstacle. my game working perfectly.issue is when my both player touch the obstacle then I want to reset the position of my player. I hope my question is understood. \$\endgroup\$ – Rahul Patil Aug 31 '19 at 14:48
  • \$\begingroup\$ OK. i am answering your question in few mins. \$\endgroup\$ – Swati Aug 31 '19 at 14:49
0
\$\begingroup\$

Create a new script Call it any thing you want, I named it "ReseatingPlayer" and then past the following code into it:

    public static ReseatingPlayer instance;
    [SerializeField] private Transform playersParent;
    [SerializeField] private Transform redPlayer;
    [SerializeField] private Transform bluePlayer;

    [Space(10)]
    [SerializeField] private float redPlayerStartX;
    [SerializeField] private float bluePlayerStartX;

    private Vector3 playersParentStartPosition;

    private void Start()
    {
        instance = this;
        playersParentStartPosition = playersParent.position;
    }

    public void ResetPlayers(float delay)
    {
        Invoke("ResetPlayersPositions", delay);
    }

    public void ResetPlayersPositions()
    {
        redPlayer.localPosition = new Vector3(redPlayerStartX, 0, 0);
        bluePlayer.localPosition = new Vector3(bluePlayerStartX, 0, 0);
        playersParent.position = playersParentStartPosition;
    }

Now attach the script to players parent assign the values to it like I did:

enter image description here

redPlayerStartX: assign red player start X position to it.

bluePlayerStartX: assign blue player start X position to it.

How to re seat player: if you want delay this do it like: 4 means that re seat player after 4 secs.

 ReseatingPlayer.instance.ResetPlayers(4);

if you don't want any delay:

ReseatingPlayer.instance.ResetPlayersPositions();

I hope this will do the job.

\$\endgroup\$
  • \$\begingroup\$ I change my code but nothing happens. Logic: when player collided with the obstacle then both player rotation 360 degrees from his last position(means last touch of obstacle) but how to get the last touch position of player when collided the obstacle and then increment(+) 360 Degree. \$\endgroup\$ – Rahul Patil Aug 31 '19 at 19:55
  • \$\begingroup\$ ReseatingPlayer.instance.ResetPlayersPositions(); have you called this from ontrrigerenter2d? \$\endgroup\$ – Swati Sep 1 '19 at 6:53
  • \$\begingroup\$ Also about 360 angle rotation use unity free plugin called DoTween. \$\endgroup\$ – Swati Sep 1 '19 at 6:54
  • \$\begingroup\$ If you need any help about dotween let me know. To be honest i not really understanding your question about 360 angle rotation. So if you need help i share my whats app number with so you call me. \$\endgroup\$ – Swati Sep 1 '19 at 6:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.