Hello I am new at Unity and I am trying to make a simple 3D first person shooter. To do this I am trying to make a camera follow the mouse cursor. To do this I want to use set the localEularAngles according to the rotation generated by the mouse.
The following is the code of my CameraController script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
public float maximumY = 360f;
public float minimumY = -360f;
public float maximumX = 60f;
public float minimumX = -60f;
public float sensitivityX = 15f;
public float sensitivityY = 15f;
public Camera cam;
float rotationY;
float rotationX;
float offset;
// Start is called before the first frame update
void Start()
{
offset = cam.transform.position.y - transform.position.y;
}
// Update is called once per frame
void Update() {
rotationY += Input.GetAxis ("Mouse X") * sensitivityY;
rotationX += Input.GetAxis ("Mouse Y") * sensitivityX;
rotationX = Mathf.Clamp(rotationX, minimumX, maximumX);
transform.localEularAngles = new Vector3(0, rotationY, 0);
cam.transform.localEularAngles = new Vector3(-rotationX, rotationY, 0);
cam.transform.position = new Vector3(cam.transform.position.x,
cam.transform.position.y + offset, cam.transform.position.z);
}
}
The idea behind this is taken from the tutorial at this link: https://www.mvcode.com/lessons/first-person-camera-controls-jamie
However, when trying to compile I get the error: Assets\CameraController.cs(36,31): error CS1061: 'Transform' does not contain a definition for 'localEularAngles' and no accessible extension method 'localEularAngles' accepting a first argument of type 'Transform' could be found (are you missing a using directive or an assembly reference?)
The error occurs at both of the places that i use transform.localEularAngles. In fact, I have found no way to write transform.localEularAngles in a way that does compile, which has led me to believe that it might be deprecated, but it seems more likely that I am missing something fundamental.
Just a simple spelling mistake, should be localEulerAngles.
transform.localEulerAngles = new Vector3(0, rotationY, 0);
cam.transform.localEulerAngles = new Vector3(-rotationX, rotationY, 0);
cam.transform.position = new Vector3(cam.transform.position.x,
cam.transform.position.y + offset, cam.transform.position.z);
Related
I am trying to get a player movement script working using the one on Unity's website but it does not work. im getting the error CS1061.
"Assets\Scripts\PlayerMovement.cs(44,21): error CS1061: 'float' does not contain a definition for 'y' and no accessible extension method 'y' accepting a first argument of type 'float' could be found (are you missing a using directive or an assembly reference?)
"
I checked the internet but with my limited knowledge of unity i couldnt quite figure it out.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private CharacterController controller;
private Vector3 playerVelocity;
private bool groundedPlayer;
private float playerSpeed = 2.0f;
private float jumpHeight = 1.0f;
private float gravityValue = -9.81f;
// Start is called before the first frame update
void Start()
{
controller = gameObject.AddComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
groundedPlayer = controller.isGrounded;
if (groundedPlayer && playerVelocity.y < 0)
{
playerVelocity.y = 0f;
}
Vector3 move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
controller.Move(move * Time.deltaTime * playerSpeed);
if (move != Vector3.zero)
{
gameObject.transform.forward = move;
}
if (Input.GetButtonDown("Jump") && groundedPlayer)
{
playerSpeed.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
}
playerVelocity.y += gravityValue * Time.deltaTime;
controller.Move(playerVelocity * Time.deltaTime);
}
}
this is my code. If anyone can figure out what is wrong? Apparently the problem is something with Classes. But i have no idea what 'y' has to do with Classes.
playerSpeed.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
I believe this is supposed to be playerVelocity instead?
playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
error CS0029: Cannot implicitly convert type 'float' to 'UnityEngine.Quaternion
I dont know how to fix I've tried everything!!!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
public Transform player;
private Rigidbody rb;
// Start is called before the first frame update
void Start()
{
rb = this.GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
Vector3 direction = player.position - transform.position;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
rb.rotation = angle;
}
}
The error is because rb.rotation is of type Quaternion and angle is of type float which are not the same. You have a field for an angle which it has to rotate. The code below set's the rotation of rigid body in such a way that only it's rotation along X-axis is set.
void Update()
{
Vector3 direction = player.position - transform.position;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
float y = Quaternion.identity.eulerAngles.y;
float z = Quaternion.identity.eulerAngles.z;
rb.rotation = Quaternion.Euler(angle, y, z);
}
The correct answer is already mentioned, here is just a compact version of that
rb.rotation = Quaternion.Euler(Vector3.right * angle);
This is an optimal option for working on a single dimension, instead of all three.
P.S. use Vector3.up and Vector3.forward to change y and z too.
check static properties on https://docs.unity3d.com/ScriptReference/Vector3.html
I have a mouse look script which allows the player to look up and down in a 3D first person game. It works perfectly in the Unity Editor. Once I build the game, I can only move the mouse on x-axis. I have tried using the old and new input system, but the same result happens. I have tried setting the window mode in the player settings to fullscreen and windowed already as well as setting the input system to both.
Here is a video clip of this happening: https://www.youtube.com/watch?v=_AxX2OmkU_Y
public class MouseLook : MonoBehaviour
{
Transform playerTransform;
PlayerController playerController;
InputMaster input;
Vector2 lookDir;
public float sens = 500f;
private float xRotation;
// Start is called before the first frame update
void Start()
{
// Hide cursor
Cursor.lockState = CursorLockMode.Locked;
playerTransform = transform.parent;
playerController = playerTransform.GetComponent<PlayerController>();
input = playerController.Input;
}
// Update is called once per frame
void Update()
{
lookDir = input.Player.Look.ReadValue<Vector2>();
// Gets mouse inputs
float moveX = lookDir.x * sens * Time.deltaTime;
float moveY = lookDir.y * sens * Time.deltaTime;
// Clamps rotation
xRotation -= moveY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerTransform.Rotate(Vector3.up * moveX);
}
}
I would use Input.GetAxis("MouseX") and Input.GetAxis("MouseY")
Make sure not to use Input.GetAxisRaw because it won't work as intended with mouse inputs :)
Documentation Link
Here is a example from the above link for mouse looking:
using UnityEngine;
using System.Collections;
// Performs a mouse look.
public class ExampleClass : MonoBehaviour
{
float horizontalSpeed = 2.0f;
float verticalSpeed = 2.0f;
void Update()
{
// Get the mouse delta. This is not in the range -1...1
float h = horizontalSpeed * Input.GetAxis("Mouse X");
float v = verticalSpeed * Input.GetAxis("Mouse Y");
transform.Rotate(v, h, 0);
}
}
I am making a 2D game where you control a shield and you have to block falling obstacles from reaching base. I know that to move something you use rb.addforce. However, when I do the same for 2D it doesn't work. I imagine that instead of using a Vector3 you use a Vector2, but it gives me these 2 errors: Assets\Movement.cs(16,25): error CS1503: Argument 1: cannot convert from 'float' to 'UnityEngine.Vector2' and this one: Assets\Movement.cs(16,25): error CS1503: Argument 1: cannot convert from 'float' to 'UnityEngine.Vector2' for each time I write the line. Here is my full code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
public Rigidbody2D rb;
public Collider2D collider;
public float moveSpeed = 0.1f;
private void Update()
{
if (Input.GetKey("w"))
{
rb.AddForce(0f, moveSpeed);
Debug.Log("w");
}
if (Input.GetKey("s"))
{
rb.AddForce(0f, moveSpeed);
Debug.Log("s");
}
}
}
You can add a new Vector2 in rb.AddForce (new Vector2(0,moveSpeed));
Here is the updated code:
public class Movement : MonoBehaviour
{
public Rigidbody2D rb;
public Collider2D collider;
public float moveSpeed = 0.1f;
private void Update()
{
if (Input.GetKey("w"))
{
rb.AddForce(new Vector2(0f, moveSpeed));
Debug.Log("w");
}
if (Input.GetKey("s"))
{
rb.AddForce(new Vector2 (0f, moveSpeed));
Debug.Log("s");
}
}
}
Rigidbody2D.AddForce doesn't take two float parameters but rather as usual a Vector2 and a ForceMode2D where the latter has a default value of ForceMode2D.Force if you don't pass it in.
So your code should rather be
// = new Vector2 (0,1) * moveSpeed
// = new Vector2 (0, moveSpeed)
rb.AddForce(Vector2.up * moveSpeed);
...
// I assume in the second case you rather want to go down so
// = new Vector2 (0, -1) * moveSpeed
// = new Vector2 (0, -moveSpeed)
rb.AddForce(Vector2.down * moveSpeed);
Btw I would rather not Debug.Log every frame in Update it's pretty expensive.
I'm pretty new to unity and I decided to make a simple first person game, however my script only allows the player to look around, not move. I have used AddForce, however I am not sure I have done it correctly.
public float walkSpeedForward = 5f;
public float walkSpeedStrafe = 4f;
public float walkSpeedBack = 3f;
public float sprintMultiplier = 2f;
public Camera cam;
public float sensitivityX = 0f;
public float sensitivityY = 0f;
public float minimumX = -360F;
public float maximumX = 360F;
public float minimumY = -60F;
public float maximumY = 60F;
public Rigidbody rb;
float rotationY = 0f;
// Use this for initialization
void Awake () {
rb = GetComponent<Rigidbody>();
rb.freezeRotation = true;
}
// Update is called once per frame
void Update () {
float rotationX = cam.transform.localEulerAngles.y + Input.GetAxis ("Mouse X") * sensitivityX;
rotationY += Input.GetAxis ("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
cam.transform.localEulerAngles = (new Vector3(-rotationY, rotationX, 0f));
}
void FixedUpdate()
{
rb.AddForce (new Vector3 (Input.GetAxis ("Horizontal") * walkSpeedStrafe * Time.deltaTime, 0, Input.GetAxis ("Vertical") * walkSpeedForward * Time.deltaTime));
}
Do not use AddForce, it is physics pushing an object. Not actually Ideal to move a character.
You can learn how to properly make a script or even, use a ready script by clicking
Assets>Import Package> Characters. This package already have characters sounds and the Scripts that you're looking for. You can choose only the scripts if you want, but I suggest for you to import them all since you're trying to learn it. So you would know how to attach sounds, controls, models. As you learn form this package. FYI you don't need to download this, it is already on your computer.
Check this video Click Here
It will only take 12 min of your time.