- Home /
Instantly move my player to a position I click on (2d)
I have a script on my player object that sets a bool named "charge" to true when I click on it. This is a precondition to moving my player somewhere else in the play area which is determined by where I click next.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerCharge : MonoBehaviour {
private bool charged;
// Use this for initialization
void Start () {
charged = false;
}
void Update ()
{
//Makes the player spin after it gets charged up
// if (charged !=false) {
// transform.Rotate (0,0,1440*Time.deltaTime); //rotates 50 degrees per second around z axis
// }
}
// Click the player to set charge it up
void OnMouseDown () {
if (charged != true) {
charged = true;
// chargedSpin ();
Debug.Log ("Charged Up!");
}
else Debug.Log ("already");
}
}
This first part works, but now I want to: Create a new script on the background sprite that captures my click and stores the transform info for where I clicked, then, if my player object has the charge = true state, instantly place my player at the position I clicked.
I'm having trouble with the approach - would I be right to put that script on the background sprite? if so, how can I a) get the variable from my player script and b) move my player object using this script.
I've tried looking into how to get scripts to communicate with each other but I'm having no joy.
Answer by JaredHD · Feb 07, 2017 at 02:38 PM
I would use a Raycast from the camera to the mouse position. Then if charged is true move the player to the Raycasted area.
So something like this:
using System;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
bool charged = false;
private void Update()
{
if (Input.GetButtonDown("Fire1"))
{
RotatePlayer();
MovePlayer();
}
}
private void RotatePlayer()
{
//Your rotate script
}
private void MovePlayer()
{
//Creates a Ray from Camera to mouse
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//Used to store the Raycast hit
RaycastHit hit;
//If we hit something place that in the "hit" variable and if charged is true move to point
if (Physics.Raycast(ray, out hit) && charged == true)
{
transform.position = hit.point;
}
}
}
That goes on the player.
Bonus: How to communicate with different scripts?
Lets say you have a script on the player and you have a script on an enemy. You press on the enemy and want to know how much health he has and display it to the Console. You would use the Debug.Log(GameObject.Find("Enemy").GetComponent<EnemyAi>().health);
GameObject.Find("Transform Name") is the easy way to do and not always recommended. Rather use tags. This finds the enemy that has the script attached to it.
.GetComponent().Variable; That retrieves the specific Script by Class name and the variable you want to edit/read. You can use Methods instead of variables as well
Thanks for responding. I couldn't get that code to work, but I'll look into the approach and see if i can figure it out myself.
The Raycast would need to hit something like a terrain or cube. Here check out this Link and see if it might help.