- Home /
Character follow the mouse click
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
UA is generally intended for specific technical questions. For general, open-ended 'how-to' questions, try the forums. To look for volunteers to write the code for you, try the 'collaboration' forum. To look for someone to hire to write the code for you, try the 'paid work' forum.
@robertmathew I dont think you quiet understand the way UA works.
Answer by Mofy · Mar 30, 2011 at 10:26 AM
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.
Answer by Arderku · Dec 20, 2011 at 11:26 PM
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; } } }
Your answer
Follow this Question
Related Questions
3rd person click to move; issues following terrain. 2 Answers
Vector3.MoveTowards gives inconsistent speed when pathfinding between waypoints. 2 Answers
click to move script goes through objects 0 Answers
Play appropriate animation based on character's direction (2D point and click) 1 Answer
Raycast won't hit Collider 1 Answer