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 Wes 1 · Nov 22, 2010 at 01:27 AM · vector3controllerrotate

How to make object lean in the direction it's going

I've got a space ship and I'm going for a starfox type look, so that when you move left or right it leans one way or the other. This is what I have to so far

var speed : float = 20.0; var rotatespeed : float = 10.0; var target : Transform;

 function Update () {
 var controller : CharacterController = GetComponent(CharacterController);
 //you can go up or down
 transform.Translate(Input.GetAxis("Horizontal") *Time.deltaTime * speed, -Input.GetAxis("Vertical") * Time.deltaTime * 0, 0);

 //always moving forward
 transform.Translate(0,0,1 * Time.deltaTime);


 //lean left right, up down 
 transform.Rotate(0, Input.GetAxis ("Horizontal") * 20*Time.deltaTime, 0);
 transform.Rotate(Input.GetAxis ("Vertical") * 30 * Time.deltaTime, 0,0);

}

I'm okay with it continually rotating left and right so they can turn I think, but if you hold up or down the ship just continues to rotate all the way around (lol) I was thinking about using an if statement to tell it to stop at a certain degree but Im not sure how to go about that, If(rotateamount = this much) stop rotating; ???

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Uriel_96 · Nov 22, 2010 at 03:13 AM

OK this is a script that I usually use to solve this

if(stop == false){
if(Input.GetKey(KeyCode.RightArrow)){
transform.Rotate(transform.up * Time.deltaTime * rotateSpeed, Space.World);
}
//you can add here the left
}
if(Input.GetKey(KeyCode.UpArrow)){
stop = true;
transform.Translate(transform.forward * Time.deltaTime * speed,Space.World);
}else{
if(Input.GetKeyUp(KeyCode.UpArrow)){
stop = false;
}
//You can add here the backawrd
Comment
Add comment · Show 1 · 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 Wes 1 · Nov 23, 2010 at 07:33 AM 0
Share

Ohhh, I see, that script is for increasing forward speed! I thought it was for rotate! I just altered it to rotate. That's what I was asking about is having the ship tilt a little in the direction it's going. That's a nice increase speed script though :)

avatar image
0

Answer by Design3.com · Dec 16, 2010 at 03:58 AM

You might be able to use Mathf.Clamp to clamp the eulerAngles?

function Upate() {
    transform.Rotate(0, Input.GetAxis ("Horizontal") * 20*Time.deltaTime, 0);
    transform.eulerAngles.y = Mathf.Clamp(transform.eulerAngles.y,-30,30);
}
Comment
Add comment · 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
0

Answer by psychentist · Apr 20, 2011 at 05:29 AM

you could also attach it to a configurable joint with min/max rotations. this might be easier to work with.

Comment
Add comment · 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

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

No one has followed this question yet.

Related Questions

How do I make gameObject.transform.rotation.z equal to a set float value? 2 Answers

Unity 8-Directional Locked Movement (Super Mario 3D World Style) 3 Answers

how to rotate GameObject towards joystick 1 Answer

Rotating wrong 0 Answers

Rotate towards target with limitation on certain axis? c# 2 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