Creating a simple character controller (Script provided)
I want a very simple character controller, a character that moves around in all directions and faces the direction he's moving in. But I'm confused about how to achieve that, I find vector3 stuff confusing in general. How would I make this code use force and clamp the speed of the character's movement and have him keep on facing the direction he's moving in. Right now after he stops moving the rotation is reset.
public float speed;
bool canPlayerMove = true;
void Start()
{
}
void FixedUpdate()
{
float moveHorizontal = -Input.GetAxisRaw("LeftJoyStickHorizontal");
float moveVertical = Input.GetAxisRaw("LeftJoyStickVertical");
if (canPlayerMove)
{
Vector3 movement = new Vector3(moveVertical, 0.0f, moveHorizontal);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movement), 0.15F);
transform.Translate(movement * speed * Time.deltaTime, Space.World);
}
}
Good day.
If you are "noob" with this issues, there are much more simple ways to achieve the simple movement you want.
Look for examples, will see is much more simple.
PD: there is a component called PlayerController, read some api and manuals too:D
BYE !
It's only as simple as how well it's explained. All of this is simple if you understand it, in my script above everything works except the rotation is reset when you stop moving. It's also moving around the transform which is not ideal. I have looked around and there seems to be no tutorial on how to make a character move and face the direction he's moving in, crazy. It's all either mouse controlled third person controllers or tank movement or something similar. The third person script that comes with unity works but it's also very messy and the comments inside the script itself don't explain much. BYE!
Answer by djordr · May 07, 2018 at 08:19 PM
Hello buddy, this doesn't work with forces but fixes you current code. Someone else would have to tell you about how to change this to use forces.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class scr : MonoBehaviour {
bool canPlayerMove = true;
public float speed = 0.0f;
void FixedUpdate()
{
float moveHorizontal = -Input.GetAxisRaw("LeftJoyStickHorizontal");
float moveVertical = Input.GetAxisRaw("LeftJoyStickVertical");
if (canPlayerMove)
{
Vector3 movement = new Vector3(moveVertical, 0.0f, moveHorizontal);
if (movement != Vector3.zero)
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movement), 0.15F);
}
transform.Translate(movement * speed * Time.deltaTime, Space.World);
}
}
}
That is beautiful, now only if someone could explain how to add a force to this ins$$anonymous$$d of using the transform.position. Because it would be nice to have the character walk if you move the joystick just a little bit and run if you move him faster. Would it be as simple as to just change the last line of the script, I'm not sure.
Your answer
Follow this Question
Related Questions
change angle towards direction 1 Answer
Rotation resets on character controller 0 Answers
Object to walk forward, turn 180 and walk back 1 Answer
How do I rotate my object back to the world's up direction whilst keeping it's forward direction? 0 Answers
How to rotate an object around a player when pushing a button..? 0 Answers