Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 Doomologist · Apr 27, 2013 at 11:02 AM · rotationtilthelicopter

AI Helicopter type Movement

I'm trying to get an AI aircraft to tilt in the direction it is moving and face the player. However I'm running into two problems I can't seem to solve. The first one is that the if statements aren't checking the increments correctly, so sometimes it will tilt left but will also tilt left if it's moving right. I'm not sure how to check for the direction it is moving in otherwise. The other problem is when I add in the lookAt function the rotations for tilt don't work at all. I also don't like the way it instantly moves to always look at the player, it should move slowly.

This is for a top down scrolling game where the environment moves beneath to give the illusion of speed.

 #pragma strict
 
 var player : Transform;
 var distance : float;
 var rotationLeft = Quaternion.Euler(0, 0, -30);
 var rotationRight = Quaternion.Euler(0, 0, 30);
 var rotationForward = Quaternion.Euler(-30, 0, 0);
 var rotationBackward = Quaternion.Euler(30, 0, 0);
 var rotationCenter = Quaternion.Euler(0, 0, 0);
 
 
 var moveRightLeft : float; 
 var moveForwardBack : float; 
 
 
 
 
 
 function Update () {
 
 
     distance = Vector3.Distance(transform.position, player.position);
     moveRightLeft = transform.position.x;
     moveForwardBack = transform.position.z;
 
     
 
     
     if((player != null) && (distance >= 45)){
     transform.position = Vector3.Lerp(transform.position, player.position, Time.deltaTime * 0.4);
     transform.rotation = Quaternion.Slerp(transform.rotation, rotationForward, Time.deltaTime * 4);
     }
     else{
     transform.rotation = Quaternion.Slerp(transform.rotation, rotationCenter, Time.deltaTime * 4);
     }
     
     
     if((moveRightLeft ++) && (distance >= 45)){
     transform.rotation = Quaternion.Slerp(transform.rotation, rotationLeft, Time.deltaTime * 4);
     }
     
     if((moveRightLeft --) && (distance >= 45)){
     transform.rotation = Quaternion.Slerp(transform.rotation, rotationRight, Time.deltaTime * 4);
     }
             
     //var targetPosition : Vector3 = Vector3(player.position.x, player.position.y, 0);
     //transform.LookAt(targetPosition);
     
          
 }
Comment
Add comment · Show 2
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 Fattie · Apr 27, 2013 at 11:38 AM 0
Share

Your code is incredibly strange.

moveRightLeft++ will actually increment that value, after you check (if it is zero ??)

You are a long ways from code that will work, but I suggest (to begin with) learn to write "break away" code which is simpler. So along the lines ..

if player is null .. return

so that's out of the way

next,

if distance > 45 ... do nothing, print a Debug.Log statements, and return

so you can see that case is out of the way

And so on .. carry on like that

avatar image Doomologist · Apr 27, 2013 at 05:34 PM 0
Share

Thanks. I need a lot more help though. I know my code is strange (: not sure what "break away" code means, but that null is to check whether the player has blown up, so it only moves when the player is alive. It also moves until it is 45 units away so that the enemy doesn't get right up on the player aircraft, it keeps some distance between the the two.

How do I check if a value increments or decrements and then "Do something" I've been commenting out code and checking if each works using Debug.Log

0 Replies

· Add your reply
  • Sort: 

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.

Follow this Question

Answers Answers and Comments

13 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

Related Questions

Rotation without tilt around a globe. 1 Answer

Bank Camera Based On Rotation Delta 1 Answer

Slight Problem in the Camera Rotation Script 0 Answers

Smooth tilt accelerometer 1 Answer

Gun tilting in the direction you look towards 1 Answer


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