Dash Mechanic, transform player on a button push
I am trying to create a dash mechanic for a 3d game. Currently i am using a raycast to identify if the player will hit an object when dashing. I then try to set the player position to be the position of the ray collision. The problem i am having is that the player is getting translated to above and below the map which is not what i want. Heres is my code: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Dash : MonoBehaviour{
[SerializeField] private string playerAlt;
private RaycastHit[] hit;
// Update is called once per frame
void Update(){
if (Input.GetButtonDown(playerAlt)) {
hit = Physics.RaycastAll(transform.position, transform.forward, 10);
Debug.DrawRay(transform.position, transform.forward * 10, Color.green, .1f);
if (hit.Length > 0){
foreach (RaycastHit hit in hit) {
if (hit.collider != null){
Vector3 result = hit.collider.gameObject.transform.localScale / 2;
result.x *= -transform.forward.x;
result.y = 0;
result.z *= -transform.forward.z;
transform.position = new Vector3(hit.collider.gameObject.transform.position.x + (result.x), 0, (hit.collider.gameObject.transform.position.z + (result.z)));
}
}
}
else
{
transform.position += transform.forward * 10;
}
}
}
} Any suggestions??
Your answer
Follow this Question
Related Questions
How to raycast object as far as possible, without collision? 0 Answers
Ray cast to object to transform, instead all objects with script transform. 0 Answers
How do you detect a mouse button click on a Game Object? C# 2 Answers
create object on raycast collision that follows raycast. 0 Answers
Adding collision to Raycast hit 0 Answers