- Home /
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 from documentation and then just use something like Input.GetAxis("Horizontal") as multiplier with Time.deltaTime and Vector.
I have tried using the transform.Rotate, but for some odd reason, its not working.
what have you tried exactly? Show us there is multiple ways of doing this, ans tons on info on the question. If you did not manage to make it work, show us what you did exactly
Answer by Nyhtian · Dec 19, 2016 at 03:59 PM
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
Oh and just FYI you don't need curly braces on if and else statements if they're only one line.
So you can just do
if (controller.isGrounded)
{
verticalvelocity = -1;
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
verticalvelocity = jumpForce;
}
oh, hey, it does work! I don't know why it wasn't before, but now it is. Thank you for the help.
Answer by Sackstand · Dec 19, 2016 at 02:22 PM
when working with physics (rigedBody) you should use the velocity, MoveTranslation and MoveRotation Method within the FixedUpdate instead of the "normale" transform.
Answer by Cambesa · Dec 19, 2016 at 05:35 PM
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.