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 Feb 13, 2020 at 08:27 PM by ZachRoman121 for the following reason:

Other

avatar image
1
Question by ZachRoman121 · Jan 17, 2017 at 12:34 AM · vector3rotation axismouse look

Move an object forward. But not up

I made a script that makes a camera move. And you control the rotation with your mouse. But the problem is. When looking up. And moving forward. You fly. I can't figure out how to do this. Here's my code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerMoving : MonoBehaviour {
     float mouseInputY;
     float mouseInputX;
     Vector3 lookHere;
     public float WalkSpeed;
     public float sensitivity;
     public Transform Player;
     void Update () {
         if (Input.GetKey (KeyCode.LeftShift)) {
             WalkSpeed = 0.5f;
         }
         Cursor.lockState = CursorLockMode.Locked;
         mouseInputY = -Input.GetAxis ("Mouse Y") * sensitivity;
         mouseInputX = Input.GetAxis ("Mouse X") * sensitivity;
 lookHere = new Vector3(mouseInputY, mouseInputX,0);
     Player.transform.Rotate(lookHere);
         float z = Player.transform.eulerAngles.z;
             Player.transform.Rotate(0, 0, -z);
         // Walking
         if (Input.GetKey ("w")) {
             Player.transform.Translate (Vector3.forward * WalkSpeed);
         }
         if (Input.GetKey ("s")) {
             Player.transform.Translate (Vector3.back * WalkSpeed);
         }
         WalkSpeed = 0.1f;
     }
 }
 

Comment
Add comment · Show 2
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 Dibbie · Jan 17, 2017 at 03:19 AM 0
Share

Your problem, is mostly around gravity over the method you are using, though personally I would try to use RigidBody velocity movement ins$$anonymous$$d of Translate or Position.

Translate and Position both move an object by world space units, so, if you dont have any or many colliders that the player/camera should be stopped by with physics (for example, a wall or something), this is probably okay -- I would normally try to move objects with the RigidBody on them, to be safe with movement, and it also avoids that issue, because of the gravity thats applied to a RigidBody.

With that said, the best fix, is to add a RigidBody to your character, and possibly constrain the rotation (if you find it doing weird things), then your character will fall back to the ground if they begin to fly anyway.

Another sure-fire way you may want to do, is use a "isGrounded" bool, that only allows the character to move forward if they are "grounded", which you can check with a OnCollisionExit or OnTriggerExit, if they leave the ground.

avatar image James2Games · Jan 17, 2017 at 04:46 AM 0
Share

lookHere = new Vector3(mouseInputY, mouseInputX,0); The position that you are getting is currently in Screen Space. You will need to convert this to world space or get the position in the world that the mouse is on before using it in-game

If you are using an orthogonal camera or a top-down 2d game this can help

Vector2 clickLocation = Camera.main.ScreenToWorldPoint(Input.mousePosition);

Otherwise this can help

http://answers.unity3d.com/questions/376735/get-world-coordinates-from-mouse-click.html

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by bburtson09 · Jan 17, 2017 at 08:15 AM

Try Rigidbody.MovePosition. instead of translate. And then in the inspector constrain the Y position.

Quick note- try to separate functionality in to different methods/classes with their own responsibilities. Additionally, unity documentation recommends that you handle physics type behaviors in FixedUpdate ()
and don't forget to calculate by the time. Delta time where necessary. documentation RigidBody.MovePosition. here : https://docs.unity3d.com/ScriptReference/Rigidbody.MovePosition.html

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

72 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

Related Questions

Rotate to face the bottom of a unit towards a planet 0 Answers

How to Rotate Plane of Cube Around its Center? (Rotate Vectors Around Point/Axis/Direction) 1 Answer

How can I rotate a vector direction around an arbitrary axis? 0 Answers

Get angle around a specified axis. 1 Answer

Vector3 - Rotate a cube by its spatial (longer) diagonal 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