- Home /
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!
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 IgnoreRaycast
layer. 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.
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
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.
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.