input axis vertical is not set up (C#) in Unity - c#

I'm trying to implement a player movement script but whenever I run the project it just displays the error "ArgumentException: Input Axis Verticle is not setup. To change the input settings use: Edit -> Settings -> Input PlayerMovement.Update () (at Assets/PlayerMovement.cs:14)"
I tried going into input controls and 'setting up the verticle axis' but to be honest I have no clue how. Also, I'm not sure is it is supposed to be like this, but when I change the verticle axis in the input settings to the y-axis it automatically changes the horizontal-axis to 'y' as well. here is my code in case it is needed but it did not display any errors
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public CharacterController controller;
public float speed = 12f;
// Update is called once per frame
void Update()
{
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Verticle");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
}
}
also, I am using C# in Unity.
Thanks in advance!
B.G

float z = Input.GetAxis("Verticle");
change to
float z = Input.GetAxis("Vertical");
detailed instructions on changing input in future : https://docs.unity3d.com/Manual/class-InputManager.html

Related

ThirdPersonMovement script Unity

I was following this video: https://youtu.be/4HpC--2iowE?t=686
And after making the thirdpersonmovement script, I get an error that he did not get. Can anyone help me? There's probably a very small error that I cannot see, I must be blind. I followed the exact steps.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThirdPersonMovement : MonoBehaviour
{
public CharacterController controller;
public float speed = 6f;
// Update is called once per frame
void Update()
{
float horizontal = Input.GetAxisRaw("Horizontal");
float vetrical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
if (direction.magnitude >= 0.1f)
{
controller.Move(direction * speed * Time.deltaTime);
}
}
}
Error:
Assets\Scripts\ThirdPersonMovement.cs(16,57): error CS0103: The name 'Vertical' does not exist in the current context
Says error is on line 16. where "vector3 direction" is located
If the code is kinda wonky sorry, first time using this, was a weird set up for me.Thank you in advance.
There is a typo:
vetrical
vertical
t and r are swapped

How can I make players get close to each other on swipe left, and on swipe right they move to the sides?

So I wanna have my players have forward velocity at all times, and when I swipe left or use A key, they move close to each other with the rope between them, and when I swipe right they go away from each other to the same position they were previously.
Link to the video gameplay: https://streamable.com/45ahyc
My movement code right now:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float movementSpeed = 10f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
float hMovement = Input.GetAxis("Horizontal") * movementSpeed;
float vMovement = Input.GetAxis("Vertical") * movementSpeed / 2;
transform.Translate(new Vector3(hMovement, 0, vMovement) * Time.deltaTime);
}
}
You can use sign flag for this.
For example, you can add ìsRight boolean public property to your PlayerController and when you read your horizontal movement, you can multiply with -1 or 1.
float hMovement = Input.GetAxis("Horizontal") * movementSpeed * (isRight)?1:-1;
So, Your right character can move left and left character move right when you swipe left.
Because, they will close the center.
Also, you can use character's x axis sign for this job.
float hMovement = Input.GetAxis("Horizontal") * movementSpeed * Mathf.Sign(transform.position.x);
if your character in right side probably its x position is + and left characters's position sign is -.
You can get same results as above code.
Note In my second suggestion, I assume, you used 2 player character and center of screen is 0.

Unity 2d Rigidbody; moving with the same speed faster in Y axis than in X axis

For some reason the spaceshooter that i want to do, when i programmed the movement using Rigidbody2d.velocity, it moves much faster in Y axis (as in acceleration too) then in X axis, and i don't know why. I checked input.manager for gravity in both axis; both are equal. Checked physics 2d, set both to 0 even thought it wouldnt affect anything since my rigidbody is set to Kinematic... I'm currently lost.
Code of the movement is the following:
public class PlayerController : MonoBehaviour
{
public GameObject player;
float moveSpeed = 3f;
public float min_y, max_y;
public float min_x, max_x;
public Rigidbody2D rb;
private void Start()
{
class_selected = false;
player = transform.gameObject;
attack_timer = (float)characterStats.attackSpeed;
cattack_timer = attack_timer ;
rb = transform.Find("playerModel").gameObject.GetComponent<Rigidbody2D>();
}
void LateUpdate()
{
...
float yMov = Input.GetAxis("Vertical");
rb.velocity = new Vector2(rb.velocity.x, yMov) * moveSpeed;
print(rb.velocity.x);
float xMov = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(xMov, rb.velocity.y) * moveSpeed;
print(rb.velocity.y);
...
}
The rigidbody 2d is set to kinematic, gravity and sensitivity on both axis in input.manager are set to equal numbers... Tried debugging with print as you see, but in console they're printed equally the same... The project settings are most likely the default ones of a new empty 2d project in unity.
And if this isnt the correct way to move my character equally fast on both axis, which one is? Tried many videos but none work, and using transform movement wasnt recommended.
As #ken posted, using rb.velocity = new Vector2(xMov,yMov) *moveSpeed fixes this completely, thank you so much ken!!!

Questions arising from first-person controller code in Unity

I am creating a first person controller in Unity. Very basic stuff, camera is a child of Player capsule. Codes work, but I need help explaining what's going on.
*camera is child of Player in hierarchy
These are my questions:
In PlayerMovement, why are we translating in the Z-axis to achieve vertical movement when Unity is in Y-Up axis?
In CamRotation, I have no idea what is going on in Update(). Why are we applying horizontal movement to our Player, but then vertical movement to our Camera? Why is it not applied on the same GameObject?
What is mouseMove trying to achieve? Why do we use var?
I think we are getting a value of how much mouse has been moved, but what then does applying Vector2.Scale do to it?
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public float speed = 5.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
float mvX = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
float mvZ = Input.GetAxis("Vertical") * Time.deltaTime * speed;
transform.Translate(mvX, 0, mvZ);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CamRotation : MonoBehaviour {
public float horizontal_speed = 3.0F;
public float vertical_speed = 2.0F;
GameObject character; // refers to the parent object the camera is attached to (our Player capsule)
// initialization
void Start()
{
character = this.transform.parent.gameObject;
}
// Update is called once per frame
void Update()
{
var mouseMove = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
mouseMove = Vector2.Scale(mouseMove, new Vector2(horizontal_speed, vertical_speed));
character.transform.Rotate(0, mouseMove.x, 0); // to rotate our character horizontally
transform.Rotate(-mouseMove.y, 0, 0); // to rotate the camera vertically
}
}
XY is the plane for 2D Unity games. For 3D, you have Z-axis for height, and XY plane for positioning.
Note that different components from mouseMove are being applied (.x for character and .y for the camera). This means that the movement from the character is not equal to the movement from the camera; one should be faster/slower than the other.
var is a predefined C# keyword that lets the compiler figure out an appropriate type. In this case, it's the same as if you had written Vector2 as in Vector2 mouseMove = new Vector2(...);.
You are scaling the value from mouseMove, by multiplying its components by predefined values in your code. That's just it.
EDIT
You apply .x to your character because, as you commented after the line of code, you want to move it horizontally. As for the camera, .y is being applied because you want to move it vertically.
The negative value could be because the axis is inverted, so you make it negative so the camera has a natural movement. It's the same principle some games have in the setting, where they allow you to invert Y-axis.

Why does my directional light only rotate from 90º to -90º (Unity 5.5)

I have tried both of these C#scripts to rotate my directional light:
using System.Collections;
using UnityEngine;
public class LightRotator : MonoBehaviour
{
void Update ()
{
transform.rotation = Quaternion.Euler(transform.eulerAngles.x + 1.0f,
transform.eulerAngles.y,
transform.eulerAngles.z);
}
}
and
using System.Collections;
using UnityEngine;
public class LightRotator : MonoBehaviour
{
void Update ()
{
transform.localEulerAngles = new Vector3(transform.localEulerAngles.x + 1.0f,
transform.localEulerAngles.y,
transform.localEulerAngles.z);
}
}
They both seem to function exactly the same: If I change transform.eulerAngles.y to transform.eulerAngles.y + 0.5f, the light will rotate along the y-axis, and the same works for the z-axis. However, when I try to do this with the x-axis, it will rotate until it hits 90º, at which point it will continue to attempt rotation but it immediately and continuously shoved back to 90º. If I reverse the direction, it does the same thing at -90º. For example, the rotation might be: 88.5,89.0,89.5,90.0, 90.5, 89.93, 90.24, 89.4, etc.
What is causing this clamping and how do I fix it?
I think this is what you are looking for: http://answers.unity3d.com/questions/187073/rotation-locks-at-90-or-270-degrees.html
In order to fix your problem, you need to use an additional vector, change it inside Update every frame, and then pass it to the eulerAngles propriety of the transform.
Vector3 vect = Vector3.zero;
float rotationSpeed = 10f;
void Start () {
vect = transform.eulerAngles; //Set the vect rotation equal to the game object's one
}
void Update ()
{
vect.x += rotationSpeed * Time.deltaTime;
//Pass unique eulerAngles representation to the object without letting Unity change it
transform.eulerAngles = vect;
}
This happens btw because there're multiple euler angles representation of a single physical rotation in the 3D space, and when you work directly on the eulerAngles propriety of the transform, Unity makes some work behind the scenes, which can lead to a gimbal lock.
Use Quaternions. It's what Unity3d uses internally and doesn't have any of the side effects of euler angles.
using System.Collections;
using UnityEngine;
public class LightRotator : MonoBehaviour
{
public Vector3 RotationAxis = Vector3.right;
Quaternion _startRotation;
float _rotationIncrement = 0;
void Start()
{
_startRotation = transform.rotation;
}
void Update ()
{
Quaternion rotationMod =
Quaternion.AngleAxis(_rotationIncrement, RotationAxis);
_rotationIncrement += 1;
transform.rotation = _startRotation * rotationMod;
}
}
However, you probably want to use something like Quaternion.RotateTowards or Quaternion.Lerp along with Time.time and a rate. You will get much smoother results that way.
if you only want to rotate along the X-axis then set the other axis as 0.

Categories