- Home /
how to determine which part of a vector3 identifies an object.
hello, I am working on re-designing a wall running script. the script is a Frankenstein's monster of both unity's Character Controller script, and my own scripting understanding.
my biggest dilemma is that while I have the player running and all that, I don't know how to separate the vector described below into left and rights. I'm doing this because that way I can use the vector to determine which side the wall is compared to the player.
directions = new Vector3[]{ Vector3.right, Vector3.right + Vector3.forward, Vector3.forward, Vector3.left + Vector3.forward, Vector3.left };
Tl;Dr: I need help determining whether the wall is on the left or right side of the player. I know how to solve several other issues; however, I just want to get this bigger issue out of the way first.
here is the script I am using at the moment. the script works alright if you're looking for a character controller-based movement script for anyone wanting to know. it sticks the player against a wall automatically, letting them slide against it.
public class WallBuildup : MonoBehaviour
{
public float normalizedAngleThreshold = 0.1f;
public CharacterController controller;
//the player character
public Transform Player;
public Transform cameraRot;
//attaching to wall
public float LengthFromWall = 1.5f;
private Vector3 lastWallNormal;
//wallrun stats
public float wallSpeedMultiplier = 5f;
public bool wallOnLeft;
//camera stuff
public float maxAngleRoll = 20;
[Range(0.0f, 1.0f)]
public float cameraTransitionDuration = 1;
//jump offs
public float jumpDuration = 1;
public float wallBouncing = 3;
public AudioClip jumpNoise;
AudioSource AudioSource;
private readonly float VolumeScale = 0.7f;
PlayerMovementScript PlayerMovementScript;
Vector3[] directions;
bool IsPlayerGrounded() => PlayerMovementScript.isGrounded;
public bool canWallrun;
public bool useSprint;
public bool canAttach;
private RaycastHit[] hits;
public bool isWallRunning;
// Start is called before the first frame update
void Start()
{
AudioSource = GetComponent<AudioSource>();
PlayerMovementScript = GetComponent<PlayerMovementScript>();
directions = new Vector3[]{
Vector3.right,
Vector3.right + Vector3.forward,
Vector3.forward,
Vector3.left + Vector3.forward,
Vector3.left
};
}
//this function determines wether or not a runnable wall is within range.
public void AttachCheck()
{
canAttach = false;
hits = new RaycastHit[directions.Length];
for (int i = 0; i < directions.Length; i++)
{
Vector3 dir = transform.TransformDirection(directions[i]);
Debug.DrawRay(transform.position, dir * LengthFromWall);
if (Physics.Raycast(Player.position, dir, out hits[i], LengthFromWall))
{
if (hits[i].collider.tag == "wallRunSurface")
{
canAttach = true;
Debug.Log("you can attach!");
OnWall(hits[0]);
}
}
}
}
public void OnWall(RaycastHit hit)
{
float d = Vector3.Dot(hit.normal, Vector3.up);
if (d >= -normalizedAngleThreshold && d <= normalizedAngleThreshold)
{
//Vector3 alongWall = Vector3.Cross(hit.normal, Vector3.up);
float vertical = Input.GetAxisRaw(GameConstants.k_AxisNameVertical);
Vector3 alongWall = transform.TransformDirection(Vector3.forward);
Debug.DrawRay(transform.position, alongWall.normalized * 10, Color.green);
Debug.DrawRay(transform.position, lastWallNormal * 10, Color.magenta);
PlayerMovementScript.velocity = alongWall * vertical * wallSpeedMultiplier;
isWallRunning = true;
}
}
public void wallJump()
{
if(isWallRunning &&(Input.GetKey(KeyCode.Space) && Input.GetKey(KeyCode.D)))
{
Vector3 leftJump = new Vector3((PlayerMovementScript.velocity.x * .5f), 0f,(-1 * PlayerMovementScript.velocity.z));
leftJump += GetWallJumpDirection() * wallBouncing;
PlayerMovementScript.velocity.y = Mathf.Sqrt(wallBouncing * -1f * PlayerMovementScript.gravity);
AudioSource.PlayOneShot(jumpNoise, VolumeScale);
isWallRunning = false;
canAttach = false;
Debug.Log("you jumped right!");
}
else if(isWallRunning && (Input.GetKey(KeyCode.Space) && Input.GetKey(KeyCode.A)))
{
Vector3 rightJump = new Vector3((PlayerMovementScript.velocity.x * .5f), 0f, PlayerMovementScript.velocity.z);
rightJump += GetWallJumpDirection() * wallBouncing;
PlayerMovementScript.velocity.y = Mathf.Sqrt(wallBouncing * -1f * PlayerMovementScript.gravity);
AudioSource.PlayOneShot(jumpNoise, VolumeScale);
isWallRunning = false;
canAttach = false;
Debug.Log("you jumped left!");
}
}
public Vector3 GetWallJumpDirection()
{
if (isWallRunning)
{
return lastWallNormal * wallBouncing + Vector3.up;
}
return Vector3.zero;
}
//script which takes the player's rotation and the side the wall is on to determine how to jump.
// Update is called once per frame
void Update()
{
isWallRunning = false;
AttachCheck();
if (isWallRunning && Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D))
{
wallJump();
}
}
}
Answer by TeamON · May 06, 2021 at 07:45 AM
In your script, the directions array contains 5 vectors: the first two for the right side and the last two for the left side. Therefore, in the loop for if i = 0 or 1 then this is the right side, if i = 3 or 4 then this is the left side.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to check if a raycast is not hitting any tagged colliders? 0 Answers
Make a game in C# 0 Answers
Getting nullreference error while using sendmessage 1 Answer