I am trying to connect up to the new input system specifically with my Nintendo Switch Pro controller (wireless). At runtime the character only moves in a large circle. You can only slightly modify the movement with the controller. What am I doing wrong? I attached photos. Action Map
Action Map 2
public class PlayerController : MonoBehaviour {
public float moveSpeed = 5.0f;
public float rotationSpeed = 280.0f;
float horizontal;
float vertical;
// Update is called once per frame
private void Update()
{
Vector3 moveDirection = Vector3.forward * vertical + Vector3.right * horizontal;
Vector3 projectedCameraForward = Vector3.ProjectOnPlane(Camera.main.transform.forward, Vector3.up);
Quaternion rotationToCamera = Quaternion.LookRotation(projectedCameraForward, Vector3.up);
moveDirection = rotationToCamera * moveDirection;
Quaternion rotationToMoveDirection = Quaternion.LookRotation(moveDirection, Vector3.up);
transform.rotation = Quaternion.RotateTowards(transform.rotation, rotationToCamera, rotationSpeed * Time.deltaTime);
transform.rotation = Quaternion.RotateTowards(transform.rotation, rotationToMoveDirection, rotationSpeed * Time.deltaTime);
transform.position += moveDirection * moveSpeed * Time.deltaTime;
}
public void onMoveInput(float horizontal, float vertical)
{
this.horizontal = horizontal;
this.vertical = vertical;
Debug.Log($"Player Input: {vertical}, {horizontal}");
}
}
Hi there it seems that you have created the Action Maps, but you have not used them in your code. I believe to fix this you would need something more like this found on Unity documentation regarding Input Action: "Gamepad.current.leftStick.x.ReadValue();" wherever you have moveDirection on the right-hand side.
Related
I recently started on unity and I wanted to make simple movement which can be seen below.
using System.Collections.Generic;
using UnityEngine;
public class movement : MonoBehaviour
{
//Variables
float speed = 5.0f;
public float turnSpeed = 4.0f;
public float moveSpeed = 2.0f;
public float minTurnAngle = -90.0f;
public float maxTurnAngle = 90.0f;
private float rotX;
//Other
Vector3 Char_velocity;
Rigidbody Physics;
void Start()
{
Physics = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
//Get axis on which we want to move
if (Input.GetButton("Vertical"))
{
//Creating vector which velocity is calculated based on vect3, speed and gets FPS compensation via fixed
//delta time
Char_velocity = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
Physics.MovePosition(transform.position + Char_velocity * speed * Time.fixedDeltaTime);
}
if (Input.GetButton("Horizontal"))
{
Char_velocity = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
Physics.MovePosition(transform.position + Char_velocity * speed * Time.deltaTime);
}
float y = Input.GetAxis("Mouse X") * turnSpeed;
rotX += Input.GetAxis("Mouse Y") * turnSpeed;
rotX = Mathf.Clamp(rotX, minTurnAngle, maxTurnAngle);
transform.eulerAngles = new Vector2(-rotX, transform.eulerAngles.y + y);
}
}
Mouse movement has been the problem for me. I got it to somewhat work but I have 2 issues. The camera should move the whole body while rotating from side to side but instead rotates the body when moving along the Y axis.
I am trying to make a FPS game but when I press w for a bit and then release it slides a bit further than wanted. I am pretty new to Unity and think it has something to do with the updates.
Code:
public float moveSpeed = 5f;
public float jumpForce = 5f;
public CharacterController controller;
private Vector3 moveDirection;
public float gravityScale;
void Start()
{
controller = GetComponent<CharacterController>();
}
void Update()
{
float yStore = moveDirection.y;
moveDirection = (transform.forward * Input.GetAxis("Vertical")) + (transform.right * Input.GetAxis("Horizontal"));
moveDirection = moveDirection.normalized * moveSpeed;
moveDirection.y = yStore;
if (controller.isGrounded)
{
moveDirection.y = 0f;
if (Input.GetButtonDown("Jump"))
{
moveDirection.y = jumpForce;
}
}
moveDirection.y = moveDirection.y + (Physics.gravity.y * gravityScale);
controller.Move(moveDirection * Time.deltaTime);
}
add a physics material to your ground and set the dynamic friction to 1. Next, set the static friction from 0.5 to 1. And last but not least, set the bounciness to 0.
Play around with these values a bit until you find something that is satisfactory.
here is the link to the documentation:
https://docs.unity3d.com/Manual/class-PhysicMaterial.html
it basically explains what each property does.
I even set up public variables to see if was getting any input, and it isn't. I even copied and pasted the names directly from the project settings page.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class vehicle : MonoBehaviour
{
private Vector3 thrusterInput;
public float rotationSpeed = 10f;
public float rotSmoothSpeed = 10f;
private Quaternion smoothedRot = Quaternion.identity;
private Quaternion targetRot;
Body[] bodies;
public Rigidbody rigidbody;
public float thrustStrength = 10f;
// Start is called before the first frame update
void Start()
{
bodies = FindObjectsOfType<Body>();
}
// Update is called once per frame
void Update()
{
foreach(Body body in bodies)
{
float sqrDst = (body.transform.position - transform.position).sqrMagnitude;
Vector3 forceDir = (body.transform.position - transform.position).normalized;
Vector3 acceleration = forceDir * Universe.gravitationalConstant * body.rigid.mass / sqrDst;
rigidbody.AddForce(acceleration, ForceMode.Acceleration);
}
Vector3 thrustDir = transform.TransformVector(thrusterInput);
rigidbody.AddForce(thrustDir * thrustStrength, ForceMode.Acceleration);
rigidbody.MoveRotation(smoothedRot);
}
void HandleMovement()
{
float thrustInputX = Input.GetAxis("Horizontal");
float thrustInputY = Input.GetAxis("Ascent");
float thrustInputZ = Input.GetAxis("Vertical");
thrusterInput = new Vector3(thrustInputX, thrustInputY, thrustInputZ);
float yawInput = Input.GetAxis("Mouse X") * rotationSpeed;
float pitchInput = Input.GetAxis("Mouse Y") * rotationSpeed;
float rollInput = Input.GetAxis("Roll") * rotationSpeed;
Quaternion yaw = Quaternion.AngleAxis(yawInput, transform.up);
Quaternion pitch = Quaternion.AngleAxis(pitchInput, transform.right);
Quaternion roll = Quaternion.AngleAxis(rollInput, transform.forward);
targetRot = yaw * pitch * roll * targetRot;
smoothedRot = Quaternion.Slerp(transform.rotation, targetRot, Time.deltaTime * rotSmoothSpeed);
}
}
Sorry if the indents are all messed up, stackoverflow wasn't letting me use tab for some reason.
Your HandleMovement() is not called from anywhere. You need to put it in Update()
I have the basic movement and rotation working however I can not work out a way to limit the rotation up and down. I want to make it so that you cant look more than 90° up and down.
Ive tried multiple ways such as using if statments and using clamp.
using UnityEngine;
public class FPSController : MonoBehaviour {
public float speed = 5f;
public float sensitivity = 2f;
public GameObject Camera;
CharacterController controller;
float moveFB;
float moveLR;
public float rotX;
public float rotY;
void Start()
{
controller = GetComponent<CharacterController>();
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void FixedUpdate ()
{
moveFB = Input.GetAxis("Vertical");
moveLR = Input.GetAxis("Horizontal");
rotX = Input.GetAxis("Mouse X") * sensitivity;
rotY = Input.GetAxis("Mouse Y") * sensitivity;
transform.Rotate(0, rotX, 0);
Vector3 movement = new Vector3(moveLR * speed * Time.deltaTime, 0, moveFB * speed * Time.deltaTime);
controller.Move(transform.rotation * movement);
Camera.transform.Rotate(-rotY, 0, 0);
}
}
With this code you will be able to rotate the camera beyond 90 degrees causing it to be upside down etc
"Camera" is a built-in unity class, I would recommend renaming it to "camera".
Try this to clamp the camera's rotation:
(with your other public floats)
public float minAngle = -90;
public float maxAngle = 90;
(at the end of FixedUpdate)
Vector3 temp = camera.transform.localEulerAngles;
camera.transform.localEulerAngles = new Vector3(Mathf.Clamp(Mathf.DeltaAngle(0, temp.x), minAngle, maxAngle), temp.y, temp.z);
Edit: changed eulerAngles to localEulerAngles
Edit 2: changed the order of the arguements of Mathf.DeltaAngle
I fixed it here. Not to sure on how it works but it works. Credit to video:https://www.youtube.com/watch?v=F5eE1YL1ZJY
using UnityEngine;
public class FPSController : MonoBehaviour {
public float speed = 5f;
public float sensitivity = 2f;
public GameObject Camera;
CharacterController controller;
float moveFB;
float moveLR;
public float rotX;
public float rotY;
public float minAngle = -90f;
public float maxAngle = 90f;
void Start()
{
controller = GetComponent<CharacterController>();
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void FixedUpdate ()
{
moveFB = Input.GetAxis("Vertical");
moveLR = Input.GetAxis("Horizontal");
rotX = Input.GetAxis("Mouse X") * sensitivity;
rotY -= Input.GetAxis("Mouse Y") * sensitivity;
rotY = Mathf.Clamp(rotY, minAngle, maxAngle);
transform.Rotate(0, rotX, 0);
Vector3 movement = new Vector3(moveLR * speed * Time.deltaTime, 0, moveFB * speed * Time.deltaTime);
controller.Move(transform.rotation * movement);
Camera.transform.localRotation = Quaternion.Euler(rotY, 0, 0);
}
}
I'm having an issue where my gameObject barely jumps at all. I think it has something to do with moveDirection because the jumping works when I comment out p.velocity = moveDirection.
Any suggestions on how to fix this?
using UnityEngine;
using System.Collections;
public class Controller : MonoBehaviour
{
public float jumpHeight = 8f;
public Rigidbody p;
public float speed = 1;
public float runSpeed = 3;
public Vector3 moveDirection = Vector3.zero;
// Use this for initialization
void Start ()
{
p = GetComponent<Rigidbody>();
p.velocity = Vector3.zero;
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown (KeyCode.Space))
{
p.AddForce(new Vector3(0, jumpHeight, 0), ForceMode.Impulse);
}
Move ();
}
void Move ()
{
if(Input.GetKey(KeyCode.D))
{
transform.Rotate(Vector3.up, Mathf.Clamp(180f * Time.deltaTime, 0f, 360f));
}
if(Input.GetKey(KeyCode.A))
{
transform.Rotate(Vector3.up, -Mathf.Clamp(180f * Time.deltaTime, 0f, 360f));
}
moveDirection = new Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
if(Input.GetKey(KeyCode.LeftShift))
{
moveDirection *= runSpeed;
}
else
{
moveDirection *= speed;
}
p.velocity = moveDirection;
}
}
Try using a much higher value for your jumpheight variable. I usually go with something in the hundreds.
Because right after doing the AddForce(...) literally within the same frame you override the velocity with moveDirection. You should be adding to the current velocity, instead of overriding it entirely as such:
Vector3 velocity = p.velocity;
p.velocity = velocity + moveDirection;
This is exactly why Unity warns against messing with velocity directly, youd be better off just doing another AddForce(...) for your movement:
p.AddForce(moveDirection * Time.deltaTime);
EDIT:
i don't like digressing too far from the OP's question, but your new problems are probably because you are doing too much with moveDirection half of what i don't even understand why but it should for the most part look like this:
moveDirection = new Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical")).normalized;
float _speed = Input.GetKey(KeyCode.LeftShift) ? runspeed : speed;
p.AddForce(moveDirection * _speed * Time.deltaTime);
Okay I figured out how to fix it. Instead of those MoveDirection variables I changed it to
if(Input.GetKey(KeyCode.W)) {
transform.position += transform.forward * Time.deltaTime * speed;
}
if(Input.GetKey(KeyCode.S)) {
transform.position -= transform.forward * Time.deltaTime * speed;
}
and now it works just fine.