Author Topic: How to Add Shoot Direction Cursor and Specify Specific Spawn Point?  (Read 119 times)

Offline ActiveDream

  • Member
  • *
  • Posts: 5
    • View Profile
Hello everyone,

My apologies if this is probably noob questions. Please help me, I am still learning.

I have looked at the codes for Tanks Multiplayer, at the moment the spawn points are randomized. I wonder how to make it specific for example Player 1 in Team Red always spawn in TransformPosition1Red, Player 2 in Team Red always spawn in TransformPosition2Red, Player 1 in Team Blue always spawn in TransformPosition1Blue, and so on?

Also, if anyone have played Tanks A Lot in Android or iOS, https://www.youtube.com/watch?v=h1xufo9wu9o
before shooting the tank have a direction cursor like a line or a cone, how to add that to this Tanks Multiplayer?

Thank you for your help and reply, I greatly appreciate it.

Best Regards,

Dennis

Offline Baroni

  • Administrator
  • *****
  • Posts: 1399
    • View Profile
Re: How to Add Shoot Direction Cursor and Specify Specific Spawn Point?
« Reply #1 on: January 13, 2019, 05:09:54 pm »
Hello Dennis, welcome to the forum.

Just to clarify for others reading this: the spawn points are not fully random, but random within the assigned team area. So red players will always spawn at a random position around "spawn point red".

In order to have specific spawn points for each player, you basically answered your question yourself - you will need more Transform game objects, probably one for each player. Look at the GameManager component in the inspector. It stores a list of all team spawn transforms, so you will need to place all of your transforms in the Game scene and assign them there. Then open up the code where the spawn points and randomization is happening. Figure out which player number is joining and let it spawn at the associated transform position (potentially by index).

Players in Tanks Multiplayer already have a crosshair for determining the shot direction on both desktop and mobile. If that's not enough or you would like to replace it with a line, I would suggest looking at LineRenderers and adding that to the "TankTurret" on the player as a starting point, and the "shotPos" game object (+offset) as a destination.

I know that I've answered your questions with basically only the concepts, but I hope this helps in getting more familiar with Unity / our asset and coding in general.
« Last Edit: January 13, 2019, 05:11:32 pm by Baroni »

Offline ActiveDream

  • Member
  • *
  • Posts: 5
    • View Profile
Re: How to Add Shoot Direction Cursor and Specify Specific Spawn Point?
« Reply #2 on: January 14, 2019, 02:36:14 pm »
Hi Baroni,

Thank you for your reply. The aim indicator that comes with Tanks Multiplayer does not work on Mobile for me. However, the aim indicator works if I just play Tanks Multiplayer on Unity Editor. I am not sure which codes I should fix to get it working on Mobile.

I am trying to make a new game mode in Tanks Multiplayer inspired by Brawl Stars. The bullet direction cursor could be a line, a cone, or a circle AOE depending on the tank prefab and the bullet whether it is a spread bullet (cone cursor), a rocket (circle AOE cursor), or a regular bullet (line cursor). I am not sure how to incorporate LineRenderer into the Player Script. Could you give me some code examples or guidance please?

Thank you for your help, I highly appreciate it

Best Regards,

Dennis 

Offline Baroni

  • Administrator
  • *****
  • Posts: 1399
    • View Profile
Re: How to Add Shoot Direction Cursor and Specify Specific Spawn Point?
« Reply #3 on: January 14, 2019, 09:30:33 pm »
The aim indicator that comes with Tanks Multiplayer does not work on Mobile for me.

Are you using Photon by any chance? I've found an issue in the execution order that did not instantiate the aim indicator correctly. If you put "yield return new WaitForEndOfFrame();" at the beginning of the Start() method, UIGame script, it should work correctly. You have to test it on a mobile device as the aim indicator is only instantiated there.

I am not sure how to incorporate LineRenderer into the Player Script. Could you give me some code examples or guidance please?

First off, you wouldn't do it by script but in the Tank prefab as a prototype. It could look like this. I would suggest playing around with the LineRenderer component until you've achieved a satisfying result. Then you can start creating other LineRenderer effects too.

For integrating them into the Player script, one way would be to instantiate the desired LineRenderer when the player shoots (for the server and all clients) in CmdShoot. You could check the currentBullet variable for the index and then instantiate the correct Line. You could add an array to the Player script for holding all the different Line prefabs, or add them to the tank prefab already so you don't have to instantiate but only need to active them (which saves performance).

Offline ActiveDream

  • Member
  • *
  • Posts: 5
    • View Profile
Re: How to Add Shoot Direction Cursor and Specify Specific Spawn Point?
« Reply #4 on: January 19, 2019, 03:41:22 pm »
Hi Baroni,

Thank you for your reply. The Aim Indicator now works on Mobile after I add those codes that you mentioned in the Start() Method in UIGame Script.

Yes, what I would like to do in the new game mode is basically similar to this existing aim indicator except that the indicator is in Line, Cone or Ground Circle AOE shape. The aim indicator that I would like it to be, only shows when the right joystick is dragged, then player choose a direction to shoot. After that the player then release the right joystick, then the whole tank and turret rotates and shoots in that chosen direction. Similar to this in Brawl Stars:

For Cone or Line Aim Indicator Shape
https://www.youtube.com/watch?v=49RQZyJIAy4

For Ground AOE Circle Aim Indicator Shape (Starts from 0:22 in the video)
https://www.youtube.com/watch?v=bgIKspm0yeo

Note that in the video above, the character keeps moving normally with left joystick when the line/cone/circle aim indicator is shown with the right joystick when player drag the right joystick. After player choose the shooting direction, then the player let go of the right joystick then, character rotates and shoots bullets at chosen direction.

Thus if we translate that to Tanks Multiplayer, the tanks and the turrets rotation are always point at the same direction until player let go of the right joystick after choosing shooting direction from Aim Indicator.

In order to do that here in Tanks Multiplayer. After I add LineRenderer component to TankPrefab with line/cone/ground circle shape. Which codes should I modify so that it works like the video above from Brawl Stars?

Thank you for your help and support, I greatly appreciate it.

Best Regards,

Dennis

Offline Baroni

  • Administrator
  • *****
  • Posts: 1399
    • View Profile
Re: How to Add Shoot Direction Cursor and Specify Specific Spawn Point?
« Reply #5 on: January 20, 2019, 10:13:17 pm »
Hey, thanks for the explanation.

Modifying Tanks Multiplayer with some "delay targeting" as seen in the videos requires new additions in the Player script. It's not that trivial as adding some coroutines to the code, so I'll try to point out the places in code you would want to experiment with:

Player.Start():
At the end of it, on mobile devices joystick events are mapped to actions in the Player script. E.g. when dragging the right joystick, Shoot() is called continuously. Depending on the exact result you're after, you would want to create a new method linked to the onDragEnd callback instead, which calls all three current methods hooked up ( ShootBegin(), RotateTurret(), Shoot() ) in the most logical order. So your new method would be executed on releasing the joystick, instead of when dragging it.

Currently the aim indicator is at a fixed position, attached to the "shotPos" transform. If you want to move the aim indicator closer and further away (like in Brawl Stars) in the direction the joystick is being dragged, you could create a new method for repositioning the aim indicator and use the onDrag callback to execute it.

UIJoystick.OnEndDrag():
Note that the onDragEnd callback does not return a Vector2 variable with the current joystick direction. Actually, in the UIJoystick.OnEndDrag method, the joystick position is reset before executing the callback. If you want to access the joystick direction in your new method e.g. for letting the turret rotate over time, you would need to modify the onDragEnd callback, as well as changing the callback order in UIJoystick.OnEndDrag. It could be that you don't need this though.

With this as a starting point, I would try to go on from there and see what it looks like, before doing more changes and implementing different aim indicator shapes.
« Last Edit: January 20, 2019, 10:20:45 pm by Baroni »

Offline ActiveDream

  • Member
  • *
  • Posts: 5
    • View Profile
Re: How to Add Shoot Direction Cursor and Specify Specific Spawn Point?
« Reply #6 on: January 28, 2019, 02:34:15 pm »
Hi Baroni,

Thank you for your reply. I have been stuck for few days trying to get the Aim Indicator to move according to the direction of right joystick. Could you give me some code examples on how to do this please?

I think the codes have to check as well for whether the Aim Indicator displayed is a cone, a line or a ground targeting circle. The cone and the line Aim Indicator only rotate using the current tank location as its pivot. Whereas, the ground targeting circle Aim Indicator could move freely anywhere without pivot only clamped by screen size on Mobile. I am completely clueless how to accomplish this in codes. Please help me.

Thank you for your help and support, I greatly appreciate it.

Best Regards,

Dennis

Offline Baroni

  • Administrator
  • *****
  • Posts: 1399
    • View Profile
Re: How to Add Shoot Direction Cursor and Specify Specific Spawn Point?
« Reply #7 on: January 30, 2019, 08:10:09 pm »
While I cannot provide code samples for the specific implementation you've requested, since that would be custom code that should be added by you, the base functionality is already there.

To summarize, you either have (1) a player-controlled aim indicator that moves on the ground, or (2) an aim indicator that is fixed to the player, but rotates with the turret. You need to define your aim indicator prefabs in the Player script, and instantiate them according to the selected bullet index. In addition, you need to re-map the joystick controls in the player script to your custom method that checks the current aim indicator and moves or rotates it.

A joystick, when moved, returns a Vector2 variable with the direction it is moved to (x/y axis from -1 to 1), similar to the arrow keys on desktop devices.

Both moving and rotating an object using a Vector2 variable as input is already demonstrated in the Player script:
(1) moving the player tank itself using the left joystick: void Move(Vector2 direction = default(Vector2))
(2) rotating the turret using the right joystick: void RotateTurret(Vector2 direction = default(Vector2))

By looking at these methods, you should be able to implement your own move/rotate aim indicator method processing a Vector2 as input. I'm sorry to not provide code, but this asset is meant as a tutorial/template after all. If you have general issues with programming, I would recommend checking out more beginner friendly tutorials either by Unity directly (they offer a lot of them) or searching for online resources that answer your specific coding questions.

Offline ActiveDream

  • Member
  • *
  • Posts: 5
    • View Profile
Re: How to Add Shoot Direction Cursor and Specify Specific Spawn Point?
« Reply #8 on: February 09, 2019, 04:43:58 pm »
Hi Baroni,

Thank you for your reply. I understand more of the concept now. The past few days I have been trying to create a flat line, cone and circle using Line Renderer. But I cannot make it stay flat, when I drag it left or right the drawings from the LineRenderer rotate. How to make it stay flat?

Also, how to make it so that player who join the room wait until max players are reached then every players in room spawn at same time in game scene?

Thank you for your help and support

Best Regards,

Dennis

Offline Baroni

  • Administrator
  • *****
  • Posts: 1399
    • View Profile
Re: How to Add Shoot Direction Cursor and Specify Specific Spawn Point?
« Reply #9 on: February 09, 2019, 07:33:23 pm »
Hi,

But I cannot make it stay flat, when I drag it left or right the drawings from the LineRenderer rotate. How to make it stay flat?

that's something you could have found online, e.g. here, or in the official manual itself: try setting its 'Alignment' to 'Local'.

Also, how to make it so that player who join the room wait until max players are reached then every players in room spawn at same time in game scene?

You would be looking at the OnPhotonPlayerConnected method, NetworkManagerCustom script. In there, the RPC for adding the player is called directly, at the end. Instead, you would need to add a check for the current player count and if the desired player count has been reached, fire that RPC for all players.