- Home /
Locking the players y-axis so you cant lean into walls. (java)
Hello guys im in need of help with my script.
I want to lock the y-axis for both my player and enemy so that you cant look through walls and such.
Is there a way to lock the y-axis but still be able to look up or down as the player?
(Sorry im a modeler on my team, not a scripter) :/
Your question is vague. What are you currently using to rotate your character? We need to see your current code plus maybe a fuller explanation of your situation.
Sorry i am using the C# $$anonymous$$ouselook script provided by unity.
I think you are going to have to explain some more. The standard $$anonymous$$ouseLook has a Axis variable that you can set in the inspector to limit the axes used for rotation. How is what you want different?
I have the same line of code like the guy in this thread. But the person that responded to it responded in a very "unclear" way. (atleast for me as a modeler)
http://answers.unity3d.com/questions/582785/how-to-lock-character-controller-rotation-axis-jav.html
The line of code at the link you provide locks the rotation so that it cannot look up or down. You can use the standard $$anonymous$$ouseLook script 'Axes' to do the same thing.
Answer by Exalia · Jan 20, 2014 at 07:42 PM
Firstly there is a transform.LookAt(); function which you might find useful.
Secondly to fix your problem you will need exempt your y value from your calculation.
Do this by defining a new vector using the Z and X co-ordinates of your target and the Y co-ordinate of your 'looker'
function lookAt ()
{
renderer.material.color = Color.yellow;
var rotation = Quaternion.LookRotation(new Vector3(Target.position.x, transform.position.y,Target.position.z) - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
}
Here's my very basic AI script, be aware of the disclaimer.
/*
* This confidential and proprietary software may be used only as
* authorised by a licensing agreement from ARM Limited
* (C) COPYRIGHT 2014 ARM Limited
* ALL RIGHTS RESERVED
* The entire notice above must be reproduced on all authorised
* copies and copies may only be made to the extent permitted
* by a licensing agreement from ARM Limited.
*/
using UnityEngine;
using System.Collections;
//Class to control the Artificial Intelligence of Enemies
public class AIScript : MonoBehaviour
{
//Public Variables
public GameObject target;
public float moveSpeed;
public float rotationSpeed = 5;
//Private Variables
private float distance;
private float finalmove;
private float finalrot;
private float moveTimer = 2.0f;
private float rotateTimer = 1.0f;
//Used for initilisation
void Start()
{
target = GameObject.FindGameObjectWithTag("Player");
gameObject.animation.Play("Idle");
}
//FixedUpdate is called at a fixed interval
void FixedUpdate ()
{
//Distance between the target and the AI
distance = Vector3.Distance(transform.position, target.transform.position);
//If target is in range
if (distance < 15)
{
//Play Run Animation
gameObject.animation.CrossFade("Run");
//Look at target (Y Exempt)
gameObject.transform.LookAt(new Vector3(target.transform.position.x,gameObject.transform.position.y,target.transform.position.z));
//Transform the AI towards the target (Needs changing to AddForce)
transform.position += transform.forward * moveSpeed * Time.deltaTime;
moveTimer = 2.0f;
}
//If target is out of range
else
{
if(moveTimer <= 0)
{
//Play walk animation
gameObject.animation.CrossFade("Walk");
transform.position += transform.forward * Time.deltaTime;
}
else
{
//Play idle animation untill moveTimer is up
gameObject.animation.CrossFade("Idle");
moveTimer -= Time.deltaTime;
}
if(rotateTimer <= 0)
{
//Dampened Rotatation towards target
gameObject.transform.LookAt(new Vector3(Random.value,gameObject.transform.position.y,Random.value));
rotateTimer = 1.0f;
}
rotateTimer -= Time.deltaTime;
}
}
}
Hope this helps
A legend among legends you are dear sir!
$$anonymous$$ay i ask if you are a professional programmer? :D
Professional no, but it is my current occupation haha, I'm just a student :)
Glad I could help