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 Arlack · Aug 29, 2018 at 10:40 PM · raycastraycastingmove an object

Help: 3d, (Set, tilted, perspective camera) move object to mouse relative to the ground/objects.

Greetings. I am new to coding, and to Unity in general. My current game set up is 3d, with a perspective camera that is set at an angle(45x).

My player character is Ethan(for now) with an animation package I picked up from the asset store. The animation set has it so the character can "lock onto" a target(playerTarget) so that the player rotates around the target, facing it. I have the target set as an empty game object. I want the target to move with the mouse cursor so I can aim the player to fight/shoot. I have a projectile set up to fire from the player to the target so the player can aim. I need the target to be relative to the ground/objects so the projectile actually goes where the player would expect.

As I said, I do not know what I'm doing really with coding and my efforts so far have been met with failure. I've spent the last 3 days trying to get this to work. The target always has some odd movement relative to the player so when I fire, the projectile shoots off into space or the ground. Between my coding skill (or lack of), and my googlefu, I can't get what I need. Your 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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Ermiq · Sep 01, 2018 at 05:11 PM

So, you need the target to be at some predefined height from the ground. If so, you could just adjust its height in Update().
To detect the ground surface you'll need to do some raycasting as well. Using raycast you'll detect the current height of the targetobject above the ground. To make the ray work as expected, you should think about layers for the target and for enemies. The ray shouldn't react on the collision with the target itself and with enemies (because, the target should not fly above enemies, it should stay at desired height (1 meter from the ground for example).
So, select the target object in hierarchy, find a Layer button (it's above the Inspector) and set it to some layer, for example, you could use the predefined IgnoreRaycastlayer. This will make all the rays ignore the target.
Also, add new custom layer and give it a name Enemies, remember the number of this layer. And set all enemies to this layer.

 // Now in the code you set the filter, which will determine the layers for the raycast in
 // which it will detect collisions. Assuming '5' is the number of your 'Enemies' layer.
 // The following code makes a filter that returns all layers except the layer '5'.
 // Replace '5' with your enemies layer number here.
 int allLayersExceptEnemies = ~(1 << 5);
 public Transform playerTarget; // it's your target object
 public float height; // the height from the ground your target should be
 
 void Update() {
     // We're going to cast a ray from the target's position and straight down to the ground,
     // the length of the ray is the desired height,
     // 'allLayersExceptEnemies' means that the ray will detect all collisions with all objects,
     // but will ignore all object in the 'Enemies' layer
     if (!Physics.Raycast (playerTarget.position, Vector3.down, height, allLayersExceptEnemies)) {
         // This code will be executed if the ray didn't detect any collisions below the target,
         // this means that we need to lower the target's height, because it's too high from the ground.
         // We're taking its current position and making its 'Y' (the up axis) a bit lower...
         // To do that we need to save current position as a temporary variable
         Vector3 v = playerTarget.position;
         // modify its up axis value
         v.y = v.y - 0.01f;
         // and set this modified vector as a new position for the playerTarget
         playerTarget.position = v;
         // now if the target is too high, it will move down by 0.01 units in every frame
         // until it reaches the desired height.
     }
 }

Oh, PS: Also, you need to check if the target is not under the ground. Actually, you would just place it in the editor somewhere not ubder the ground and it should work. This code will keep it at the desired height.
But if you encounter some issues with the target suddenly fall down under the ground, then the code needs to be modified. But for now, try this.

Comment
Add comment · Show 3 · 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 Arlack · Sep 02, 2018 at 05:50 AM 0
Share

Right away I'd like to say thank you for trying to help!! I love your documentation. It's nice to see that as I'm still early learning. It's 2 am almost. <.< Been busy today and didn't get to check until now, but Visual Studio gave me an error with the code. Under the first "playerTarget.position"

         playerTarget.position.y = playerTarget.position.y - 0.01f;

"Cannot modify the return value of 'Transform.position' because it is not a variable"

If it helps I'm running Unity 2018.2. I'm gonna go pass out now! laugh

avatar image Ermiq Arlack · Sep 02, 2018 at 10:21 AM 0
Share

Sorry for that, my bad. I forgot that in order to modify position or rotation of the transform, one should save it in a variable and then assign modified variable to the position/rotation. I've changed the code in that part, now it's correct.

avatar image Arlack Ermiq · Sep 02, 2018 at 01:45 PM 0
Share

There we go, now it's doing something. laugh However, all it's doing is having the game object fall down to the set height and nothing else.

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

89 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

Related Questions

Raycast help..... 2 Answers

Problem With Raycasthit Angle 0 Answers

Diagonal Raycasting to detect platform returns true, even when false 1 Answer

camera.ScreenPointToRay always has same origin... 1 Answer

Why doesn't Physics.Raycast(ray, RaycastHit, distance) work? 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