- Would it be possible to do this? I’ve referred to these resources without any luck:
Question on a similar topic (I admit that for this one, I could look a bit more closely as to how to do this, but I’m not sure if I clearly understand what I’d need to do with the best answer - plus, the answers there don’t appear to refer to the NavMeshAgent, which I used from some other resources)
- Unity3D livestream
- Another video
Currently, I have a Capsule standing on a plane without a Rigidbody, and with a SmoothMouseLook script attached to the Main Camera (to which I have made it a child of the Capsule). I have baked a NavMesh. I am aiming to move the Capsule to no avail.
Judging from this, what I believe I need is a ray going from the Capsule to the point clicked in the camera, but I’m not sure how to do this.
Based on this code, I have it working, but I’m wondering how I could keep my camera still:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI; // for NavMeshAgent to work
public class PlayerMovement : MonoBehaviour {
private Animator anim;
private NavMeshAgent navMeshAgent;
private Vector3 position;
public static Vector3 cursorPosition;
public CharacterController controller;
public float speed;
private Vector3 newPosition;
// shooting and animation variables - ignore
public float shootDistance = 10f;
public float shootRate = .5f;
private Transform targetedEnemy; // coordinates shoot enemy
private Ray shootRay;
private RaycastHit shootHit; // info about what hit
private bool walking; // walk animation playing
private bool enemyClicked;
private float nextFire;
// for setup - do before "start"
void Awake() {
// get components and apply to field
navMeshAgent = GetComponent<NavMeshAgent>();
}
// Update is called once per frame
void Update() {
locateCursor();
// cast ray from mouse position from camera to scene
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit; // more than boolean
// https://answers.unity.com/questions/885341/fps-using-mouse-to-move-rather-than-keys.html
if (Input.GetMouseButtonDown(0)) // https://www.youtube.com/watch?v=GANwdCKoimU
{
//Locate where the player clicked on the terrain
locatePosition();
} else
{
}
// if (navMeshAgent.remainingDistance <= navMeshAgent.stoppingDistance)
moveToPosition();
}
void locatePosition()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 1000))
{
if (hit.collider.tag != "Player" && hit.collider.tag != "Enemy")
{
navMeshAgent.destination = hit.point;
}
}
}
void locateCursor()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 1000))
{
cursorPosition = hit.point;
}
}
void moveToPosition()
{
//Game Object is moving
if (Vector3.Distance(transform.position, position) > 1)
{
Quaternion newRotation = Quaternion.LookRotation(position - transform.position, Vector3.forward);
newRotation.x = 0f;
newRotation.z = 0f;
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 10);
// controller.SimpleMove(transform.forward * speed);
// transform.position = Vector3.Lerp(transform.position, newPosition, 0.5f);
}
//Game Object is not moving
else
{
}
}
}
In this video I made, I did not move my mouse, yet the game object is rotating against my intentions.
Hi @mmonis2014 - did you ever figure out how to get this to work? I would like to do the same thing.