- Home /
Modifying the HeadLook Controller in C#
Hey guys.
I've modified the headlook controller itself in C#. Every single code that's being used in my project is written with JS, except for the HeadLook Controller. I can't do the simplest thing; avoiding the Player tag.
using UnityEngine;
using System.Collections;
public class CursorHit : MonoBehaviour {
public HeadLookController headLook;
// public Transform Player;
private float offset = 3.5f;
//public LayerMask mask = 9;//1 << 8;
public LayerMask mask = 6;
// Update is called once per frame
void LateUpdate () {
//
// int AimRaycast = 8;
//int AimRaycastMask = 1 << AimRaycast;
if (Time.timeScale == 1.0) {
if (Input.GetKey(KeyCode.UpArrow))
offset += Time.deltaTime;
if (Input.GetKey(KeyCode.DownArrow))
offset -= Time.deltaTime;
offset = Input.GetAxis("Mouse Y");
Ray cursorRay = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(cursorRay, out hit)) {
transform.position = hit.point + offset * Vector3.up;
Screen.lockCursor = true;
// Physics.IgnoreCollision(Player.collider, collider);
// print(LayerMask.LayerToName(8));
}
headLook.target = transform.position;
}
}
}
I can do this just fine in JS, but everything is done a little differently in C#. I want the raycast done here to avoid/ignore any object with the "Player" tag. Can anyone help me? Thanks in advance.
Answer by kmeboe · Oct 04, 2012 at 01:18 AM
I don't have a ton of experience with colliders, so there may be a better/more efficient way to do this. But this should work:
if (hit.collider.gameObject.tag != "Player")
{
// Good to go!
}
Yes, that's exactly what I was looking for, but for some reason this locks the headlook sphere when raycast returns "Player", and wont unlock when it returns any other tag...
Glad I could answer the question.
Now for your next problem. What is the "headlook sphere"? Are you locking it with a line of code? $$anonymous$$aybe post the relevant code for us to exa$$anonymous$$e.
Edit: sounds like you worked it out.
This: http://unity3d.com/support/resources/unity-extensions/head-look-controller
It's pretty cool, and you can gain similar results with what R* has done with $$anonymous$$ax Payne 3, when slightly modified.
Answer by scarletsnake · Oct 04, 2012 at 09:23 PM
Solved it. Using bits and pieces from here; http://fearedfuture.blogspot.com/2009/08/unity-ray-casting-c-code.html The tag option did work, if the gameObject's tag never came up as Player. I'm sure it'll be useful in the future as well. Thanks for your time.
Your answer
Follow this Question
Related Questions
How to create User layer that acts like the "Ignore Raycast" layer? 1 Answer
ignore raycast layers problem 1 Answer
Multiple Cars not working 1 Answer
Raycast that ignores object with certain tags? 2 Answers
Distribute terrain in zones 3 Answers