[SOLVED] NoClip transform issues? (Or is it just my laptop?)

Hey guys, so I started a new project to test some no clipping stuff, and so far its working pretty well.

Currently this script is just attached to a cube in the scene. It works pretty well for not looking for much help so far.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NoClip : MonoBehaviour
{
    public float speed_normal = 5f;
    public float rotX;
    public float rotY;
    public float camSensitivity = 5f;

    void Update()
    {
        rotX += Input.GetAxis("Mouse X") * (camSensitivity * Time.deltaTime);
        rotY += Input.GetAxis("Mouse Y") * (camSensitivity * Time.deltaTime);
        rotY = Mathf.Clamp (rotY, -90, 90);

        transform.localRotation = Quaternion.AngleAxis(rotX, Vector3.up);
        transform.localRotation *= Quaternion.AngleAxis(rotY, Vector3.left);

        transform.position += transform.forward.normalized * speed_normal * Input.GetAxis ("Vertical") * Time.deltaTime;
        transform.position += transform.right.normalized * speed_normal * Input.GetAxis ("Horizontal") * Time.deltaTime;
    }




}

I am running into a few issues however that are very minor.

EDIT: First issue was a framerate issue with laptops performance. Just need assistance with the diagonal speed!

The second issue is diagonal speed. It seems to go faster while moving diagonal. I did however add “.normalized” to this part, but I cant tell due to laptops performance whether or not it actually helped or not.

transform.position += transform.forward.normalized * speed_normal * Input.GetAxis ("Vertical") * Time.deltaTime;
 transform.position += transform.right.normalized * speed_normal * Input.GetAxis ("Horizontal") * Time.deltaTime;

other than that, all is good and it works as expected.

Thanks for the help!

No point normalising Transform.forward and the like, it’s a unit vector anyway.

To fix the diagonal speed you want to normalise the entire input vector. Something like this.

Vector2 inputVector = new Vector2(Input.GetAxis("Horizontal", Input.GetAxis("Vertical");
inputVector = inputVector.ClampMagnitude(1);
2 Likes

As a cheeky extra tip, use:

transform.Translate(inputVector);

instead of modifying transform.position directly, it reads better :slight_smile:

1 Like

Thanks guys!

Implemented this into my code:

Vector2 inputVector = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
        inputVector = Vector2.ClampMagnitude(inputVector,1) * speed_normal * Time.deltaTime;

        transform.Translate (inputVector);

This works, however there is a slight problem that I cant seem to fix. Using W or S makes the cube go up and down instead of forward and back, UNLESS I look straight down and press W or S. A and D work fine moving left and right. Any reason behind why this change in code is causing this? Almost as if the TOP of the cube is being treated as its forward.

EDIT: Haha figured it out. Needed to use vec3 instead and put vertical axis into Z instead of Y. Thanks again guys

1 Like