Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ploppy · Jan 26, 2015 at 09:50 AM · javascriptunity 4.6

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

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sniper43 · Jan 26, 2015 at 10:48 AM 0
Share

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.

avatar image Ghost Storeys · Jan 26, 2015 at 10:48 PM -2
Share

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.

avatar image meat5000 ♦ Ghost Storeys · Jan 26, 2015 at 10:48 PM 0
Share

Who are you talking to??

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ploppy · Jan 26, 2015 at 02:45 PM 0
Share

@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

avatar image nightowl79a · Jan 26, 2015 at 08:45 PM 0
Share

You mean like pathfinding? So you can click on the ground or an object and move to that position?

avatar image
0

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);
         }
     }
 
 }



Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ploppy · Jan 26, 2015 at 07:04 PM 0
Share

@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

avatar image SnStarr · Jan 26, 2015 at 07:11 PM 0
Share

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.

avatar image ploppy · Jan 27, 2015 at 10:51 AM 0
Share

@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

avatar image SnStarr · Jan 27, 2015 at 05:18 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

gameobject.SetActive(false) issue in Stand-Alone build 1 Answer

How to send a raycast to an object to give me ammo? 0 Answers

Simple car script not work on unity 5? 1 Answer

input.getkey and loadlevel won't run 1 Answer

object is not a member of 'UnityEngine.Component'. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges