- Home /
Aim Assist Script Problems
I'm creating an aim assist for use when the player is using a controller rather than a keyboard and mouse that works with the SmoothMouseLook.cs script that is on the Unify Script Wiki.
What I'm trying to do is raycast from the center of the screen forwards to find what the player is looking at. Then we check whether the object they are looking at is an enemy, and if so, temporarily reduce the sensitivity of the SmoothMouseLook script. Though I'm having some trouble- nothing seems to be happening. The ray doesn't seem to hit anything, so the sensitivity is never changed. Here's the script- can you help me out?
using UnityEngine;
using System.Collections;
public class AimHelper : MonoBehaviour {
private Vector3 DirectionRay;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
DirectionRay = transform.TransformDirection(Vector3.forward);
SmoothMouseLook Mouselook = GetComponent<SmoothMouseLook>();
RaycastHit hit;
if (Physics.Raycast (transform.position, DirectionRay, out hit, 100)) {
// Apply a force to the rigidbody we hit
if (hit.collider.CompareTag("Enemy")) {
Mouselook.sensitivityX = 4f;
Mouselook.sensitivityY = 4f;
}
} else {
Mouselook.sensitivityX = 6f;
Mouselook.sensitivityY = 6f;
}
}
}
Have you verified that the ray is in fact what you intended using something like Gizmos.DrawRay? Assu$$anonymous$$g your ray is correct, I'd check the layers of the objects you are raycasting against.
Your answer
Follow this Question
Related Questions
2D Rotate sprite (arm) so it faces the mouse. 1 Answer
Touch and aim with a raycast line 2 Answers
2D Cursor Aiming 2 Answers
Object "Look at" Mouse Z Axis 1 Answer
Get a float from a different GameObject? (MouseLook) 1 Answer