- Home /
Question by
abdelrahmanyoussry509 · Aug 04, 2020 at 11:38 AM ·
scripting problemvector3jumpinghow to
How do i make my player jump from an angle? (Wallrun Jump)
hi i will get to the point , i want my player while he is running on the wall ,when i press space he jumps from the wall he is running on at an 45 degree angle so he will be able to reach the other wall oppsite to the other wall
i made the wall run and jumping but when he jumps he jumps up not at 45 degree angle i want
if you know how to fix it please explain it to me cuse i am new and dumb Thank you :P
(The Line that is the Problem is from line 225 to 230)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Motion : MonoBehaviour
{
public float speed;
private Rigidbody rig;
public float sprintmodifire;
public Camera PlayerCam;
private float remove;
public Transform groundchek;
public LayerMask groundmask;
private float t_adjustedSpeed;
public float jumpforce;
private float BaseFOV;
private float FOVmodifre = 1.2f;
//bobling
public Transform WeaponParent;
private Vector3 WeaponParentOrgin;
private float MovementCounter;//counts the secounds you are moving
private float IdelCounter;
private Vector3 WeaponBobTarget;
public bool isRuningLEFT = false;
public bool isRuningRigh = false;
private RaycastHit HitLEF;
private RaycastHit HitRIG;
private float jumpCount = 0;
private bool IsGrapPush= false;
public GameObject Explostion;
void Start()
{
rig = GetComponent<Rigidbody>();
BaseFOV = PlayerCam.fieldOfView;
remove = 1;
WeaponParentOrgin = WeaponParent.localPosition;
}
public IEnumerator GrapplePush()
{
rig.AddRelativeForce((Vector3.forward * t_adjustedSpeed / 7));
yield return new WaitForSeconds(1f);
FindObjectOfType<GrapplingGunScript>().isGrapling = false;
}
private void Update()
{
//axis
float t_Hmove = Input.GetAxis("Horizontal");
float t_Vmove = Input.GetAxis("Vertical");
//controls
bool spirnt = Input.GetKey(KeyCode.LeftShift);
bool jump = Input.GetKey(KeyCode.Space);
Vector3 t_direction = new Vector3(t_Hmove, 0, t_Vmove);
t_direction.Normalize();
//Headbobling
if(t_Hmove == 0 && t_Vmove == 0)
{
Headbob(IdelCounter,0.025f,0.025f);
IdelCounter += Time.deltaTime ;
}else if (spirnt)
{
Headbob(MovementCounter, 0.1f, 0.075f);
MovementCounter += Time.deltaTime * 5;
}
else
{
Headbob(MovementCounter, 0.05f, 0.05f);
MovementCounter += Time.deltaTime *3;
}
WeaponParent.localPosition = Vector3.Lerp(WeaponParent.localPosition, WeaponBobTarget, Time.deltaTime * 8f);
}
// Update is called once per frame
void FixedUpdate()
{
var grappler = FindObjectOfType<GrapplingGunScript>();
bool isGorunded;
isGorunded = Physics.Raycast(groundchek.position, Vector3.down, 0.1f, groundmask);
//Axis and motion
float t_Hmove = Input.GetAxis("Horizontal");
float t_Vmove = Input.GetAxis("Vertical");
//we save them both in a vector 3
Vector3 t_direction = new Vector3( t_Hmove,0, t_Vmove);
// when we press w and d at same time we add 1from w on 1 from d so it makes player go faster so we nomlize (keep it 1)
t_direction.Normalize();
//Controls
bool spirnt = Input.GetKey(KeyCode.LeftShift);
bool jump = Input.GetKey(KeyCode.Space);
//MOTION AND SPRINT
bool isjumping = jump;
bool isSprinting = spirnt && t_Vmove >0 && !isjumping && isGorunded ||isRuningLEFT ||isRuningRigh || IsGrapPush;
if (isjumping && isGorunded )
{
rig.AddForce( Vector3.up * jumpforce);
}
t_adjustedSpeed = speed;
//GRAPPLE
IEnumerator GrapplePush()
{
rig.AddRelativeForce((Vector3.forward * t_adjustedSpeed / 12));
yield return new WaitForSeconds(0.7f);
FindObjectOfType<GrapplingGunScript>().isGrapling = false;
IsGrapPush = true;
yield return new WaitForSeconds(1f);
IsGrapPush = false;
}
if (isSprinting)
{
t_adjustedSpeed = sprintmodifire * t_adjustedSpeed;
}
if (grappler == null || !grappler.isGrapling)
{
Vector3 TargetVelocity = transform.TransformDirection(t_direction) * t_adjustedSpeed * Time.deltaTime;
TargetVelocity.y = rig.velocity.y;
rig.velocity = TargetVelocity;
}
else if(grappler != null && grappler.isGrapling)
{
StartCoroutine(GrapplePush());
}
if (isSprinting)
{
PlayerCam.fieldOfView = Mathf.Lerp(PlayerCam.fieldOfView,BaseFOV *FOVmodifre,Time.deltaTime*8f);
}
else
{
PlayerCam.fieldOfView = Mathf.Lerp(PlayerCam.fieldOfView, BaseFOV , Time.deltaTime * 8f);
}
//Wallrun code
if (isGorunded)
{
jumpCount = 0f;
}
if (remove == 1 )
{
if (Physics.Raycast(transform.position, transform.right, out HitRIG, 1))
{
if (HitRIG.transform.tag == "Wall")
{
isRuningLEFT = false;
isRuningRigh = true;
jumpCount += 1;
rig.useGravity = false;
rig.AddForce(100, 0, 0);
}
}
else if (Physics.Raycast(transform.position, -transform.right, out HitLEF, 1))
{
if (HitLEF.transform.tag == "Wall")
{
isRuningLEFT = true;
isRuningRigh = false;
jumpCount += 1;
rig.useGravity = false;
rig.AddForce(100,0,0);
}
}
else
{
isRuningLEFT = false;
isRuningRigh = false;
rig.useGravity = true;
}
}
if(isRuningLEFT && Input.GetKeyDown(KeyCode.Space) || isRuningRigh && Input.GetKeyDown(KeyCode.Space))
{
rig.AddForce(Vector3.up * 20, ForceMode.Impulse);
print("is jumping");
}
}
void Headbob(float p_z,float X,float Y)
{
WeaponBobTarget = WeaponParentOrgin+ new Vector3(Mathf.Cos(p_z ) * X, Mathf.Sin(p_z * 2) * Y, 0);
}
}
Comment
Answer by davidcox70 · Aug 04, 2020 at 01:13 PM
Try adding some left or right direction into your addForce:
if(isRuningLEFT && Input.GetKeyDown(KeyCode.Space))
{
rig.AddForce((Vector3.up+Vector3.left) * 20, ForceMode.Impulse);
print("is jumping left");
}
if (isRuningRigh && Input.GetKeyDown(KeyCode.Space))
{
rig.AddForce((Vector3.up+Vector3.right) * 20, ForceMode.Impulse);
print("is jumping right");
}