Okay I’m stuck. I’m still kind of new to C# so go easy on me. This script is attached to the circle. In the script below im trying to call out the Player Object and then move it towards the location of the Circle Object once the circle is touched (once). (meaning nothing happens if I tap outside of the Circle Object. Keep in mind I don’t want a collision. Just a simple move here to circle location.) Need help ASAP! Thank You! (CODE IS INCOMPLETE)
using UnityEngine;
using System.Collections;
public class MovePlayerHere : MonoBehaviour {
private GameObject player;
// Use this for initialization
void Start () {
player = GameObject.Find (“Player”);
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown (0)) {
player.transform.position;
}
}
}
OK, the general answer I will give is that you should move the player as a player, not the player as some other part. Separate your concerns.
That being said, lets look at this in a practical manner.
using UnityEngine;
using System.Collections;
public class MovePlayerHere : MonoBehaviour {
// private store for the player.
private GameObject player;
// units that this circle will move the player per second
public float speed = 2;
// Update is called once per frame
void Update () {
// this does a check every frame, rather than the start of this object
if(player == null) player = GameObject.Find ("Player");
// if we still dont have a player, avoid any bugs that will creep up.
if(player == null) return;
// this happens when we press the left mouse button.
if (Input.GetMouseButtonDown (0)) {
// direction is always target - current
Vector3 direction = transform.position - player.position;
// this converts the direction to a unit of meauser (1)
Vector3 normal = direction.normalized;
// this gets the distance that the speed is in units
Vector3 distanceToMove = normal * speed;
// now get the amount of speed that should be moved this frame
Vector3 distanceToMoveThisFrame = distanceToMove * Time.deltaTime;
// get the position from the player
Vector3 position = player.transform.position;
// add the distance we need to move to this current position
position = position + distanceToMoveThisFrame;
// apply that position back to the player.
player.transform.position = position;
}
}
}
Oh, and try to use code tags…
Edit… of course… half of this is replace with Vector3.MoveTowards()