0

I recently started on unity and I wanted to make simple movement which can be seen below.

using System.Collections.Generic;
using UnityEngine;

public class movement : MonoBehaviour
{
    //Variables
    float speed = 5.0f;
    public float turnSpeed = 4.0f;
    public float moveSpeed = 2.0f;
    public float minTurnAngle = -90.0f;
    public float maxTurnAngle = 90.0f;
    private float rotX;

    //Other
    Vector3 Char_velocity;
    Rigidbody Physics;

    void Start()
    {
        Physics = GetComponent<Rigidbody>();
    }

    void FixedUpdate()
    {
        //Get axis on which we want to move
        if (Input.GetButton("Vertical"))
        {
            //Creating vector which velocity is calculated based on vect3, speed and gets FPS compensation via fixed 
            //delta time
            Char_velocity = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
            Physics.MovePosition(transform.position + Char_velocity * speed * Time.fixedDeltaTime);
        }
        
        if (Input.GetButton("Horizontal"))
        {
            Char_velocity = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
            Physics.MovePosition(transform.position + Char_velocity * speed * Time.deltaTime);
        }

        float y = Input.GetAxis("Mouse X") * turnSpeed;
        rotX += Input.GetAxis("Mouse Y") * turnSpeed;
        rotX = Mathf.Clamp(rotX, minTurnAngle, maxTurnAngle);
        transform.eulerAngles = new Vector2(-rotX, transform.eulerAngles.y + y);
    }
}

Mouse movement has been the problem for me. I got it to somewhat work but I have 2 issues. The camera should move the whole body while rotating from side to side but instead rotates the body when moving along the Y axis.

JardexX
  • 3
  • 2
  • General note about [`eulerAngles`](https://docs.unity3d.com/ScriptReference/Transform-eulerAngles.html) `When you read the .eulerAngles property, Unity converts the Quaternion's internal representation of the rotation to Euler angles. Because, there is more than one way to represent any given rotation using Euler angles, the values you read back out may be quite different from the values you assigned. This can cause confusion if you are trying to gradually increment the values to produce animation.` – derHugo Feb 16 '23 at 14:07
  • Instead of `transform.eulerAngles.y + y` you should use a float field `rotY` like for the X axis and rather do `rotY += y;` and then `transform.eulerAngles = Quaternion.Euler(-rotX, rotY, 0);` .. further though since you use a `Rigidbody` you should rather use `MoveRotation` ... Your namings are extremely confusing btw .. `Physics` is a type itself in Unity and calling your `Rigidbody Physics` is not the best choice tbh ;) – derHugo Feb 16 '23 at 14:11
  • Thanks, I will try it! Also I know my names are confusing at times. Naming things can be sometimes harder than the coding itself. Will let you know when I finish testing it ;). – JardexX Feb 16 '23 at 14:18
  • About the naming.. what about `Rigidbody rigidbody`? ;) That would be pretty self explanatory – derHugo Feb 16 '23 at 14:20
  • So I think I missunderstood the [instructions](https://pastecode.io/s/ju6rnpx2) Also about the rigidbody, will rename now. I should keep things simple. – JardexX Feb 16 '23 at 14:33
  • Stupid me ^^ it should of course be `transform.rotation = Quaternion.Euler(...);` – derHugo Feb 16 '23 at 14:36
  • Happnes mate, its fine. Still dispite not giving me an error anymore it did not fix my issue. https://pastecode.io/s/bmd6xnrw – JardexX Feb 16 '23 at 14:44

0 Answers0