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 Hims · May 23, 2013 at 12:07 PM · cameramovecharacter controllerforward

move character controller forward using gui button.

i am using the following script to control the character controller camera rotation (up,down,left , right).

i want to move my character forward and backward using gui button.

my character controller is not moving in my camera direction.

following is the code. please check out the commented part of the script and let me know where and what is wrong in the script!!!

 //WASD to orbit, left Ctrl/Alt to zoom
 using UnityEngine;
 using System.Collections;
 
 [AddComponentMenu("Camera-Control/Keyboard Orbit")]
 
 public class camerarottest: MonoBehaviour {
    // public Transform target;
    // public float distance = 20.0f;
     private Vector3 moveDirection = Vector3.zero;
     
     public float zoomSpd = 2.0f;
     public float walkspeed = 3.0f;
 
     public float xSpeed = 240.0f;
     public float ySpeed = 123.0f;
 
     public int yMinLimit = -723;
     public int yMaxLimit = 877;
 
     public static float x = 0.0f;
     private float y = 0.0f;
     
     private float GUIHorizontal = 0;
     private float GUIVertical = 0;
     
     public GUIStyle uptexture;
     public GUIStyle lefttexture;
     public GUIStyle righttexture;
     public GUIStyle downtexture;
     //public GUIStyle Zoomintexture;
     //public GUIStyle Zoomouttexture;
     //public GUIStyle reset;
     public Texture2D camerabox;
     
     public void Start () {
        Vector3 angles = transform.eulerAngles;
         x = angles.y;
         y = angles.x;
         //print(angles);
         // Make the rigid body not change rotation
         if (rigidbody)
             rigidbody.freezeRotation = true;
     }
 
 void OnGUI()
 {
 
     if(Event.current.type == EventType.Repaint)
     {
         GUIHorizontal = 0;
         GUIVertical = 0;
     }
     GUI.BeginGroup(new Rect(182,80, 800, 600));
 
     GUI.DrawTexture(new Rect(700,430,78,79),camerabox);
 
     GUILayout.BeginVertical();
     
     if(GUI.RepeatButton(new Rect(710,430,56,29),"",uptexture))
     {
         GUIVertical = -1;
     }
     GUILayout.BeginHorizontal();
     if(GUI.RepeatButton(new Rect(700,443, 29, 55),"", lefttexture))
     {
         GUIHorizontal = 1;
     }
     if(GUI.RepeatButton(new Rect(750,443, 30, 55),"", righttexture))
     {
         GUIHorizontal = -1;
     }
     GUILayout.EndHorizontal();
     if(GUI.RepeatButton(new Rect(710,480, 56, 30),"", downtexture))
     {
         GUIVertical = 1;
     }
     GUILayout.EndVertical();
     
         if(GUI.RepeatButton(new Rect(725,515, 27, 27),"",moveForward))
     {
        //this part is not giving any error but not working as expectation.
        //i want to move the character controller camera forward when this button s clicked.
            CharacterController controller = GetComponent<CharacterController>();
         
         moveDirection = transform.forward;
             
         moveDirection =  transform.Translate (transform.forward * 2.0f * Time.deltaTime, Space.Self);
 
          moveDirection = transform.TransformDirection(moveDirection);
             
         controller.Move(moveDirection * Time.deltaTime);
     
             
         /*    CharacterController controller = GetComponent<CharacterController>();
     if (controller.isGrounded) {
  
         moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,0);
         
                  if (moveDirection != Vector3.zero) {
             Vector3 rotation = transform.rotation; 
             rotation.SetLookRotation(moveDirection); 
             transform.rotation = rotation;
         }
         
           moveDirection = transform.TransformDirection(moveDirection);
            // moveDirection *= speed;
             }
             
             controller.Move(moveDirection * Time.deltaTime);
             
         */    
     }
     if(GUI.RepeatButton(new Rect(725,550, 27, 27),"",moveBackward))
     {
         CharacterController controller = GetComponent<CharacterController>();
         moveDirection = -transform.forward;
         moveDirection =  transform.Translate (transform.forward * 2.0f * Time.deltaTime, Space.Self);
         moveDirection = transform.TransformDirection(moveDirection);
         controller.Move(moveDirection * Time.deltaTime);
     }
         
     
     
     GUI.EndGroup();
 }
 
 
     public void Update () {
       
             
             x -= Input.GetAxis("Horizontal") * xSpeed * 0.02f;
             y += Input.GetAxis("Verticalnew") * ySpeed * 0.02f;
             
             
             x -= GUIHorizontal * xSpeed * 0.02f;
             y += GUIVertical * ySpeed * 0.02f;
             
             y = ClampAngle(y, yMinLimit, yMaxLimit);
       
             Quaternion rotation = Quaternion.Euler(y, x, 0.0f);
            
             transform.rotation = rotation;
           
         
     }
     public static float ClampAngle (float angle, float min, float max) {
         if (angle < -360.0f)
             angle += 360.0f;
         if (angle > 360.0f)
             angle -= 360.0f;
         return Mathf.Clamp (angle, min, max);
     
     }
 }
 







Comment
Add comment · Show 1
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 Hims · May 24, 2013 at 06:05 AM 0
Share

Solved by myself

var cameraObject : GameObject; var curSpeed : float = 3.0f;

change this:

controller.$$anonymous$$ove(forward Time.deltatime curSpeed); to:

controller.$$anonymous$$ove(cameraObject.transform.forward * curSpeed);

and assign the camera you want to use to the variable in the inspector :) hope this helps to whom who wants to move character controller using gui button :)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Hims · May 24, 2013 at 06:05 AM

Solved by myself

var cameraObject : GameObject; var curSpeed : float = 3.0f;

change this:

controller.Move(forward Time.deltatime curSpeed); to:

controller.Move(cameraObject.transform.forward * curSpeed);

and assign the camera you want to use to the variable in the inspector :) hope this helps to whom who wants to move character controller using gui button :)

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

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

Strange issue, movement direction has been reversed when X axis is negative 0 Answers

Simple moving Cam in direction its facing 2 Answers

Change forward control on mouse rotation 0 Answers

camera move on unity 1 Answer

How would I make it so that my character moves only so fast 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