PointAndClick
How can i make the third person character from the sample assets walk via point and click.I added some lines of code to the controller into the FixedUpdate Method. ...
if (Input.GetMouseButton (0)) {
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, out hit, 1000)) {
move = new Vector3 (hit.point.x, hit.point.y, hit.point.z);
}
}
... But it is not working. Can someone help? Thanks
hit.point is already a Vector3, you can use it. Is the code reaching the move assignment (does the raycast work? set a debug to check)? are you doing anything with the move variable afterwards?
Answer by dna1978 · Aug 31, 2015 at 01:35 PM
I pasted the complete controller code.As i said it is from the sample assets.The lines i added do not work,so i commented them out.WASD movement works.Point and click movement should be easy to add,but i am to unexperienced to get it working. Thanks for help.
using UnityEngine; using UnitySampleAssets.CrossPlatformInput;
namespace UnitySampleAssets.Characters.ThirdPerson {
[RequireComponent(typeof (ThirdPersonCharacter))]
public class ThirdPersonUserControl : MonoBehaviour
{
public bool walkByDefault = false; // toggle for walking state
public bool lookInCameraDirection = true;// should the character be looking in the same direction that the camera is facing
private Vector3 lookPos; // The position that the character should be looking towards
private ThirdPersonCharacter character; // A reference to the ThirdPersonCharacter on the object
private Transform cam; // A reference to the main camera in the scenes transform
private Vector3 camForward; // The current forward direction of the camera
private Vector3 move;
private bool jump;// the world-relative desired move direction, calculated from the camForward and user input.
// Use this for initialization
private void Start()
{
// get the transform of the main camera
if (Camera.main != null)
{
cam = Camera.main.transform;
}
else
{
Debug.LogWarning(
"Warning: no main camera found. Third person character needs a Camera tagged \"MainCamera\", for camera-relative controls.");
// we use self-relative controls in this case, which probably isn't what the user wants, but hey, we warned them!
}
// get the third person character ( this should never be null due to require component )
character = GetComponent<ThirdPersonCharacter>();
}
void Update()
{
/*if(!jump)
jump = CrossPlatformInputManager.GetButtonDown("Jump");*/
}
// Fixed update is called in sync with physics
private void FixedUpdate()
{
// read inputs
bool crouch = false;
float h = CrossPlatformInputManager.GetAxis("Horizontal");
float v = CrossPlatformInputManager.GetAxis("Vertical");
crouch = Input.GetKey(KeyCode.C);
/*if (Input.GetMouseButton (0)) {
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, out hit, 1000)) {
move = new Vector3 (hit.point.x, hit.point.y, hit.point.z);
}
}*/
// calculate move direction to pass to character
if (cam != null)
{
// calculate camera relative direction to move:
camForward = Vector3.Scale(cam.forward, new Vector3(1, 0, 1)).normalized;
move = v*camForward + h*cam.right;
}
else
{
// we use world-relative directions in the case of no main camera
move = v*Vector3.forward + h*Vector3.right;
}
if (move.magnitude > 1) move.Normalize();
if !MOBILE_INPUT
// On non-mobile builds, walk/run speed is modified by a key press.
bool walkToggle = Input.GetKey(KeyCode.LeftShift);
// We select appropriate speed based on whether we're walking by default, and whether the walk/run toggle button is pressed:
float walkMultiplier = (walkByDefault ? walkToggle ? 1 : 0.5f : walkToggle ? 0.5f : 1);
move *= walkMultiplier;
endif
// calculate the head look target position
lookPos = lookInCameraDirection && cam != null
? transform.position + cam.forward*100
: transform.position + transform.forward*100;
// pass all parameters to the character control script
character.Move(move, crouch, jump, lookPos);
jump = false;
}
}
}