'I need to limit mouse follow in Unity2d
i'm developing a little 2d sidescroller game, and i was wondering how could i stop my character from looking straight up in the air and directly onto the ground, so basically having a field of view of 45 degrees on both sides. Any advice?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement_script : MonoBehaviour
{
private Rigidbody2D body;
[SerializeField] private float speed;
private Camera theCam;
private void Awake()
{
body = GetComponent<Rigidbody2D>();
}
private void Start()
{
theCam = Camera.main;
}
private void Update()
{
body.velocity = new Vector2(Input.GetAxis("Horizontal") * speed, body.velocity.y);
if (Input.GetKey(KeyCode.Space))
{
body.velocity = new Vector2(body.velocity.x, speed);
}
Vector3 mouse = Input.mousePosition;
Vector3 screenPoint = Camera.main.WorldToScreenPoint(transform.localPosition);
Vector2 offset = new Vector2(mouse.x - screenPoint.x, mouse.y - screenPoint.y);
float angle = Mathf.Atan2(offset.y, offset.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, angle);
}
}
so this is the script for the player movement,thanks in advance.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
