[Right Click to Necromance] How I do the run system, for only one character? (FOR 2D RPG GAME)
Hello, i need help to make the run system of the game Right Click To Necromance, I creating a 2D Panoramic RPG Game. The system is : When the player is clicking, the character follow the cursor in speed of character, and when stops clicking, the character stop where he is. Sorry for my English, I'm brazilian. Thanks if you helped me!
Answer by Scoutas · Jan 10, 2017 at 03:12 AM
So, what you need to do:
1) Find the coordinate of where you clicked.
2) Send the player there, while the button is held down.
public class MovementScript : MonoBehaviour {
public Transform player;
// How many units you want your player to move per second.
public float playerSpeed;
// World coordinates under the mouse
Vector3 targetPosition;
void Update(){
// Get these world coordinates
// Input.mousePosition returns a position of the mouse on the screen.
// Camera.main.ScreenToWorldPoint uses the position of the mouse to convert it to a world position.
targetPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
// If the player is holding down the left mouse button ( GetMouseButton(0) is the left mouse button)
if (Input.GetMouseButton (0)) {
// Get the movement direction
Vector3 direction = targetPosition - player.position;
// Normalize it (make the vector magnitude be 1)
Vector3 normalizedDirection = direction.normalized;
// Add the direction to the current player position, with playerSpeed
player.position += normalizedDirection * playerSpeed * Time.deltaTime;
}
}
}
Your answer
Follow this Question
Related Questions
Apply walking animations to many people over Crowd simulation 2 Answers
Multy Player both players are moving 0 Answers
I have to start moving first to run, and cant start moving in the other direction when running. 0 Answers
How do I share my game with a friend, and do they need Unity as well? 1 Answer
running infinite 2 Answers