I'm new to Unity/C#, working on a script for PS4 controller, to get my character to be able to move in any directions using left stick and aim using right stick.
I used raycast to prevent my avatar from going through walls; that solved my issue of walking directly through walls.
However, when I aimed with right stick and moved in an opposite direction with my left, it still passed through the walls.
To tackle that, I adjusted the raycast parameter by adding the direction of the left stick (movement).
That worked, but it gave birth to a bug - I couldn't move backwards directly at all. Diagonal-backs were fine, but I simply cannot move backwards in an open space.
Here is my code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class NewMove : MonoBehaviour
{
Rigidbody m_Rigidbody;
public float m_Speed = 10f;
public float DashStamina = 100f;
void Start()
{
//Fetch the Rigidbody from the GameObject with this script attached
m_Rigidbody = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
// Left Stick Input - Movement
float HorizontalMovementInput = Input.GetAxis("LStickHorizontal");
float VerticalMovementInput = Input.GetAxis("LStickVertical");
// Right Stick Input - Aim
float RSHorizontalMovementInput = Input.GetAxis("RStickHorizontal");
float RSVerticalMovementInput = Input.GetAxis("RStickVertical");
Vector3 CharDirection = new Vector3(HorizontalMovementInput, 0, VerticalMovementInput);
if (Input.GetButton("SubMovement") && DashStamina > 1)
// Dash speed
{
Vector3 fwd = transform.TransformDirection(Vector3.forward);
if (Physics.Raycast(transform.position + CharDirection, fwd, 3) == false)
{
//Store user input as a movement vector
Vector3 m_Input = new Vector3(Input.GetAxis("LStickHorizontal"), 0, Input.GetAxis("LStickVertical"));
//Apply the movement vector to the current position, which is //multiplied by deltaTime and speed for a smooth MovePosition
m_Rigidbody.MovePosition(transform.position + m_Input * Time.fixedDeltaTime * (m_Speed * 2));
DashStamina -= 0.5f;
}
}
else
// Normal running speed
{
Vector3 fwd = transform.TransformDirection(Vector3.forward);
if (Physics.Raycast(transform.position + CharDirection, fwd, 3) == false)
{
Vector3 m_Input = new Vector3(Input.GetAxis("LStickHorizontal"), 0, Input.GetAxis("LStickVertical"));
m_Rigidbody.MovePosition(transform.position + m_Input * Time.fixedDeltaTime * m_Speed);
DashStamina += 0.05f;
}
}
//Faces Archer to last direction of left stick
transform.LookAt(transform.position + CharDirection);
//Faces Archer to aiming direction of right stick; will override above
Vector3 CharAim = new Vector3(RSHorizontalMovementInput, 0, RSVerticalMovementInput);
transform.LookAt(transform.position + CharAim);
DashStamina = Mathf.Clamp(DashStamina, 0, 100);
}
}
Please advise.