3d mouse aim camera 3rd person vertical C# - c#

I'm trying to make mouse aim camera that rotate my Player horizontally and vertically, look at Player and keep constant distance.
In that version it works fine but I can't make it horizontal also.
Each version camera freezes and I rotate my player itself or so.
I'm new to programming so I it's propably easy task with proper target.transform.eulerAngles.y assigning also to vertical but I cannot do it.
public class MouseAimCamera : MonoBehaviour {
public GameObject target;
public float rotateSpeed = 5;
Vector3 offset;
void Start() {
offset = target.transform.position - transform.position;
}
void LateUpdate() {
float horizontal = Input.GetAxis("Mouse X") * rotateSpeed;
target.transform.Rotate(0, horizontal, 0);
float desiredAngle = target.transform.eulerAngles.y;
Quaternion rotation = Quaternion.Euler(0, desiredAngle, 0);
transform.position = target.transform.position - (rotation * offset);
transform.LookAt(target.transform);
}
}
I'd be glad if any of you can help me.

This will rotate in world space, which will probably feel wrong at some angles since the camera is not always oriented straight up.
public class MouseAimCamera : MonoBehaviour {
public GameObject target;
public float rotateSpeed = 5;
void Start() {
transform.parent = target.transform;
transform.LookAt(target.transform);
}
void LateUpdate() {
float horizontal = Input.GetAxis("Mouse X") * rotateSpeed;
float vertical = Input.GetAxis("Mouse Y") * rotateSpeed;
target.transform.RotateAround(target.transform.position, Vector3.up, horizontal);
target.transform.RotateAround(target.transform.position, Vector3.left, vertical);
}
}
This will rotate in local space, and might feel more natural depending on what you're trying to build. This is close to your original solution, so i'm guessing it's not what you want.
public class MouseAimCamera : MonoBehaviour {
public GameObject target;
public float rotateSpeed = 5;
void Start() {
transform.parent = target.transform;
transform.LookAt(target.transform);
}
void LateUpdate() {
float horizontal = Input.GetAxis("Mouse X") * rotateSpeed;
float vertical = Input.GetAxis("Mouse Y") * rotateSpeed;
target.transform.Rotate(vertical, horizontal, 0);
}
}

Related

Rotate the turret and muzzle of the tank

It is necessary to rotate the turret and muzzle so that the sight is always directed to the center of the screen. I have a camera that can be rotated separately, and the turret and muzzle should follow it slowly, like in the world of tanks.
I have this code. The tower does not keep up with the camera and stops where I took the camera.
public class Tower : MonoBehaviour
{
public Transform Towr;
public Transform Cannon;
public float TowerSpeed;
public float CannonSpeed;
float TowerAngle;
float CannonAngle;
private void Update()
{
RotateTower();
RotateCannon();
}
void RotateTower()
{
TowerAngle += Input.GetAxis("Mouse X") * TowerSpeed * Time.deltaTime;
TowerAngle = Mathf.Clamp(TowerAngle, -90, 90);
Towr.localRotation = Quaternion.AngleAxis(TowerAngle, Vector3.up);
}
void RotateCannon()
{
CannonAngle += Input.GetAxis("Mouse Y") * CannonSpeed * -Time.deltaTime;
CannonAngle = Mathf.Clamp(CannonAngle, -2, 2);
Cannon.localRotation = Quaternion.AngleAxis(CannonAngle, Vector3.right);
}
}
I already found a solution how to rotate the tower behind the camera along the "Y" axis
public Transform cam;
public float speed = 50f;
private void FixedUpdate()
{
transform.localRotation = Quaternion.RotateTowards(transform.localRotation, Quaternion.Euler(0, cam.eulerAngles.y , 0), speed * Time.deltaTime);
}

How to make the player walk in the direction it is pointing?

I made a simple movement system in Unity 3D, but I don't know how to make it so that I move in the direction my player is pointing in.
using UnityEngine;
public class PlayerControler : MonoBehaviour
{
CharacterController characterController;
public float MovementSpeed = 1f;
public float Gravity = 9.8f;
private float velocity = 0f;
void Start()
{
characterController = GetComponent<CharacterController>();
}
void Update()
{
float horizontal = Input.GetAxis("Horizontal") * MovementSpeed;
float vertical = Input.GetAxis("Vertical") * MovementSpeed;
characterController.Move((Vector3.right * horizontal + Vector3.forward * vertical) * Time.deltaTime);
if (characterController.isGrounded)
{
velocity = 0;
}
else
{
velocity -= Gravity * Time.deltaTime;
characterController.Move(new Vector3(0, velocity, 0));
}
}
}
This is the player controller.
using UnityEngine;
public class MouseControl : MonoBehaviour
{
public float horizontalSpeed = 1f;
public float verticalSpeed = 1f;
private float xRotation = 0.0f;
private float yRotation = 0.0f;
private Camera cam;
void Start()
{
cam = Camera.main;
}
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * horizontalSpeed;
float mouseY = Input.GetAxis("Mouse Y") * verticalSpeed;
yRotation += mouseX;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
cam.transform.eulerAngles = new Vector3(xRotation, yRotation, 0.0f);
}
}
This is the code that makes my character face where my cursor is.
Edit: This is a First-Person 3D game. The player has a CharacterControler component on it, and the Main Camera is a child of the player. The second piece of code changes the direction that the camera is facing when the cursor is moved. The first script is the movement script, and utilises the CharacterController component of the player to move. I want to make to that instead of going in four static directions every time I press a movement key, I want the player to move in proportion to the direction that the camera is facing (on the X axis). E.g: If I am facing West and I press “W” to go forwards, I want the character to go West instead of North.
Instead of the global vectors Vector3.forward and Vector3.right in
characterController.Move((Vector3.right * horizontal + Vector3.forward * vertical) * Time.deltaTime);
rather use your local direction vectors Transform.forward and Transform.right
characterController.Move((transform.right * horizontal + transform.forward * vertical) * Time.deltaTime);
#derHugo was right, but I forgot to update the angles in the player movement script, so it always thought that I was rotated 0,0,0.
using UnityEngine;
public class PlayerControler : MonoBehaviour
{
Vector3 angles;
CharacterController characterController;
MouseControl mouseControl;
public float MovementSpeed = 1f;
public float Gravity = 9.8f;
private float velocity = 0f;
void Start()
{
characterController = GetComponent<CharacterController>();
mouseControl = GetComponent<MouseControl>();
}
void Update()
{
angles = new Vector3(mouseControl.xRotation, mouseControl.yRotation, 0f);
transform.eulerAngles = angles;
float horizontal = Input.GetAxis("Horizontal") * MovementSpeed;
float vertical = Input.GetAxis("Vertical") * MovementSpeed;
characterController.Move((transform.right * horizontal + transform.forward * vertical) * Time.deltaTime);
if (characterController.isGrounded)
{
velocity = 0;
}
else
{
velocity -= Gravity * Time.deltaTime;
characterController.Move(new Vector3(0, velocity, 0));
}
}
}
Updated code
Note: I had to made the X and Y rotation variables public.
[HideInInspector]public float xRotation = 0.0f;
[HideInInspector]public float yRotation = 0.0f;

Unity 3d movement not working with Camera direction

I have two scripts one is the MouseHandler and the other is the SimpleMovement. rotating the camera works and moving works however when the camera turns the movement doesn't go in that direction. E.G i turn the camera 90 degrees to the right but the forward doesn't change. The forward doesn't go to where the camera is facing. Sorry if i'm just being stupid. Any help would be appreciated
MouseHandler script:
public class MouseHandler : MonoBehaviour
{
// horizontal rotation speed
public float horizontalSpeed = 1f;
// vertical rotation speed
public float verticalSpeed = 1f;
private float xRotation = 0.0f;
private float yRotation = 0.0f;
private Camera cam;
void Start()
{
cam = Camera.main;
}
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * horizontalSpeed;
float mouseY = Input.GetAxis("Mouse Y") * verticalSpeed;
yRotation += mouseX;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90, 90);
cam.transform.eulerAngles = new Vector3(xRotation, yRotation, 0.0f);
}
}
SimpleMovement Script:
public class SimpleMovement : MonoBehaviour
{
CharacterController characterController;
public float MovementSpeed = 1;
public float Gravity = 9.8f;
private float velocity = 0;
private void Start()
{
characterController = GetComponent<CharacterController>();
}
void Update()
{
// player movement - forward, backward, left, right
float horizontal = Input.GetAxis("Horizontal") * MovementSpeed;
float vertical = Input.GetAxis("Vertical") * MovementSpeed;
characterController.Move((Vector3.right * horizontal + Vector3.forward * vertical) * Time.deltaTime);
// Gravity
if (characterController.isGrounded)
{
velocity = 0;
}
else
{
velocity -= Gravity * Time.deltaTime;
characterController.Move(new Vector3(0, velocity, 0));
}
}
}
First, get a reference to the main cmaera and cache it, because you're going to be referencing it frequently, and simply using Camera.main is a bit expensive:
private Camera mainCam;
...
private void Start()
{
characterController = GetComponent<CharacterController>();
mainCam = Camera.main;
}
Then, use mainCam.transform.right and mainCam.transform.forward but with the y set to 0 and normalized instead of Vector3.right and Vector3.forward. This will make the movement be based on the rotation of the camera:
float horizontal = Input.GetAxis("Horizontal") * MovementSpeed;
float vertical = Input.GetAxis("Vertical") * MovementSpeed;
Vector3 camRightFlat = new Vector3(mainCam.transform.right.x, 0f,
mainCam.transform.right.z).normalized;
Vector3 camForwardFlat = new Vector3(mainCam.transform.forward.x, 0f,
mainCam.transform.forward.z).normalized;
characterController.Move(
(camRightFlat * horizontal + camForwardFlat * vertical) * Time.deltaTime);
Altogether:
public class SimpleMovement : MonoBehaviour
{
CharacterController characterController;
public float MovementSpeed = 1;
public float Gravity = 9.8f;
private float velocity = 0;
private Camera mainCam;
private void Start()
{
characterController = GetComponent<CharacterController>();
mainCam = Camera.main;
}
void Update()
{
// player movement - forward, backward, left, right
float horizontal = Input.GetAxis("Horizontal") * MovementSpeed;
float vertical = Input.GetAxis("Vertical") * MovementSpeed;
Vector3 camRightFlat = new Vector3(mainCam.transform.right.x, 0f,
mainCam.transform.right.z).normalized;
Vector3 camForwardFlat = new Vector3(mainCam.transform.forward.x, 0f,
mainCam.transform.forward.z).normalized;
characterController.Move((camRightFlat * horizontal + camForwardFlat * vertical)
* Time.deltaTime);
// Gravity
if (characterController.isGrounded)
{
velocity = 0;
}
else
{
velocity -= Gravity * Time.deltaTime;
characterController.Move(new Vector3(0, velocity, 0));
}
}
}

First Person Controller camera rotation doesn't work

I'm looking for some c# script on unity to update first person controller
camera rotation transform. More specificly, I'm running an animation, and by the end of animation, I set "movie camera transform" on fps camera transform, how to show the code. For position variable, everything is ok. However, rotation variable doesn't work well.
The variable get the transform rotation right (the same movie camera transform), but I can't see the change on scene. The "first person controller rotation transform"
always get the last rotation coordinate that happened.
I already tested many rotation functions, like Rotate(Vector3), rotation, localRotation, eulerAngles, localEulerAngles....
Vector3 pos, roteuler;
public Camera fpscam, movie;
void getPosRot(){
roteuler = movie.transform.eulerAngles;
pos = movie.transform.position;
}
void Update(){
fps.transform.position = pos;
fps.transform.eulerAngles = roteuler;
}
Here is the camera part of my FPS script:
public class PlayerController : MonoBehaviour
{
float RotateX;
float RotateY;
public GameObject Camera;
public float RotationSpeed = 3.0f;
public float MaxYAxis = 60.0f;
public float MinYAxis = -48.0f;
private void Start()
{
}
void Update()
{
Rotation();
}
void Rotation()
{
RotateX += Input.GetAxis("Mouse X") * RotationSpeed;
RotateY += Input.GetAxis("Mouse Y") * RotationSpeed;
RotateY = Mathf.Clamp(RotateY, MinYAxis, MaxYAxis);
Camera.transform.localRotation = Quaternion.Euler(-RotateY, 0f, 0f);
transform.rotation = Quaternion.Euler(0f, RotateX, 0f);
}
}

Unknown rotation behaviour of gameObject while trying to rotate using LocalEulerAngles

I am new to game development , I am following a book Unity in action by Manning . This is the code snippet used to rotate a game object (from the book):
public RotationAxes axes = RotationAxes.MouseXandY;
public float sensitivityHorizontal = 9.0f;
public float sensitivityVert = 9.0f;
private float rotationX = 0;
public float minimumVert = -45.0f;
public float maximumVert = 45.0f;
// Update is called once per frame
void Update () {
float input = Input.GetAxis ("Mouse X");
//Vertical Rotation
if (axes == RotationAxes.MouseY) {
rotationX-= Input.GetAxis ("Mouse Y") * sensitivityVert;
rotationX = Mathf.Clamp (rotationX, minimumVert, maximumVert);
float rotationY = transform.localEulerAngles.y;
transform.localEulerAngles = new Vector3 (rotationX, rotationY, 0);
}
}
This code actually works but the other way around i.e when my move my mouse upwards the object rotates down and up when I move down . I figured out that it was rotationX-= Input.GetAxis ("Mouse Y") * sensitivityVert; this that was causing the problem so I change it to rotationX+= Input.GetAxis ("Mouse Y") * sensitivityVert; and now it rotates really strangely ! I have made sure that it only rotates in vertical axis but it's rotating in all the directions i.e even in horizontal and z axis.
Can somebody please tell me where I am going wrong? ThankYou
Works fine for me. I cleaned up your code and constrained Y to its initial value.
public class rotateX : MonoBehaviour
{
public float sensitivityVert = 9.0f;
private float rotationX = 0;
public float minimumVert = -45.0f;
public float maximumVert = 45.0f;
Vector3 initial;
void Start()
{
initial = transform.localEulerAngles;
}
// OnMouseDown is called when the user has pressed the
// mouse button while over the GUIElement or Collider.
protected void OnMouseDown()
{
sign = -sign;
}
float sign = -1f;
void Update()
{
rotationX += sign * Input.GetAxis("Mouse Y") * sensitivityVert;
//rotationX = Mathf.Clamp(rotationX, minimumVert, maximumVert);
transform.localEulerAngles = new Vector3(rotationX, initial.y, 0f);
}
}
The cube rotates up and down around its pivot.

Categories