- Home /
Moving a character whilst also aiming with a crosshair TPS
Hi guys, thanks for replying. It seems like my message has been cut off half way for some reason. I will repost:
Hi guys! I am currently creating a infinite runner which automatically moves the character forwards and the terrain backwards. I wanted to use the mouse as the cursor to shoot objects on screen, whilst also rotating the character left or right (doesn't have to be exactly where the mouse is pointing, rotation of character should be a -60* to 60*). At the moment I am using a mouse orbit script, but the mouse positioning is awkward, sometimes to the side of the character even when he is running forward (see screenshot attached). Is there a way I am able to have it so that if the cursor/crosshair is exactly in the middle of the screen, the character will run forwards and if left or right (depending on how far out it is) then the character will travel in that direction?
var target : Transform;
var distance = 10.0;
var xSpeed = 250.0;
var ySpeed = 120.0;
var yMinLimit = -20;
var yMaxLimit = 80;
private var x = 0.0;
private var y = 0.0;
var maxZoom : float = 15;
var minZoom : float = 3;
//var speedZoom : float = 10;
@script AddComponentMenu("Camera-Control/Mouse Orbit")
function Start () {
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
function LateUpdate () {
if (target) {
x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation = Quaternion.Euler(y, x, 0);
var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
target.rotation = rotation;
transform.rotation = rotation;
transform.position = position;
distance = Mathf.Clamp (distance + Input.GetAxis("Mouse ScrollWheel"), minZoom, maxZoom);
}
}
static function ClampAngle (angle : float, min : float, max : float) {
if (angle < -180)
angle += 180;
if (angle > 180)
angle -= 180;
return Mathf.Clamp (angle, min, max);
}
What exactly is the problem? What have you tried? What do you need help with ?
There are a lot of unknown in your question. Imagine you are standing outside point out object for a friend to shoot at. The rotation your friend need to hit the target will be different than your rotation. And the differences in the two rotation will vary based on the distance the target is away from the two of you. You have the same triangulation issue with your crosshair/TPS scenario. The first decision is ai$$anonymous$$g. You can do:
Raycast. Only allow the gun to shoot if the raycast hits something.
Use a fixed distance. That is, the shooter will hit the middle of the crosshairs at some specified distance.
$$anonymous$$ix the two. Use Raycast when it hits something and use distance when the Raycast fails.
The second decision is rotation. Choices:
Character's rotation places the crosshair
Crosshair is used to rotate the character
Character and crosshair are independent and the shooting does not follow the forward of the character.
Note if using Raycasting, the angle the character rotates will can vary greatly with just subtle moves of the mouse (since targets will not be at a fixed distance from the TPS).
@robertbu It seems as if there was an error when posting my original question, as it came through incomplete. Please see the updated version above :)
@ShadoX Please see the question again above, there was an error when posting earlier! Thanks!
Answer by bdorn14 · Mar 22, 2014 at 07:23 PM
You need to add a animation to make the character 'turn' while running, and add a script in that will make the character 'follow' the mouse cursor. There should be one in the default unity assets package.