• 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 a81261742 · Oct 06, 2020 at 10:17 AM · 2d

,Unity2D while you clicking , the object is rotating

i have a game,this is main character script

 using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
     using UnityEngine.UI;
 
 [RequireComponent(typeof(Rigidbody2D))]
 [RequireComponent(typeof(CapsuleCollider2D))]
 
 public class CharacterController2D : MonoBehaviour
 {
 // Move player in 2D space
 public float maxSpeed = 3.4f;
 public float jumpHeight = 6.5f;
 public float gravityScale = 1.5f;
 public Camera mainCamera;
 public float residueOil = 250f; //остаток бензина
 public Slider slider;
 
 
 
 
 bool facingRight = true;
 float moveDirection = 0;
 bool isGrounded = false;
 Vector3 cameraPos;
 Rigidbody2D r2d;
 Collider2D mainCollider;
 // Check every collider except Player and Ignore Raycast
 LayerMask layerMask = ~(1 << 2 | 1 << 8);
 Transform t;
 
 // Use this for initialization
 void Start()
 {
 t = transform;
 r2d = GetComponent();
 mainCollider = GetComponent();
 r2d.freezeRotation = true;
 r2d.collisionDetectionMode = CollisionDetectionMode2D.Continuous;
 r2d.gravityScale = gravityScale;
 facingRight = t.localScale.x > 0;
 gameObject.layer = 8;
 
 if(mainCamera)
 cameraPos = mainCamera.transform.position;
 slider.value = 250;
 
 }
 
 // Update is called once per frame
 void Update()
 {
 moveDirection = 1;
 
 
 
 
 
 
 // Change facing direction
 if (moveDirection != 0)
 {
 if (moveDirection > 0 && !facingRight)
 {
 facingRight = true;
 t.localScale = new Vector3(Mathf.Abs(t.localScale.x), t.localScale.y, transform.localScale.z);
 
 }
 
 }
 
 // Jumping
 if (Input.GetKey("space") && residueOil > 0f) //проверяем и нажатие и остаток бензина
 {
 residueOil -= jumpHeight; // уменьшаем остаток бензина на величину подъема
 r2d.velocity = new Vector2(r2d.velocity.x, jumpHeight); //двигаемся вверх
 slider.value = residueOil;
 
 }
 
 // Camera follow
 if(mainCamera)
 mainCamera.transform.position = new Vector3(t.position.x, cameraPos.y, cameraPos.z);
 }
 
 void FixedUpdate()
 {
 Bounds colliderBounds = mainCollider.bounds;
 Vector3 groundCheckPos = colliderBounds.min + new Vector3(colliderBounds.size.x * 0.5f, 0.1f, 0);
 // Check if player is grounded
 isGrounded = Physics2D.OverlapCircle(groundCheckPos, 0.23f, layerMask);
 
 // Apply movement velocity
 r2d.velocity = new Vector2((moveDirection) * maxSpeed, r2d.velocity.y);
 
 // Simple debug
 Debug.DrawLine(groundCheckPos, groundCheckPos - new Vector3(0, 0.23f, 0), isGrounded ? Color.green : Color.red);
 }
 }


This is an airplane,it keeps moving to the right. And it also have a oil bar.When you click on the space bar, it gains height.How to make it so that when you click on the space bar,he lifted his nose up,and if you do nothing,so that he lowered his nose down.Thanks :) ,i have a game,this is main character script

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 [RequireComponent(typeof(Rigidbody2D))]
 [RequireComponent(typeof(CapsuleCollider2D))]
 
 public class CharacterController2D : MonoBehaviour
 {
 // Move player in 2D space
 public float maxSpeed = 3.4f;
 public float jumpHeight = 6.5f;
 public float gravityScale = 1.5f;
 public Camera mainCamera;
 public float residueOil = 250f; //остаток бензина
 public Slider slider;
 
 
 
 
 bool facingRight = true;
 float moveDirection = 0;
 bool isGrounded = false;
 Vector3 cameraPos;
 Rigidbody2D r2d;
 Collider2D mainCollider;
 // Check every collider except Player and Ignore Raycast
 LayerMask layerMask = ~(1 << 2 | 1 << 8);
 Transform t;
 
 // Use this for initialization
 void Start()
 {
 t = transform;
 r2d = GetComponent();
 mainCollider = GetComponent();
 r2d.freezeRotation = true;
 r2d.collisionDetectionMode = CollisionDetectionMode2D.Continuous;
 r2d.gravityScale = gravityScale;
 facingRight = t.localScale.x > 0;
 gameObject.layer = 8;
 
 if(mainCamera)
 cameraPos = mainCamera.transform.position;
 slider.value = 250;
 
 }
 
 // Update is called once per frame
 void Update()
 {
 moveDirection = 1;
 
 
 
 
 
 
 // Change facing direction
 if (moveDirection != 0)
 {
 if (moveDirection > 0 && !facingRight)
 {
 facingRight = true;
 t.localScale = new Vector3(Mathf.Abs(t.localScale.x), t.localScale.y, transform.localScale.z);
 
 }
 
 }
 
 // Jumping
 if (Input.GetKey("space") && residueOil > 0f) //проверяем и нажатие и остаток бензина
 {
 residueOil -= jumpHeight; // уменьшаем остаток бензина на величину подъема
 r2d.velocity = new Vector2(r2d.velocity.x, jumpHeight); //двигаемся вверх
 slider.value = residueOil;
 
 }
 
 // Camera follow
 if(mainCamera)
 mainCamera.transform.position = new Vector3(t.position.x, cameraPos.y, cameraPos.z);
 }
 
 void FixedUpdate()
 {
 Bounds colliderBounds = mainCollider.bounds;
 Vector3 groundCheckPos = colliderBounds.min + new Vector3(colliderBounds.size.x * 0.5f, 0.1f, 0);
 // Check if player is grounded
 isGrounded = Physics2D.OverlapCircle(groundCheckPos, 0.23f, layerMask);
 
 // Apply movement velocity
 r2d.velocity = new Vector2((moveDirection) * maxSpeed, r2d.velocity.y);
 
 // Simple debug
 Debug.DrawLine(groundCheckPos, groundCheckPos - new Vector3(0, 0.23f, 0), isGrounded ? Color.green : Color.red);
 }
 }

This is an airplane,it keeps moving to the right. And it also have a oil bar.When you click on the space bar, it gains height.How to make it so that when you click on the space bar,he lifted his nose up,and if you do nothing,so that he lowered his nose down.Thanks :)is is main character script

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

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.

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

294 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D Animation does not start 1 Answer

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

Having problems getting a zoom to mouse function working well with smoothstep, need some help. 0 Answers

using unity for a true 2D game 3 Answers

Trail Render not become visible during game play 0 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