- Home /
FPS using mouse to move rather than keys
Hi I am developing a First person game but would like to use the mouse to move on a click to move as opposed to the keys. I also need to disable Y movement and restrict the x movement to 180. I dabbled with the standard unity FPS but is seems very jagged on movement and from what I can see being a new user, their doesn't seem to be a way to restrict y movement. Can someone help with this or suggest a good tutorial on this type of movement. Thanks
This is a bit too vague.
A suggestion is to moev the object like it was a regular 3D object, and have the camera be a child of this object.
I don't think you understood his question. He just wants the "steering" of the character to be dictated by the position of the mouse X.
Answer by sniper43 · Jan 27, 2015 at 12:45 PM
If your problem is jagged movement might be the only thing you need is to lerp the movement.
Here's what it should look like:
using UnityEngine; using System.Collections;
public class adsasd : MonoBehaviour {
private Vector3 newPosition;
void Update () {
Vector3 oldPosition = transform.position;
//TODO
//if clicked change newPosition instead of transform
transform.position = Vector3.Lerp( oldPosition, newPosition, 0.5f);
}
}
What this snippet does is smooth out the movement from A to B by the interpolator of 0.5f. You can modify the interpolator value as you like, but preferably keep it between 0f and 1f.
Answer by nightowl79a · Jan 26, 2015 at 11:17 AM
Make a capsule, add a Camera as a child of the capsule and set it so the camera is looking in the same forward direction as the capsule. Import the MouseLook script from the standard assets. Attach a mouse look script to the capsule and change the "Axis" variable to X only. Add another mouse look script onto the camera and change the "Axis" variable to Y only(you can also restrict the amount with the Minimum Y and Maximum Y values). Make this script and add it to the capsule:
public class MoveForward : MonoBehaviour {
public float speed;
void Update ()
{
if (Input.GetMouseButton(0))
{
transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
}
}
This is a very basic way of doing it. This script will make the capsule move forward when the left mouse button is held down.
@nightowl79a Thanks for that. However, I am developing a hidden object point & click so I need move to be where ever the mouse is clicked. For example, in your code, I can only move forward. How do I adapt to go to where the mouse was clicked? Thanks
You mean like pathfinding? So you can click on the ground or an object and move to that position?
Answer by SnStarr · Jan 26, 2015 at 02:53 PM
Try this
using UnityEngine;
using System.Collections;
public class ClickToMove : MonoBehaviour
{
public float speed;
public CharacterController controller;
private Vector3 position;
public AnimationClip run;
public AnimationClip idle;
public static bool attack;
public static bool die;
public static Vector3 cursorPosition;
public static bool busy;
// Use this for initialization
void Start ()
{
position = transform.position;
busy = false;
}
// Update is called once per frame
void Update ()
{
if(!busy)
{
locateCursor();
if(!attack&&!die)
{
if(Input.GetMouseButton(0))
{
//Locate where the player clicked on the terrain
locatePosition();
}
//Move the player to the position
moveToPosition();
}
else
{
}
}
}
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")
{
position = 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);
animation.CrossFade(run.name);
}
//Game Object is not moving
else
{
animation.CrossFade(idle.name);
}
}
}
@SnStarr many thanks but when i click all it does is rotate. Does not move to where I clicked. I have tried it with animations of run and idle, but still the same. Thanks
Your player must have a character controller component and no rigid body. Your player must not be overlapped with terrain. Your player must not have its X, y, or Z axis' clamped. It works. Just tested it. No problems at all.
@SnStarr Sorry to be a pain, but I am having no luck with this. This is what I have so far. FirstPersonController and assigned to that, I have your script and a character controller. When I click in the scene it seems to float and when I click left or right it seems very quick and seems to stutter. Just a nOOb question, why do I need animation with a FPC? Thanks
No, you dont need first person controller at all. the script i gave you is a complete click to move controller script. Adding it to the FPC just bugs out the controller as you mentioned above. Just make a blank script, add my code, attach it to your player without the FPC enabled.