Move Character in the way he is facing.,Make character move in way he is facing.
Hello, I have been strugling with this problem for a long time and I dont know the solution. So currently my character is moving in same directions e.g. Press W move up and when I press right he goes right but when i press W again he goes in that before same way before. So how can I make my character move/orient in the way he is facing? `
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveBrada : MonoBehaviour
{
private CharacterController characterController;
private Animator animator;
Vector3 moveDir = Vector3.zero;
[SerializeField]
private float moveSpeed = 100;
[SerializeField]
private float turnSpeed;
private void Awake()
{
characterController = GetComponent<CharacterController>();
animator = GetComponentInChildren<Animator>();
}
// Update is called once per frame
private void Update()
{
var horizontal = Input.GetAxis("Horizontal");
var vertical = Input.GetAxis("Vertical");
var movement = new Vector3(horizontal, 0, vertical);
characterController.SimpleMove(movement * Time.deltaTime * moveSpeed);
animator.SetFloat("Speed", movement.magnitude);
if (movement.magnitude > 0)
{
Quaternion newDirection = Quaternion.LookRotation(movement);
transform.rotation = Quaternion.Slerp(transform.rotation, newDirection, Time.deltaTime * turnSpeed);
}
}
}
,So I am new at Unity Engine and I started making a shooter game and I simply cant make my character move in way he is facing. With this code he is moving like straight: using W he goes straight in 1 way always. How to fix this making him go in way he is facing or backing up in opposite way he is facing and so.?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveBrada : MonoBehaviour
{
private CharacterController characterController;
private Animator animator;
Vector3 moveDir = Vector3.zero;
[SerializeField]
private float moveSpeed = 100;
[SerializeField]
private float turnSpeed;
private void Awake()
{
characterController = GetComponent<CharacterController>();
animator = GetComponentInChildren<Animator>();
}
// Update is called once per frame
private void Update()
{
var horizontal = Input.GetAxis("Horizontal");
var vertical = Input.GetAxis("Vertical");
var movement = new Vector3(horizontal, 0, vertical);
characterController.SimpleMove(movement * Time.deltaTime * moveSpeed);
animator.SetFloat("Speed", movement.magnitude);
if (movement.magnitude > 0)
{
Quaternion newDirection = Quaternion.LookRotation(movement);
transform.rotation = Quaternion.Slerp(transform.rotation, newDirection, Time.deltaTime * turnSpeed);
}
}
}
I sadly cannot see what your "Simple$$anonymous$$ove" - Function does, however, maybe
transform.forward
and
transform.right
can help you as these are the normalized vectors of the "forward" and "right" direction of your transform. You can use them to precicely move your character forward/backward and left/right. I tested it in a little scene by adding the following script to a little cube.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Simple$$anonymous$$ove : $$anonymous$$onoBehaviour
{
public float rotationSpeed = 30;
public float movementSpeed = 3;
void Update()
{
RotateWith$$anonymous$$eys();
$$anonymous$$ove();
}
void RotateWith$$anonymous$$eys()
{
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.Q))
{
transform.Rotate(Vector3.up, Time.deltaTime * -rotationSpeed);
}
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.E))
{
transform.Rotate(Vector3.up, Time.deltaTime * rotationSpeed);
}
}
void $$anonymous$$ove()
{
Vector2 input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
transform.Translate(Vector3.forward * input.y * Time.deltaTime * movementSpeed);
transform.Translate(Vector3.right * input.x * Time.deltaTime * movementSpeed);
}
}
Answer by tormentoarmagedoom · Jan 23, 2019 at 09:18 AM
Good day.
I think you are lookig for this:
Transfomr.translate()
This function moves in LOCAL coords (1,0,0) is always "ahead", because is using relative coords (oposite of global). In Unity you need always to think if using global coords or local coords.
Bye!
Your answer
Follow this Question
Related Questions
Player teleport not working 0 Answers
How to move gameObject along a Vector3 with OnMouseDrag()? 0 Answers
How to constrain first person player to a path? 0 Answers
Locking an objects movement on a desired axis, disregarding any external forces. 0 Answers
How to properly make an object follow the edge of another object? 0 Answers