How to make my object go in the direction its facing [C#]

I have a basic movement and rotation code, but I want it to go in the direction that the cube is facing. Could someone help me?

using UnityEngine;
using System.Collections;

public class Player_Controll : MonoBehaviour
{
private CharacterController controller;

public float jumpForce;
public float moveForce;

private Vector3 Move_Vector;
private Vector3 LastMove;
private float gravity = 14f;
private float verticalvelocity;

private Transform CamTrans;

private void Start()
{
    controller = GetComponent<CharacterController>();
}

private void Update()
{
    CharacterControll();
}

private void CharacterControll()
{
    if (controller.isGrounded)
    {
        verticalvelocity = -1;

        if (Input.GetKeyDown(KeyCode.Space))
        {
            verticalvelocity = jumpForce;
        }
    }
    else
    {
        verticalvelocity -= gravity * Time.deltaTime;
        
    }

    Vector3 MoveVector = Vector3.zero;
    transform.Rotate(0, Input.GetAxis("Horizontal") * MoveSpeed * 50 * Time.deltaTime, 0);
    MoveVector.y = verticalvelocity;
    MoveVector.z = Input.GetAxis("Vertical") * moveForce;
    controller.Move(MoveVector * Time.deltaTime);
}

}

I haven't really used CharacterControllers, but can't you just rotate the transform? Look [Transform.Rotate][1] from documentation and then just use something like Input.GetAxis("Horizontal") as multiplier with Time.deltaTime and Vector. [1]: https://docs.unity3d.com/ScriptReference/Transform.Rotate.html

3 Answers

3

Doing

	Vector3 MoveVector = Vector3.zero;
	transform.Rotate(0, Input.GetAxis("Horizontal") * MoveSpeed * 50 * Time.deltaTime, 0);
	MoveVector.y = verticalvelocity;
	MoveVector.z = Input.GetAxis("Vertical") * moveForce * transform.forward.z;
	MoveVector.x = Input.GetAxis("Vertical") * moveForce * transform.forward.x;
	controller.Move(MoveVector * Time.deltaTime);
}``

seems to work.

When I go to plug that into my code, it doesn't work for some odd reason

The character is a ragdoll. I don't suppose that would change anything would it?

when working with physics (rigedBody) you should use the velocity, MoveTranslation and MoveRotation Method within the FixedUpdate instead of the “normale” transform.

True, but I am using the character control method.

Did you set the AudioSource from the editor? Show me your configuration of the editor, and how you set the floor tags, and all that stuff. Your question is not completed :/

I’m not sure how the CharacterController works but you could go back to basic and try to move the character with:

MoveVector.y = verticalvelocity;
float speed=Input.GetAxis("Vertical") * moveForce;
float radians=transform.rotation.eulerAngles.y * Mathf.Deg2Rad;
MoveVector.x = Mathf.Sin(radians) * speed;
MoveVector.z = Mathf.Cos(radians) * speed;
controller.Move(MoveVector * Time.deltaTime);

The Cos and Sin might need to get switched if it doesn’t work this way.

Kind regards,

Yorick

I thank you for your help, but it didn't work for some odd reason.

Yeah, I never thought the editor would have anything to do with it. The tag was used as a match to the hierarchy "Floor" is that incorrect use? [99957-error3.png|99957] [99958-error1.png|99958] Sorry for not being clear.