- Home /
Question by
isao_hatake · Sep 05, 2013 at 12:16 AM ·
characterparkour
I have a problem with my parkour C# script on final lines
Hello everyone and thanks to receive your help, i'm making a parkour script but i have problems on the final lines on void JumpToWall last method, can someone help?
using UnityEngine;
using System.Collections;
public class Parkour: MonoBehaviour
{
public float moveSpeed = 6.0f; // Movement speed
public float turnSpeed = 90.0f; // Turning speed in degrees per second
public float gravity = 10.0f; // Gravity acceleration
public float jumpSpeed = 10.0f;
public float jumpRange = 10.0f;
float distGround;
float lerpSpeed = 10.0f; // Smoothing speed
float deltaGround = 0.2f; // Character is grounded up to the distance
float vertSpeed = 0.0f; // The current speed of the Vertical Jump
bool isGrounded;
bool jumping = false; // Flag for jumping to the wall
Vector3 surfaceNormal = new Vector3(1,2,3);
Vector3 myNormal = Vector3.forward;
Vector3 myForward;
Ray ray;
RaycastHit hit;
Quaternion targetRot;
// Use this for initialization
void Start ()
{
myNormal = transform.up; // Normal starts as the up direction of the character
rigidbody.freezeRotation = true; // Disables the rotation via physics
distGround = collider.bounds.extents.y - collider.bounds.center.y; // Distance from transform.posistion to the ground
}
void FixedUpdate()
{
rigidbody.AddForce (-gravity * rigidbody.mass * myNormal); // Apply constant weight force according to character normal
}
// Update is called once per frame
void Update ()
{
// Jump Code - Jump to wall or simple Jump
if (jumping) return;
Ray ray;
RaycastHit hit;
if (Input.GetButtonDown ("Jump"))
{ // If the player is pressing jump...
ray = new Ray(transform.position, transform.forward);
if (Physics.Raycast(ray, out hit, jumpRange)) { // Checks to see if there is a wall
JumpToWall(hit.point, hit.normal); // Yes Jump to the wall
//THERE IS NO METHOD FOR JUMPTOWALL!
}
} else {
if(isGrounded) { //no; if grounded, jump up
rigidbody.velocity += jumpSpeed * myNormal;
}
}
// Movement Code - Turn left/right with Horizontal Axis
transform.Rotate(0, Input.GetAxis ("Horizontal") * turnSpeed * Time.deltaTime, 0); // Update the surface noraml and isGrounded
ray = new Ray(transform.position, -myNormal); // Cast a ray downwards
if (Physics.Raycast(ray, out hit))
{
isGrounded = hit.distance <= distGround + deltaGround;
surfaceNormal = hit.normal;
}
else
{
isGrounded = false;
surfaceNormal = Vector3.up; // Assumes that the noraml is up to avoid some weird shit
}
myNormal = Vector3.Lerp(myNormal, surfaceNormal, lerpSpeed * Time.deltaTime);
myForward = Vector3.Cross (transform.right, myNormal); // Finds the forwards direction with my normal
targetRot = Quaternion.LookRotation (myForward, myNormal); // Alligns the character to the new myNormal whilst keep the same forward Direction
transform.rotation = Quaternion.Lerp (transform.rotation, targetRot, lerpSpeed * Time.deltaTime);
transform.Translate (0, 0, Input.GetAxis ("Vertical") * moveSpeed * Time.deltaTime);
}
void JumpToWall(Vector3 point, Vector3 normal) {
jumping = true;
var orgPos = transform.position;
var orgRot = transform.rotation;
var dstPos = point + normal * (distGround + 0.5);
var myForward = Vector3.Cross(transform.right, normal);
var dstRot = Quaternion.LookRotation (myForward, normal);
for (var t = 0; t < 1.0; t=t+1) {
t += Time.deltaTime;
transform.position = Vector3.Lerp (orgPos, dstPos, t);
transform.rotation = Quaternion.Slerp (orgRot, dstRot, t);
yield; // return here next frame
}
}
}
Comment
"I have problems" is not helpful. What exactly should it be doing, and what is it doing wrong?