- Home /
Other
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;
}
}
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.
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
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
Follow this Question
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