Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Oct 20, 2018 at 07:59 PM by evelynbb for the following reason:

Other

avatar image
0
Question by evelynbb · Oct 19, 2018 at 03:57 PM · unity 5scripting problem

(C#) how do i make a first-person controller script that just moves me on the X & Y axis?

hi! i am a complete novice when it comes to coding and only know the absolute basics. please bear this in mind while trying to help my clueless bum.

i've been trying to figure out how to get a first person controller working for a little physics game to learn things with, and while this script i've whipped up works fairly well, it has a problem. i'm trying to make a simple controller to move the character on the X and Z axis' while also having full camera control, like any normal FPS.

my current setup's camera controls work properly, and aside from that it adds force in the direction the camera is facing, but this leads to my character flying off into space if i look up or down. how do i go about clamping the movement range on this script so it strictly moves on the X and Y axis'? i feel like there's a quick and effective way to do it, but with my limited knowledge of the C# language i am unsure of how to find it. surely there's just a way to move horizontally?

my code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class camerafollowscript : MonoBehaviour {
 
     public Camera pubcam;
 
     public Rigidbody playerblock;
 
     public Vector3 offset;
 
     public float fwdForce = 5f;
     public float horiForce = 25f;
     public float revForce = 10f;
 
     Vector2 rotation = new Vector2(0, 0);
     public float speed = 3;
 
     // Use this for initialization
     void Start () {
 
     }
     
     // Update is called once per frame
     void Update () {
 
         rotation.y += Input.GetAxis ("Mouse X");
         rotation.x += -Input.GetAxis ("Mouse Y");
         transform.eulerAngles = (Vector2)rotation * speed;
         
         if (Input.GetKey(KeyCode.W))
         {
             playerblock.AddForce(pubcam.transform.forward * fwdForce);
         }
 
         if (Input.GetKey(KeyCode.S))
         {
             playerblock.AddForce(pubcam.transform.forward * -revForce);
         }
 
         if (Input.GetKey(KeyCode.A))
         {
             playerblock.AddForce(pubcam.transform.right * -horiForce);
         }
 
         if (Input.GetKey(KeyCode.D))
         {
             playerblock.AddForce(pubcam.transform.right * horiForce);
         }
         
     }
     
     private void LateUpdate()
     {
         pubcam.transform.position = playerblock.transform.position + offset;
     }
     
 }
 
Comment
Add comment
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

1 Reply

  • Sort: 
avatar image
0

Answer by TheEmeraldRuby · Oct 19, 2018 at 04:39 PM

A quick fix is to go to your player's rigidbody component, and under constraints tick the box for the Y axis under "freeze position". Of course, if there are slopes in your game you'll have to find a work around. You could watch this tutorial as it covers movement: https://www.youtube.com/watch?v=KfxVFjNPE3A I hope this helps!

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

Follow this Question

Answers Answers and Comments

245 People are following this question.

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

Related Questions

How to detect which how many of a type a variable a script has 1 Answer

cannot drag script to player.Guitext error,cannot drag player script to the player in hierarchy 2 Answers

Referencing a Cs Script from a JS within the Compile Order 0 Answers

Modal Panel Problem 1 Answer

Error trying to parse multiple IF statements of a KeyInput and Collider 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