Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by aaron2005 · Jun 27, 2015 at 11:52 PM · player movement

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);
 
         }
     }

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image starikcetin · Jun 27, 2015 at 11:55 PM 0
Share

I didn't even understand what you did or what you trying to do, neither the code.

avatar image aaron2005 · Jun 28, 2015 at 02:49 PM 0
Share

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.

avatar image Inok · Jun 28, 2015 at 05:57 PM 0
Share

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.

avatar image aaron2005 · Jun 28, 2015 at 06:45 PM 0
Share

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 :/

avatar image Inok · Jun 28, 2015 at 07:05 PM 0
Share

you use "RigidBodyFPSController" prefab from standart assets?

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Player movement restrictions 0 Answers

How do i keep my characters orientation? 0 Answers

Player cannot jump 0 Answers

Local player always at same position 2 Answers

Player2d Movement (rigidbody2d.velocity) with small vibrations, very similar to small collisions 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges