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 Parsa99 · Oct 10, 2013 at 07:54 PM · rotationplayermovecube

special move with a cube.

I wona to move a cube with rotate,So that the move is due to the rotation. like this image: alt text

my code is :

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour {
     
     
     public float speed = 6.0F;
     public float jumpSpeed = 8.0F;
     public float gravity = 20.0F;
     private Vector3 moveDirection = Vector3.zero;
     
     // Use this for initialization
     void Start () {
     
     }
     void Update() {
         CharacterController controller = GetComponent<CharacterController>();
         if (controller.isGrounded) {
             float mv = Input.GetAxis("Horizontal");
             transform.Rotate(Vector3.forward,-mv);
             moveDirection = new Vector3(mv* Time.deltaTime, mv* Time.deltaTime, 0);
             moveDirection = transform.TransformDirection(moveDirection);
             moveDirection *= speed;
             if (Input.GetButton("Jump"))
                 moveDirection.y = jumpSpeed;
             
         }
         moveDirection.y -= gravity * Time.deltaTime;
         controller.Move(moveDirection * Time.deltaTime);
     }
 }

how I can do it? when when player has been rotate the Axis is not work move to forward.

graphic81.png (18.1 kB)
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 robertbu · Oct 10, 2013 at 08:43 PM 0
Share

Using a character controller I cannot see any way to do what you want without faking it. That is, you could rotate the cube and move it up and down either using a formula or using raycasting so that it appeared to roll forward, but there would be no real physics involved.

Or you could make the cube into a Rigidbody and use AddRelativeTorque() to rotate the cube. A Rigidbody solution would have its own challenges...especially in that you would have to keep the local 'x' axis of the cube relatively parallel to the ground in order for the object to not to tip over and get stuck.

avatar image Parsa99 · Oct 10, 2013 at 09:08 PM 0
Share

Can you give me a sample?

1 Reply

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

Answer by robertbu · Oct 10, 2013 at 10:29 PM

Here is a bit of source that, using a Rigidbody, will roll a cube across a plane using the arrow keys. To Use:

  • Create a plane

  • Put a cube on top of the plane

  • Add a Rigidbody to the plane

  • Add the script

 #pragma strict
 
 private var forceAmount = 40.0;
 private var rotationSpeed = 45.0;
 
 function FixedUpdate() {
     transform.Rotate(0.0, rotationSpeed * Time.fixedDeltaTime * Input.GetAxis("Horizontal"), 0.0, Space.World);
      rigidbody.AddRelativeTorque(Vector3(forceAmount * Input.GetAxis("Vertical"), 0.0, 0.0));
 }

Note I had "Horizontal" and "Vertical" setup to use keys, not a joystick or mouse movement when I ran a brief test of this code. For general use, more code would be needed to address specific issues with the interaction of the cube and the physical environment.

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

15 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

Related Questions

Moving and rolling a cube 1 Answer

collider then change rotation converse? 0 Answers

orienting player in one direction 1 Answer

how to tell a game object to move between 2 points(A,B) along x axis , right to left, and then move left to right back to its first position ??? 3 Answers

Directional Movement - Input position calculation? 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