Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 andycodes · Feb 05, 2017 at 08:10 AM · physicsvector3magnitudeslingshot

click on object, drag to draw line in opposite direction, mouseUp to apply force 3D

I haven't been sure where to start with this question and all related questions that i've been able to find so far have only dealt with 2D space.

What I'm trying to achieve is to: 1. begin drawing a line when an object containing the script is clicked on in the game. 2. when the mouse is dragged backwards, a line is drawn in the opposite direction, however it needs to be locked to the XZ axis ( So the Y Coordinate stays at 0). 3. when the mouse is released, a physics calculation is done to add force to the ball and it gets shot away from the mouse.

I wrote up some pseudo code here to help visualize what I'm trying to achieve:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ShootBall : MonoBehaviour {
 
     private Vector3 clickedPos; //where the user clicked 
     private Vector3 releasePos; //where the user releases the mouse
     public float powerMultiplier; //adds extra force to object when released.
 
     public float minPull;        //minimum drag distance for physics force to be added
     public float maxPull;        //maximum distance the ray will calculate physics for to limit force added

     //minPull and max Pull represent magnitudes
 
     void Start () {
         //get rigidbody component from gameobject (assume there is one attached).
     }
 
     void Update () {
         
     }
 
     void OnMouseDown(){
         //Start to draw a ray
         //clickedPos = this position
     }
 
     void OnMouseDrag(){
         //Draw line in opposite direction BUT restrict it to the x and z axis
         //(if current position - clickedPosition > maxPull) the line doesn't grow
     }
 
     void OnMouseUp(){
         //if(clickedPos - releasePos > minpull)  rigidbody.addforce(distancePulled * powerMultiplier)
     }
 }




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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by mustang4484 · Dec 13, 2018 at 08:59 PM

hello have you wrote the real code?

Thk

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

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

102 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to touch anywhere in the screen and the point touched should alwaysstart from a particular point 1 Answer

Constraining maximum movement speed on a rigidbody on certain axes. 1 Answer

2.5d Player Controller 0 Answers

How do I get my solution to go forward or right on the world axis 1 Answer

Vector3 values always precalculated, or just in MonoDevelop? 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