Move player to the edge of a trigger

Ok so heres the thing, after 3 days of failing with formulas and crunching different methods of math. I’ve come to a point where I’m outta juice. So i hope there’s some1 who can help me.

Ok then. When the player jumps to a ledge there’s a trigger that enables the player to “grab” the edge.
The edge can be placed at a varying distance so the script is gonna have to fix the position of the player.

670648--24065--$Untitled-1.jpg
The above picture is the result so far, the green line is the trigger. The pink line is a Debug.DrawLine of the formula i used.
I want that pink line to only go to the edge of the trigger but w/e formula i try keeps making it go out of the box.

Heres the code i’ve worked on:

if(triggerID.tag=="triggerEdgeClimb"&Input.GetKey(KeyCode.E)||trigger_firstPass){
            
            this.transform.rotation=triggerID.transform.rotation;
            if(trigger_firstPass==false){
                edgeTriggerTimer=0;
                trigger_firstPass=true;
                positionStore=this.transform.position;
            }

            
            this.transform.position=Vector3.Lerp(
                    /*From*/    positionStore,
            
                    /*To*/        triggerID.transform.TransformDirection(
                                    Vector3(0,0,
                                        triggerID.transform.localScale.z-(
                                            1.0f-triggerID.transform.localScale.z
                                        )
                                    )
                                )+
                                positionStore,
                                //her position relative to the trigger in Z,
                                
                                    
                    /*???*/        edgeTriggerTimer
                                );
            edgeTriggerTimer+=(Time.deltaTime/2);
}

i excluded the part where i use DrawLine() since it contains the same information.

So some1 plz shed some light on this for me!

bump

I believe I am close, though I don’t know how exact what I came up with fits into your scheme.

First, You didn’t show how you were getting the trigger, so I invented one myself. I set off first to make a simple game controller that, when I pressed E would go and find a trigger. It wasn’t hard to set up.

Once I found it, I set off to figure out how was the best way to align the controller to the trigger. This is where I hit the same brick wall as you did. (probably because I was following your example) So I stopped and though, What if we make a rule.

“All triggers have a wider X scale.”

Without this simple rule, we would have to figure out if we were facing the X direction or Z direction, in front of, in back of. It was becoming a nightmare real quick. Once I got that down, then math comes into play. Specifically this:

Vector3 point=obj.transform.InverseTransformPoint(transform.position);
point.x=0;
point=obj.transform.TransformPoint(point);
transform.LookAt(point)

What this does, is to take the offset from the trigger (where the player is now) and makes the X zero. This means that it is a point aligned with the trigger, but at the level of the player. Once we have it, we simply look at that point.

Now, its as simple as saying…" NO, you can’t control me anymore, I am going to move to my position." and do it.

So this is a very simple character controller script with this implemented.

using UnityEngine;
using System.Collections;


[RequireComponent (typeof(CharacterController))]
public class Movement1 : MonoBehaviour {
	public float rotateSpeed = 250f;
	public float moveSpeed = 20f;
	public float gravity = 20f;
	private CharacterController controller;
	private bool playerControl=true;

	public void Awake() {
		controller = gameObject.GetComponent<CharacterController>();
	}

	// Use this for initialization
	void Start () {

	}

	// Update is called once per frame
	void Update () {
		if(!playerControl) return;
		transform.Rotate(0,Input.GetAxis("Mouse X") * Time.deltaTime * rotateSpeed, 0);
		var move=transform.forward * Input.GetAxis("Vertical") * moveSpeed;
		move.y=-gravity;
		controller.Move(move * Time.deltaTime);
		
		if(Input.GetKeyDown(KeyCode.E)){
			Collider[] colliders =Physics.OverlapSphere(transform.position, 10.0f);
			float dist=Mathf.Infinity;
			GameObject obj=null;
			foreach(Collider collider in colliders){
				if(collider.isTrigger){
					Vector3 point=collider.transform.InverseTransformPoint(transform.position);
					point.x=0;
					if(point.magnitude < dist){
						dist=point.magnitude;
						obj=collider.gameObject;
					}
				}
			}
			if(obj)
				StartCoroutine(myCoroutine(obj));
		}
	}
	
	IEnumerator myCoroutine(GameObject obj){
		playerControl=false;
		
		Vector3 point=obj.transform.InverseTransformPoint(transform.position);
		point.x=0;
		point=obj.transform.TransformPoint(point);
		transform.LookAt(point);
		
		while(transform.InverseTransformPoint(point).z > 0){
			var move=transform.forward * moveSpeed / 8.0f;
			move.y=-gravity;
			controller.Move(move * Time.deltaTime);
			yield return 0;
		}
		//yield return new WaitForSeconds(0.5f);
		print("Jump");
		playerControl=true;
	}
}

Of course, written in C#, just to irritate the JS users. lol

I did couple this with a SmoothFollow script and it works pretty well.

I don’t really understand what i can use that rule with the x axis for.
I would assume for what i have now i only need a subtractor for the distance to be traveled. Which where i keep hit a brick wall.
this.transform.rotation=triggerID.transform.rotation
“this” is the player object.
and triggerID is the collider

I’m customizing the default 3rdpersonCharacterController which happens to be written in JS.

also. my trigger catcher:

private var triggerID:Collider;
function OnTriggerEnter (other : Collider){
    /*since the variable "other" is defined by the function OnTriggerEnter
    it wont be usable outside this function.
    transfering the data to a variable thats declared outside the function
    solves the problem*/
    triggerID=other;
}

function OnTriggerExit (){
    triggerID=null; // now there's no trigger active
}

i’ve been reading ur post like 10 times now. But i’m still stuck. sadface

bump

bump