Question by
Pyrem · Jul 07, 2018 at 07:56 PM ·
rigidbodycontrollermovement scriptjoystickcharacter controller
I can't seem to get this movement + automatic lookat the direction of movement work for a 2.5D game
Good Day!
I need help making this script.
1) Movement (which works currently) 2) Rotation (LookAt) as per the movement direction automatically 3) Use animations attached to the character.
After looking here and there, could not find how to create this specific script for these sets of requirements.
Please help!
GitHub Link
Thanks.
using CnControls;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
[SerializeField] private float defaultSpeed = 5f;
[SerializeField] private float infectedSpeed = 7f;
[SerializeField] private float humanSpeed1 = 6f;
[SerializeField] private float humanSpeed2 = 7f;
[SerializeField] private float humanSpeed3 = 8f;
[SerializeField] private float sensitivity = 4f;
private PlayerMotor motor;
private Vector3 _movVelocity = Vector3.zero;
private Rigidbody rb;
// Use this for initialization
private void Start()
{
rb = GetComponent<Rigidbody>();
}
private void FixedUpdate()
{
PerformMovement();
}
private void Update()
{
CharacterController controller = GetComponent<CharacterController>();
// Axis are inverted due to orientation
// Define the axis as per the joystick input
var _xMov = CnInputManager.GetAxis("Vertical");
var _zMov = CnInputManager.GetAxis("Horizontal");
// Debug.Log("X = " + _xMov + " || " + "Z = " + _zMov);
// Set position values as per the joystick input
var _movHorizontal = transform.forward * _xMov;
var _movVertical = transform.right * _zMov;
// Sets speed as per the tag
defaultSpeed = tag.Contains("Zombi") ? infectedSpeed : humanSpeed1;
// Sets current speed
var _velocity = (_movHorizontal + _movVertical).normalized * defaultSpeed;
// Moves the character using the velocity set
Move(_velocity);
}
private void Move(Vector3 _velocity)
{
// Set the zero velocity to the active velocity
_movVelocity = _velocity;
}
private void PerformMovement()
{
// Move when the velocity is not zero as per the PlayerController
if (_movVelocity != Vector3.zero)
rb.MovePosition(rb.position + _movVelocity * Time.fixedDeltaTime);
}
}
Comment