- Home /
jump at opposite angle of surface
I am super stuck on making a jumping script for my player. I am creating a kind of "rolling jump' clone (http://arcadelife.wordpress.com/2012/04/22/rolling-jump-review-ipodiphone/) and I want the player to be able to jump on rotating cylinders, and then jump off in the opposite direction of the floor. I have tried to use raycastHit to find the normal of the ground, onCollisionStay, eulerAngles, transformDirection but as I am not very good at coding i'm finding it hard to integrate bits together.
Below is a drawing of what I want to achieve, but with gravity still so if the player jumps towards nothing it will fall to the ground. At the moment I'm using fixed joints to attach the player to the rotating platforms, by creating it on collision.
Here is my current code:
private var myNormal: Vector3;
private var distGround: float;
private var turnAngle: float = 0;
function Start (){
myNormal = transform.up;
}
function Update () {
var up = transform.TransformDirection(Vector3.up);
var ray : Ray;
var hit : RaycastHit;
Debug.DrawRay(transform.position, -up * 10, Color.green);
var fingerCount = 0;
for (var touch : Touch in Input.touches) {
if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
fingerCount++;
}
if (fingerCount > 0){
rigidbody.velocity = Vector3.up * minHeight;
print ("User has " + fingerCount + " finger(s) touching the screen");
ray = Ray(transform.position, -myNormal);
if (Physics.Raycast(ray, hit)){ // use it to update myNormal and isGrounded
isGrounded = hit.distance <= distGround + deltaGround;
myNormal = Vector3.Lerp(myNormal, hit.normal, Time.deltaTime);
}
var turn = Input.GetAxis("Mouse X") * turnSpeed * Time.deltaTime;
turnAngle = (turnAngle + turn) % 360;
var rot = Quaternion.FromToRotation(Vector3.up, myNormal);
rot *= Quaternion.Euler(0,turnAngle,0); // and to current direction
transform.rotation = rot;
}
}
I've looked at many other questions and answers but none of them seem to work for me, does anyone have any ideas or answers? This has been driving me crazy for ages!
Thanks
Answer by alb917 · Apr 29, 2013 at 12:41 PM
I actually managed to solve this by ticking Animate Physics in the animation component! I've literally been stuck on this for a good 2 weeks and it was something so simple!
Answer by cdrandin · Apr 27, 2013 at 10:57 PM
Here is a script to rotates around the center of your screen. so you have an idea what to do.
Using C#:
using UnityEngine;
using System.Collections;
public class TEST : MonoBehaviour {
public string stringToEdit = "X";
public Vector2 center;
public float r;
public float angle;
private float x, y;
private float incr;
// Use this for initialization
void Start () {
center = new Vector2( Screen.width/2, Screen.height/2 );
r = 100.0f;
angle = 0;
incr = Mathf.PI/24;
stringToEdit = "X";
}
// Update is called once per frame
void Update () {
// parameterize the points on the circle as a function of time using the relations
// x=cx+rcos(t),y=cy+rsin(t) for (cx,cy) the circle's center, r its radius,
// and t the time.
x = r*Mathf.Cos( angle ) + center.x;
y = r*Mathf.Sin( angle ) + center.y;
angle += incr;
if(angle > Mathf.PI*2)
angle = 0.0f;
}
void OnGUI() {
GUI.Label(new Rect(x, y, 100, 50), stringToEdit);
}
}
SO, if you want to move it with some input just have for every cycle you press left or right you move +/- the incr value which you add onto the angle, to give a rotating effect about the center of your screen.
Sorry, I forgot to say that my platforms already rotate, which I have done with animations. I can jump from platform to platform but when the player jumps there is only movement on the Y axis, even if the player is on the side of the platform.
Thanks for your answer though!