• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Mafia2Smoke · Jan 20, 2014 at 02:58 PM · playery-axislocking

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) :/

Comment
Add comment · Show 12
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Jan 20, 2014 at 03:14 PM 0
Share

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.

avatar image Mafia2Smoke · Jan 20, 2014 at 03:45 PM 0
Share

Sorry i am using the C# $$anonymous$$ouselook script provided by unity.

avatar image robertbu · Jan 20, 2014 at 05:00 PM 0
Share

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?

avatar image Mafia2Smoke · Jan 20, 2014 at 05:11 PM 0
Share

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

avatar image robertbu · Jan 20, 2014 at 05:27 PM 0
Share

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.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mafia2Smoke · Jan 20, 2014 at 08:34 PM 1
Share

A legend among legends you are dear sir!

$$anonymous$$ay i ask if you are a professional programmer? :D

avatar image Exalia · Jan 20, 2014 at 08:38 PM 0
Share

Professional no, but it is my current occupation haha, I'm just a student :)

Glad I could help

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how to stop something from following in the y axis 1 Answer

Help|MouseLook Y-Axis moving in the wrong direction 1 Answer

How to move the player upwards and forwards at the same time? 1 Answer

Rotation of Camera = Transform on the y-axis? 2 Answers

Look At Target Without Y 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges