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 shaggysays · Oct 31, 2014 at 07:35 PM · physicsinstantiatemove an objectgrappling gun

Need help moving player object towards an instantiated ball

ok so ive been working on this project to where the player has a "gun" which is just a cylinder right now with an empty object at the end called "spawner", i have a prefab of the "ammo" which is a ball with a script attached that makes the ball stick to the object it hits, now what im trying to do is make it so when the ball hits and sticks (which works right now with rigidbodies) it pulls the player to the ball object. ive tried using this inside the ball script ex: player.position = Vector3.movetowards(player.position,this.transform.position, step); but it wont do anything. ive even tried just making a new script to move the player towards ball just using the "Vector3.movetowards" but for some reason my player isnt budging, nor is the ball for that matter (though i dont want it to.. ive tried).. can anyone help me with this? racking my brain on it for hours and feel like im over thinking it. im just trying to simulate a grappling hook without a rope. and yes i have looked and looked for a solution for a grappling gun, but everyone seems to be worried about the rope and never reveals how they got this to work.

ballstick.js

 #pragma strict
 var player : GameObject;
 var pspeed : float;
 function Start () {
 
 }
 
 function Update () {
 var step = pspeed * Time.deltaTime;
 player.transform.position = Vector3.MoveTowards(player.transform.position,this.transform.position, step);
 
 //var dist = Vector3.Distance(player.position,this.transform.position);
 //
 //Debug.Log(dist);
 }
 
 
 function OnCollisionEnter(col : Collision){
 var hj : HingeJoint;
 
 hj = gameObject.AddComponent("HingeJoint");
 hingeJoint.connectedBody = col.rigidbody;
 
 
 
 
 transform.rigidbody.velocity = Vector3(0,0,0);
 transform.rigidbody.useGravity = false;
 transform.rigidbody.isKinematic = true;
 //transform.parent = col.transform;
 //transform.position = Vector3.zero;
 //transform.rotation = Quaternion.identity;
 
 
 
 
 }
 




and heres the script that shoots the ball

shootammo.js

 #pragma strict
 var bullet : Rigidbody;
 var speed : float;
 var player : Transform;
 function Start () {
 
 }
 
 function Update () {
 
 
         if(Input.GetButtonDown("Fire1")){
         
                 var clone : Rigidbody;
                 
                 
             clone = Instantiate(bullet,transform.position,transform.rotation);
         
         clone.velocity = transform.TransformDirection(Vector3.up * speed);
                 }
 
 
 
             
 
 }




EDIT UPDATE: ive tried spring joints between the ball and player character to no avail, but i did see movement using Vector3.lerp, but only when i fired the gun, and it instantly spawned onto the ball, stopping the balls trajectory and is definitely not what i want

Comment
Add comment · Show 2
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 MrSoad · Nov 01, 2014 at 01:20 AM 0
Share

This is not an answer, this is just his code reformatted with the empty Start() functions removed, I'm going to sleep(its late here) but I hope this saves whoever does answer this a few $$anonymous$$utes time :

 #pragma strict
 
 var player : GameObject;
 var pspeed : float;
 
 function Update () {
     var step = pspeed * Time.deltaTime;
     player.transform.position = Vector3.$$anonymous$$oveTowards(player.transform.position,this.transform.position, step);
 }
 
 function OnCollisionEnter (col : Collision) {
 
     var hj : HingeJoint;
 
     hj = gameObject.AddComponent("HingeJoint");
     hingeJoint.connectedBody = col.rigidbody;
     
     transform.rigidbody.velocity = Vector3(0,0,0);
     transform.rigidbody.useGravity = false;
     transform.rigidbody.is$$anonymous$$inematic = true;
 }
  
  
 #pragma strict
 
 var bullet : Rigidbody;
 var speed : float;
 var player : Transform;
 
 function Update () {
 
     if(Input.GetButtonDown("Fire1")){
 
         var clone : Rigidbody;
 
         clone = Instantiate(bullet,transform.position,transform.rotation);
         clone.velocity = transform.TransformDirection(Vector3.up * speed);
     }
 }
avatar image shaggysays · Nov 01, 2014 at 03:28 AM 0
Share

thank you for that, sorry i didnt even think about cutting those out, ive been stressing myself out over this.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by jdog98 · Nov 01, 2014 at 02:10 AM

Try transform.LookAt(target); it turns the player to look at target . If you put a force forward on the player, then it should move towards target

Comment
Add comment · Show 2 · Share
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 shaggysays · Nov 01, 2014 at 03:26 AM 0
Share

ok ive found a nice way to get my player character to go to a point, using Vector3.slerp, and messing with the player gravity, but how do i reference an instantiated object, this is the missing piece to my puzzle now, because they keep co$$anonymous$$g up as "Ball ammo(Clone)" so there is no unique identifier to the object for me to use it as a target that way, im on to the solution now, if someone can help me figure that out, and thank you so much for answering to help. many cheers.

avatar image jdog98 · Nov 01, 2014 at 04:45 AM 0
Share

alt text

target help.png (25.1 kB)

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

Set Rigidbody.velocity for an instantiated object 1 Answer

Help On Checking OverLaping in instantiating 0 Answers

Instantiate object to move with other gameObject 0 Answers

Need auto aim 0 Answers

Help with my code ArgumentException: The prefab you want to instantiate is null. UnityEngine.Object.CheckNullArgument (System.Object arg, System.String message) 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