How can i move a character along with the mouse in 3d??
That’s a loaded question: About a dozen different ways. One of the most popular is to raycast from the mouse position, in the direction the camera is facing, either every frame or on click. Using the raycast, you’d use some layer or “RaycastHit” checks to figure out what it is that needs to happen (if the raycast hit something on the “terrain” layer, or the object his had the “terrain” tag, then pass the Vector3 of the spot where the raycast hit the terrain object to the movement controller for the character, for instance).
Here’s a MoveToPoint function that you can use if you’re attaching a perfectly normal CharacterController script from the Unity StandardAssets to your character.
if(Input.GetMouseButtonDown(0))
{
Rayray=Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray,outhit))
{
obj=hit.collider.gameObject;
if(obj.name == “boy_anim_fbx”)
{
m_startpos=hit.point;
m_goboy.transform.position = m_startpos;
}
}
}
if(Input.GetMouseButtonUp(0))
{
m_endpos =hit.point;
m_goboy.transform.position = m_endpos;
}
i was trying like this but its not working…
“Not working” isn’t enough information. How isn’t it working exactly? I don’t know what half of these variables are because their definitions and how they’re being used aren’t here- so post the rest of the script too. Also, be sure to put any code you post in [code ][/code ] tags.
usingUnityEngine;
usingSystem.Collections;
publicclassRiding : MonoBehaviour
{
publicAnimatorm_boyAnimation;
publicGameObjectm_goboy;
Vector3m_startpos,m_endpos;
RaycastHithit;
GameObjectobj = null;
//Usethisforinitialization
voidStart ()
{
}
//Updateiscalledonceperframe
voidUpdate ()
{
if(Input.GetMouseButtonDown(0))
{
Rayray=Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray,outhit))
{
obj=hit.collider.gameObject;
if(obj.name == "boy_anim_fbx")
{
//m_boyAnimation.SetBool("walk",false);
m_startpos=hit.point;
m_goboy.transform.position = m_startpos;
}
}
}
if(Input.GetMouseButtonUp(0))
{
m_endpos =hit.point;
//m_boyAnimation.SetBool("walk",true);
m_goboy.transform.position = m_endpos;
m_goboy.transform.position = Vector3.MoveTowards(m_startpos,m_endpos,Time.deltaTime*1);
}
}
}
EDIT: Sorry, a bit annoyed because it’s still nearly impossible to read. Give me a minute.
Something more like this, perhaps? I’m not really sure what the point of the “OnMouseUp” stuff was.
using UnityEngine;
using System.Collections;
public class Riding : MonoBehaviour
{
public Animator m_boyAnimation;
public GameObject m_goboy;
private Transform m_goboyTransform;
private Vector3 m_goalPos;
void Start()
{
if (m_goboy != null)
m_goboyTransform = m_goboy.GetComponent<Transform>();
m_goalPos = m_goboyTransform.position;
}
void Update()
{
if (m_goboy != null)
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.name == "boy_anim_fbx")
{
m_goalPos = hit.point;
}
}
}
if (Vector3.Distance(m_goboyTransform.position, m_goalPos) >= .1f)
{
//m_boyAnimation.SetBool("walk", true);
m_goboyTransform.position = Vector3.MoveTowards(m_goboyTransform.position, m_goalPos, Time.deltaTime * 1);
}
else
{
//m_boyAnimation.SetBool("walk", false);
m_goalPos = m_goboyTransform.position;
}
}
}
}