I am extremely new to Unity and I’m losing my mind over this. I made a ball as a player to test movement and some camera stuff and I ended up wanting to try and make the camera act as if it was the player itself, with mouse movement inputs.
Only issue is, if the camera is the player’s child, the input part works perfectly, but when I move the player the camera just goes spinning, but if I detach it from the player, the horizontal movement stops working completely.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
public float MouseX;
public float MouseY;
public Transform Player;
public Transform Head;
public float Angle;
void Update()
{
MouseX = Input.GetAxis("Mouse X") * 200 * Time.deltaTime;
Player.Rotate(Vector3.up, MouseX);
MouseY = Input.GetAxis("Mouse Y") * 200 * Time.deltaTime;
Angle -= MouseY;
Angle = Mathf.Clamp(Angle, -90, 90);
Head.localRotation = Quaternion.Euler(Angle, 0, 0);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using TMPro;
public class Movement : MonoBehaviour
{
private Rigidbody rb;
private int count;
private float movementX;
private float movementY;
public float speed = 0;
public float jumpForce = 5f;
public TextMeshProUGUI countText;
private bool isGrounded = true;
public GameObject winTextObject;
void Start()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText();
winTextObject.SetActive(false);
}
void OnMove(InputValue movementValue)
{
Vector2 movementVector = movementValue.Get<Vector2>();
movementX = movementVector.x;
movementY = movementVector.y;
}
private void FixedUpdate()
{
Vector3 movement = new Vector3(movementX, 0.0f, movementY);
rb.AddForce(movement * speed);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("PickUp"))
{
other.gameObject.SetActive(false);
count = count + 1;
SetCountText();
}
}
void Update()
{
if (isGrounded && Keyboard.current.spaceKey.isPressed)
{
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
isGrounded = false;
}
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Ground"))
{
isGrounded = true;
}
}
void SetCountText()
{
countText.text = "Count: " + count.ToString();
if (count == 8)
{
winTextObject.SetActive(true);
}
}
}
If the player is a ball it's normal for the camera to spin when you place it as a child. As for the X movements can you do a Debug.Log(movementX) in the FixedUpdate and if the value stays at 0 when you move then could you share more about the OnMove function
– Cyber_Cats