Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 DedValve · Aug 28, 2015 at 11:13 AM · speedmomentuminertia

Make an object move towards another object when button is held then drift when button is released

My most recent conundrum is trying to create a "pull" effect. I'm making a space game where tapping on a small planet will create a line between the player & the planet and the planet pull the player.

The planet will only pull when the player taps and holds on it. What I want to achieve are 2 things:

1) Have the player slowly build up momentum up until a limit (normalize velocity will be used here I believe)

2) Have the player drift at a slower speed when I let go

Right now the speed stays constant but whats really been breaking my head in trying to solve is creating a "drift" effect (like drifting in space). When I let go of the planet the player abruptly stops. I would want them to keep drifting in that direction. If I press a planet in another direction then I want the speed to slow down (not abruptly stop) and quickly start moving to that planet. The effect I'm trying to achieve is Super Mario Galaxys pull stars. https://www.youtube.com/watch?v=eoqZXKQFEng

Here's what I have so far. You can pretty much just throw this on an cube with a rigidbody.

 using UnityEngine;
 using System.Collections;
 
 public class Pull : MonoBehaviour {
 
     public GameObject player;
     public Rigidbody RBplayer;
     public Vector3 Tplayer; //get the players transform
     public bool move;
     public Transform GoTo; 
     public float speed;
     public float maxSpeed;
 
     void Start() {
         player = GameObject.FindWithTag("Player");    //get the player gameobject
         RBplayer = player.GetComponent<Rigidbody>(); //get the players rigidbody
         Tplayer = player.transform.position; //get the players V3
         GoTo = gameObject.transform; //Player to go to current position. So it wont move away on start          
     }
 
     void Update() {
         if(move == true){
             float step = speed * Time.deltaTime;
             //tells the players position by getting the players current position and have it move towards the pully we tap on
             transform.position = Vector3.MoveTowards(transform.position,GoTo.position,step); 
             RBplayer.AddForce(step * GoTo.transform.position);
             RBplayer.velocity = RBplayer.velocity.normalized * maxSpeed;
         }
     }
 
     void OnCollisionEnter(){ //if we collide with any other object then the line between player & pully is broken
         move = false;
     }
 }
 

and here is a seperate script that I attach on the planets that determine if the player should be pulled.

 using UnityEngine;
 using System.Collections;
 
 public class MoveTowards : MonoBehaviour {
 
     public Pull pullScript;
 
     void Start () {
         pullScript = GameObject.FindWithTag("Player").GetComponent<Pull>();
     }
     
     void OnMouseDown () {
         pullScript.GoTo = gameObject.transform;
         pullScript.move = true;
     }
 
     void OnMouseUp () {
         pullScript.move = false;
         pullScript.GoTo = null;
     }
 }

Any help would be greatly appreciated!

Comment
Add comment
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

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

2 People are following this question.

avatar image avatar image

Related Questions

How can I remove inertia from character controller? 0 Answers

rigidbody2D with instant max speed and slow stop 1 Answer

How to decrease inertia for a rolling ball 1 Answer

What can I do to prevent momentum? 0 Answers

How can I "nerf" air-strafe-ing in this script? 2 Answers


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