- Home /
Pull the player to an raycast.hit
Hi there. I got a little problem. I have a script, where I can pull blocks toward my player. This works fine.
Now I need the other way. I hit an object with my laser and the object should stay still, but the player have to get toward the object. I need to get force on my player, because I want to get thrown farther if I stop my laser. I use the FPS script from unity. I tried the other FPS-rigidbody, but there are problems with other stuff (I have freezed blocks, which still move if I step on them with FPS rigidbody).
I will give you a piece of script with my pull stuff. The stuff in "ziehen" and "schieben" are the interesting part. I got the code from the internet (sorry, dont now the reference) and worked a bit on it.
// Update is called once per frame
void Update () {
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1)) {
StopCoroutine("FireLaser");
StartCoroutine("FireLaser");
//Debug.Log (hit.transform.name);
}
if (Input.GetMouseButtonUp(0) || Input.GetMouseButtonUp(1)) {
if (letztesZielObjekt != null && letztesZielObjekt.GetComponent<fallendesObjektScript>() != null){
letztesZielObjekt.GetComponent<fallendesObjektScript>().gestossen = false;
//letztesZielObjekt.GetComponent<Rigidbody>().useGravity = true;
}
StopCoroutine("FireLaser");
line.enabled = false;
//StartCoroutine("FireLaser");
//Debug.Log (hit.transform.name);
}
}
IEnumerator FireLaser(){
line.enabled = true;
while(Input.GetMouseButton(0) || Input.GetMouseButton(1)){
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hit;
line.SetPosition(0, ray.origin);
if(Physics.Raycast(ray, out hit, 100)){
line.SetPosition(1, hit.point);
}else {
line.SetPosition(1, ray.GetPoint (100));
}
//line.SetPosition(1, hit.point);
if(hit.rigidbody && zeitManipulator.zeitLauf == 1 && hit.rigidbody.gameObject.tag != "ignorier" && hit.transform.gameObject.GetComponent<fallendesObjektScript>() != null){
if (letztesZielObjekt != null){
if (letztesZielObjekt == hit.transform.gameObject){
//letztesZielObjekt.GetComponent<fallendesObjektScript>().gestossen = true;
} else
if (letztesZielObjekt != hit.transform.gameObject && hit.transform.gameObject.GetComponent<fallendesObjektScript>() != null){
letztesZielObjekt.GetComponent<fallendesObjektScript>().gestossen = false;
//letztesZielObjekt.GetComponent<Rigidbody>().useGravity = false;
letztesZielObjekt = hit.transform.gameObject;
letztesZielObjekt.GetComponent<fallendesObjektScript>().gestossen = true;
//letztesZielObjekt.GetComponent<Rigidbody>().useGravity = true;
}
}
letztesZielObjekt = hit.transform.gameObject;
//Debug.Log (hit.transform.name);
//hit.transform.gameObject.SetActive(false);
//hit.rigidbody.useGravity = true;
hit.rigidbody.constraints = RigidbodyConstraints.None;
if (Input.GetMouseButton(0)){
ziehen(hit);
} else
if (Input.GetMouseButton(1)){
schieben(hit);
}
}
else
{
Physics.Raycast(ray, out hit, 100);
}
yield return null;
}
}
void ziehen (RaycastHit ziel){
if(ziel.rigidbody!=null) {
Vector3 D = transform.position - ziel.transform.position; // line from crate to player
float dist = D.magnitude;
Vector3 pullDir = D.normalized; // short blue arrow from crate to player
if(dist>3) { // don't pull if too close
// this is the same math to apply fake gravity. 10 = normal gravity
float pullF = 10;
// for fun, pull a little bit more if further away:
// (so, random, optional junk):
float pullForDist = (dist-3)/2.0f;
if(pullForDist>20) pullForDist=20;
pullF += pullForDist;
//stärke = stärke / dist;
// Now apply to pull force, using standard meters/sec converted
// into meters/frame:
ziel.rigidbody.velocity += pullDir*(pullF * Time.deltaTime * stärke);
}
}
}
void schieben (RaycastHit ziel){
if(ziel.rigidbody!=null) {
Vector3 D = transform.position - ziel.transform.position; // line from crate to player
float dist = D.magnitude;
Vector3 pullDir = D.normalized; // short blue arrow from crate to player
// don't pull if too close
// this is the same math to apply fake gravity. 10 = normal gravity
float pullF = 10;
// for fun, pull a little bit more if further away:
// (so, random, optional junk):
float pullForDist = (dist-3)/2.0f;
if(pullForDist>10) pullForDist=10;
pullF += pullForDist;
//stärke = stärke / dist;
// Now apply to pull force, using standard meters/sec converted
// into meters/frame:
ziel.rigidbody.velocity -= pullDir*(pullF * Time.deltaTime * stärke);
}
}
I didn't even understand what you did or what you trying to do, neither the code.
Okay. I´m sorry.
Lets say, you have a big gap. And on the other side you have a big stone. Now you aim the stone and fire a laser. While you hold the fire button you "fly" toward the stone. If you release the button, you have still the force, which pull you toward the stone. And with this force you can fly a bit farther.
Thats basics are something like you know from just cause with the grappling hook, where you can get yourself to a helicopter.
if i undestand right you need replace "ziel" in "void schieben" to gameobject that represent your player's rigidbody. and check that player rigidbody not kinematic and not have position constraints when you apply pull velocity on player rigidbody.
Yes, thats exactly what I need. Inok. $$anonymous$$y problem is, that every try to use force or velocity don´t works on my FPS player controller. I tried both, the normal and this with the rigidbody. But I couldnt move my player :/
you use "RigidBodyFPSController" prefab from standart assets?