How do I have my character aim where my mouse cursor is?
Here I took a picture to help you understand more of what I want!: http://imgur.com/a/jHqD6
so the red dot down in the entrance area, is where my mouse cursor was and the second dot above the entrance is where it was shooting, I would like the bullets to shoot where the mouse cursor is and if possible have the gun Move with the cursor on the Y axis (up and down).. also instead of having a cursor is there a way I could use a crosshair "image" that I make instead..
Here is the mouse code
// Turn the player to face the mouse cursor.
Turning ();
void Turning ()
{
//Create a ray from the mouse cursor on screen in the direction of the camera.
Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);
// Create a RaycastHit variable to store information about what was hit by the ray.
RaycastHit floorHit;
// Perform the raycast and if it hits something on the floor layer...
if(Physics.Raycast (camRay, out floorHit, camRayLength, floorMask))
{
// Create a vector from the player to the point on the floor the raycast from the mouse hit.
Vector3 playerToMouse = floorHit.point - transform.position;
// Ensure the vector is entirely along the floor plane.
playerToMouse.y = 0f;
// Create a quaternion (rotation) based on looking down the vector from the player to the mouse.
Quaternion newRotatation = Quaternion.LookRotation (playerToMouse);
// Set the player's rotation to this new rotation.
playerRigidbody.MoveRotation (newRotatation);
}
I think that's about it these are all the things I am having trouble with and can't figure out i'm a noob thanks in advance for any help!
Answer by villevli · Aug 10, 2016 at 10:21 PM
Just remove the line: playerToMouse.y = 0f;
(Line 16 on the code you posted here)
You can change the cursor using Cursor.SetCursor or you can make a more sophisticated crosshair that moves in the game world.
One way to make it: Hide the cursor with Cursor.visible and create a gameobject with sprite renderer and your crosshair image set to it. Set the objects position to the floorHit.point and rotation based on the normal at the target point. That's easy to get in your current script from floorHit.normal and the rotation can be set e.q. with crosshair.transform.forward = normal
or crosshair.transform.rotation = Quaternion.LookRotation (normal)
.
I figured out the crosshair, but my character follows the cursor correctly but he can't look up at all it starts off slightly ai$$anonymous$$g torwards the ground and you can't aim up past that, also the bullets don't shoot exactly where the cursor is any ideas?
Answer by Sethhalocat · Aug 10, 2016 at 10:42 PM
I recently did this in one of my games, im more then happy to share with you the script im using and maybe you can use it. I forced one object to always look at another object that follows the mouse around. Its my clever way since i suck at coding.
looks at mouse:
using UnityEngine;
using System.Collections;
public class Follow : MonoBehaviour {
void Update() {
Vector3 dir = Input.mousePosition - Camera.main.WorldToScreenPoint(transform.position);
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
}
spawns object:
var Shot : GameObject;
function Update(){
if (Player.arrowAmount >= 0){
if (MainAnimations.Scared == false){
if( Input.GetButtonDown("Fire1"))
shooting();
}
}
}
function shooting(){
Player.arrowAmount -= 1;
Instantiate(Shot, transform.position, transform.rotation);
}
My character shoots arrows and has the ability to run out of arrows, I figured id keep that part in if you wanted to add amunition to your game, if not you can always just take it out. The first one btw was C# and the last one was Java script... I hope this helps! :)