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 StartUpStudios · Sep 18, 2016 at 01:39 PM · scripting problemphysicsfirst-person

Throw Object at Different Angle on MouseClick

I have a script where I click and an object spawns on the screen (in first person). Great. However, no matter where I click on the screen, it's always throws perfectly perpendicular to the camera, effectively making it impossible to aim. I want to throw as if there was a cannon attached to the camera, and it pointed to wherever the mouse was in the world.

(Yes, I know I dont have any 'throwing' physics on my Inst obj. But I figured I would get the angle down before I messed with the throwing physics)

I could not find a single thing. I could have been using the wrong search terms, but my past 2 hours of searching has not netted me an answer. Thank you for your time.

Here is the code I use:

 using UnityEngine;
 using System.Collections;
  
 public class RoadMaster : MonoBehaviour
 {
  
     public GameObject object2Throw;
     public GameObject barrels;
     public Camera RMCamera;
     public Transform gunObj;
     Plane plane;
  
     void Start()
     {
         plane = new Plane(Vector3.forward, Vector3.zero);
     }
  
     void Update()
     {
         if (Input.GetButtonDown("Fire1"))
         {
             Debug.Log("Firing");
             var mousePos = Input.mousePosition;
             mousePos.z = 10.0f;       // we want 2m away from the camera position
             var objectPos = RMCamera.ScreenToWorldPoint(mousePos);
             Instantiate(object2Throw, objectPos, Quaternion.LookRotation(RMCamera.ScreenToWorldPoint(mousePos)));
  
  
  
        
         }
     }
  
  
     public void makeBarrels()
     {
         object2Throw = barrels;
  
     }
 }
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
Best Answer

Answer by StartUpStudios · Sep 19, 2016 at 06:11 PM

I've figured out a way to make this work. I took the hit position of a Raycast from the camera to a canvas in front of the camera, and made the 'cannon' look at it. Then shot the object with the cannon's direction. Below is the code. Hopefully it can help someone else out.

 using UnityEngine;
 using System.Collections;
 
 public class RoadMaster : MonoBehaviour
 {
 
     public float firingForce = 500;
     public GameObject object2Throw;
     public GameObject barrels;
     public GameObject Ramps;
     public Camera RMCamera;
 
     public GameObject objectCannon;
 
     public Transform mousePOS;
     public Transform target;
 
     Plane plane;
 
     void Start()
     {
         plane = new Plane(Vector3.forward, Vector3.zero);
     }
 
     void Update()
     {
         if (Input.GetButtonDown("Fire1"))
         {
             Debug.Log("Firing");
             var mousePos = Input.mousePosition;
             mousePos.z = 10.0f;       // we want 2m away from the camera position
             var objectPos = RMCamera.ScreenToWorldPoint(mousePos);
             if (object2Throw != Ramps)
             {
                GameObject tossedObj = (GameObject)Instantiate(object2Throw, objectPos, objectCannon.transform.rotation);
                 tossedObj.GetComponent<Rigidbody>().AddForce(objectCannon.transform.forward * firingForce); 
             } else 
             {
                 Instantiate(object2Throw, objectPos, Quaternion.Euler(270, 90, 0));
 
             }
             }
         
         //target.position = RMCamera.ScreenToWorldPoint(Input.mousePosition);
         //objectCannon.transform.LookAt(target);
         RaycastHit hit;
         Ray ray = RMCamera.ScreenPointToRay(Input.mousePosition);
 
         if (Physics.Raycast(ray, out hit))
         {
             
             Vector3 hitPosition = hit.point;
             objectCannon.transform.LookAt(hitPosition);
             // Do something with the object that was hit by the raycast.
         }
     }
 
 
     public void makeBarrels()
     {
         object2Throw = barrels;
 
     }
 
     public void makeRamps()
     {
         object2Throw = Ramps;
 
     }
 }
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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Is it recommended to do scientific modelling in unity or should i write a standalone program 3 Answers

Car Movment Script not fully working!!!!!!! 1 Answer

Need help with my movement script.. :( 1 Answer

2D Platformer with custom physics Object - jumping issue 0 Answers

Model not translating along its position with Animate Physics option enabled. 0 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