where ever i click the mouse the character should go there in a surface.my need is i want to move the character according to the mouse click.when i click the mouse button character should stand where i clicked the mouse.at the point of the mouse i want a small circle of green light to identify the mouse point where i click
This has been answered here click-to-move. To sum it up:
- cast a ray from the camera
- if it hits something get the location of the collision
- set your "green light" location
- move your object to this point
If you want to learn how to write this yourself (or understand how it works) a great place to start with the code would be the last example of Physics.Raycast in the unity docs. For the green light effect I would Instantiate a partical emitter at the target location.
using UnityEngine;
using System.Collections;
public class Pcontllorer : MonoBehaviour {
public float PlayerMoveSpeed;
private Vector3 TargetPosition;
void Start () {
if (!networkView.isMine)
{
enabled = false;
TargetPosition = transform.position;
}
}
void Update () {
if (networkView.isMine)
{
if (Input.GetMouseButtonDown(0))
{
Plane playerPlane = new Plane(Vector3.up, transform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float hitdist = 0.0f;
if (playerPlane.Raycast(ray, out hitdist))
{
Vector3 targetPoint = ray.GetPoint(hitdist);
TargetPosition = ray.GetPoint(hitdist);
Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
transform.rotation = targetRotation;
}
}
transform.position = Vector3.Lerp(transform.position, TargetPosition, Time.deltaTime * PlayerMoveSpeed);
}
}
void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
{
if (stream.isWriting)
{
Vector3 pos = transform.position;
stream.Serialize(ref pos);
}
else
{
Vector3 posRec = Vector3.zero;
stream.Serialize(ref posRec);
transform.position = posRec;
}
}
}