- Home /
Would anyone teach me how to create camera movement and look controls?
I need help creating a camera I try replicating what other YouTubers and game developers did for their camera movement but sadly, I didn't have any luck following their tutorials since it changes my movement script. They use add-force while I was using rb.velocity which had smoother movement. Can anyone teach me how to create camera movement and link my camera to my movement so when I look to the right w moves me forward based on the character instead of the world? I hope that made sense because I really don't know how to explain this issue I'm facing. I'm currently new to unity and I've researched quiet a bit about coding but I really cant find my way through this camera movement issue. Thanks and take care every one.
Answer by Llama_w_2Ls · Jul 23, 2020 at 02:55 PM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class MouseComponent : MonoBehaviour
{
public float MouseSensitivity = 500f;
public Transform PlayerBody;
float Xrotation = 0f;
//Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
public void Update()
{
float MouseX = Input.GetAxis("Mouse X") * MouseSensitivity * Time.deltaTime;
float MouseY = Input.GetAxis("Mouse Y") * MouseSensitivity * Time.deltaTime;
Xrotation -= MouseY;
Xrotation = Mathf.Clamp(Xrotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(Xrotation, 0f, 0f);
PlayerBody.Rotate(Vector3.up * MouseX);
}
}
From the Brackeys tutorial lol. Should help with what youre asking.
Answer by mzaidan1996 · Jul 23, 2020 at 02:57 PM
just tried it and it is not working. @Llama_w_2Ls
What exactly is not working. When you move your mouse, is the camera not moving with it?
You also need to assign the playerbody, which is the GameObject that your character controller is sitting under
I tried it but my camera isnt moving, I changed my mouse sensitivity and I put my player body but no movement showed and nothing happened.
Ok im using a new script this is it using UnityEngine;
public class CameraControl : $$anonymous$$onoBehaviour { //These are the variables that are associated with the camera looking speed. public float mouseSensitivity; public float mouseSmoothing;
private Vector2 currentlookingPos;
private Vector2 smoothedVelocity;
private GameObject player;
private void Start()
{
player = transform.parent.gameObject;
//This is to lock and make the cursor invisible when playing the game.
Cursor.lockState = CursorLock$$anonymous$$ode.Locked;
Cursor.visible = false;
}
private void Update()
{
$$anonymous$$ouseControl();
}
private void $$anonymous$$ouseControl()
{
Vector2 inputValues = new Vector2(Input.GetAxisRaw("$$anonymous$$ouse X"), Input.GetAxisRaw("$$anonymous$$ouse Y"));
float mouseX = Input.GetAxisRaw("$$anonymous$$ouse X") * mouseSensitivity * mouseSmoothing;
float mouseY = Input.GetAxisRaw("$$anonymous$$ouse Y") * mouseSensitivity * mouseSmoothing;
inputValues = Vector2.Scale(inputValues, new Vector2(mouseSensitivity * mouseSmoothing, mouseSensitivity * mouseSmoothing));
smoothedVelocity.x = $$anonymous$$athf.Lerp(smoothedVelocity.x, inputValues.x, 1f / mouseSmoothing);
smoothedVelocity.y = $$anonymous$$athf.Lerp(smoothedVelocity.y, inputValues.y, 1f / mouseSmoothing);
currentlookingPos += smoothedVelocity;
transform.localRotation = Quaternion.AngleAxis(-currentlookingPos.y, Vector3.right);
player.transform.localRotation = Quaternion.AngleAxis(currentlookingPos.x, player.transform.up);
currentlookingPos.y = $$anonymous$$athf.Clamp(currentlookingPos.y, -90f, 90f);
}
} but for some weird reason, when I look at a certain side and press w, it keeps taking me to another spot, for example, i spawn and hold W and it is moving me forward, when i look to the left, and i press w, it takes me to the right. my movement script uses rb.velocity = new vector3(x, rb.velocity.y, z) * walkspeed.
Thanks for the help @Llama_w_2Ls .
Your answer
Follow this Question
Related Questions
Jerky 3rd Person Camera Following Movement and Rotation 0 Answers
camera movments fixed.. character controller without using character controller -.-' 2 Answers
Rotate Camera to the object 0 Answers
How to have free rotation camera? 1 Answer
Free Orbit Cam and Mouse Aim Cam aren't working together 0 Answers