Humble Request for a small c# script

Updated on October 15, 2018 in [A] 2D
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
4 on October 14, 2018

Hello everyone.

I am very new here and I believe that I will find an answer to my question here.

I am a 3d artist and has little knowledge of c# scripting. If anyone can help me out here, I shall be very very thankful.

I am working on a 2d unity game. I have an object and it rotates 360 degree automatically from its pivot point. All I want that if some tap on the screen then that object starts moving in the direction which it was pointing.

let suppose I have a canon which is rotating 360 degree and it has rotated let say 80 degree and some one tap on the screen then i want that canon move in that 80 degree direction at already defined speed.

I hope i have defined the problem in clear words.

Thank you all.

  • Liked by
Reply
0 on October 14, 2018

Do you want GUI so the player can use a “ball” to move around

  • Liked by
Reply
Cancel
2 on October 14, 2018

 

I have attached an illustration and i hope it will clear the idea that what I have requested.

I want the player to move in the direction of the arrow and the arrow rotate around from player at 360 degree. if someone tap on the screen then the play move in the direction of the arrow, whether the arrow is at 80 degree or 120 degree and any other degree angle.

 

image url: https://prnt.sc/l5v9v3

Helpful
on October 14, 2018

Well just use the rigidbody component and set the arrow in the forward direction (blue z-axis) of the game object. Then you can use something like this in the Update method:

    public Rigidbody2D rb;
    public float speed = 5f;
    private void Update()
    {
        if (Input.GetButtonDown("Fire1"))
        {
            rb.AddForce(transform.forward * speed, ForceMode2D.Impulse);
        }
    }

 

on October 15, 2018

thank you soooooooooooooooooooooooooooooooooooooooooooooooooo much. I really really appreciate it.

I tried your code but it didnt work. so i have changed TRANSFORM.FORWARD to TRANSFORM.UP and it has worked like a charm.

once again I am very thankful to you. thanks

 

Show more replies
  • Liked by
Reply
Cancel