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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
3
Question by AtomicMarine · Dec 29, 2010 at 01:31 PM · iosplayerjoystickpenelope

Make Player face where Joystick is pointing?

I am using Unity IOS I have 2 joysticks in my game.

One for moving. and one for aiming.

The one for moving is working fine. But I can't seem to get the one for aiming right, How would I the top half of the player face where the joystick is pointing and preferably stay facing that direction until the joystick is pressed again?

Much like the "alive for ever" game for the iPod and iPhone.

here is the code for the movement joystick I have been trying to fiddle with this code to try and get it to be applicable for the top half of the player, only problem is this code moves the player while making them face in the direction of the joystick.

Thanks in advance :)

@script RequireComponent( CharacterController )

var moveJoystick : Joystick; var cameraTransform : Transform; var speed : float = 6; private var thisTransform : Transform; private var character : CharacterController;

function Start(){ thisTransform = GetComponent( Transform ); character = GetComponent( CharacterController ); }

function FaceMovementDirection(){ var horizontalVelocity : Vector3 = character.velocity; horizontalVelocity.y = 0; if ( horizontalVelocity.magnitude > 0.1) thisTransform.forward = horizontalVelocity.normalized; }

function Update(){

 var movement = cameraTransform.TransformDirection( Vector3(
                 moveJoystick.position.x, 0, moveJoystick.position.y ) );
 movement.y =0;
 movement.Normalize();

 var absJoyPos = Vector2( Mathf.Abs( moveJoystick.position.x ), 
                         Mathf.Abs( moveJoystick.position.y ) );
     movement *= speed * ( ( absJoyPos.x > absJoyPos.y ) ? absJoyPos.x : absJoyPos.y );
     movement *= Time.deltaTime;

     // Actually move the character
     movement += Physics.gravity;
 character.Move( movement );


 FaceMovementDirection();

}

Comment
Add comment · Show 6
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 Jesus_Freak · Dec 29, 2010 at 02:07 PM 0
Share

I'll try, don't have unity iOS or joysticks, so I can't test it, but I'll use logic and try my best.

avatar image Jesus_Freak · Dec 29, 2010 at 03:15 PM 0
Share

since i have no idea about joysticks (im sure you mean the real ones like on console controllers (wii's nunchuck)) i have to play that game to see what you mean...i'm afraid to say i might not be able to help you, but i have to keep my promise and try!

avatar image Jesus_Freak · Dec 29, 2010 at 03:30 PM 1
Share

ahh, i see what you mean, is your character two parts (waist-below, waist above?) becaus ethat would be easy (make one joystick only control movement, and one control the rotation of the upper player and if you want, shooting.) so, is your character(s) two parts?

avatar image AtomicMarine · Jan 04, 2011 at 02:35 PM 0
Share

Sorry for the late reply was at a new years thing out of town. Yes $$anonymous$$y character is two parts :). The IOS joysticks act in the exact same way that a Wii mote joystick would act, and u can also get the position of the joystick as well as its position with reference to its origin. In short yes my character is two parts, and so far i have the bottom part moving and the top part rotating the around in circles when the joystick is moved left or right, but I need the gun to face where the joystick is pointing not just rotate when the joystick is moved, so that is my current problem. Thanks a ton BTW

avatar image AtomicMarine · Jan 04, 2011 at 02:39 PM 0
Share

$$anonymous$$uch like the game Alive 4ever for the iphone/ipod control system

Show more comments

6 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer Wiki

Answer by AtomicMarine · Jan 07, 2011 at 03:22 PM

This is the code i used to solve the problem

I have commented it out heavily so hopefully that helps if you have any other problems just leave a comment

Also this code was done with the IOS so if you are using a different system you may have to adjust

//Author Dominic Obojkovits //Dual Movement Script for TopDown Shooter, or dual Joystick controlled shooter //This script is COPYLEFTED and cannont be copyrighted

@script RequireComponent( CharacterController )

var moveJoystick : Joystick; var cameraTransform : Transform; var speed : float = 6; private var thisTransform : Transform; private var character : CharacterController; var newTransform : Transform; var rotateJoystick : Joystick; var rotationSpeed : Vector2 = Vector2( 50, 50 ); var botTransform : Transform; var targetTrans : Transform;

function Start(){ thisTransform = GetComponent( Transform ); character = GetComponent( CharacterController );

}

function FaceMovementDirection(){

 //This finds the direction that the character is moving in, and will face his Bottom half in that direction
 var horizontalVelocity : Vector3 = character.velocity;
 horizontalVelocity.y = 0;
 if ( horizontalVelocity.magnitude > 0.1)
 botTransform.forward = horizontalVelocity.normalized * 180;

}

function Update(){

     //Code for Moving 
     //The code for moving just moves the player in the direction your left hand joystick is facing
     //but this code is specific to the IOS so if you are using a different system I reccomend something else
     var movement = cameraTransform.TransformDirection( Vector3(
                 moveJoystick.position.x, 0, moveJoystick.position.y ) );
     movement.y =0;
     movement.Normalize();

     var absJoyPos = Vector2( Mathf.Abs( moveJoystick.position.x ), 
                     Mathf.Abs( moveJoystick.position.y ) );
     movement *= speed * ( ( absJoyPos.x > absJoyPos.y ) ? absJoyPos.x : absJoyPos.y );
     movement *= Time.deltaTime;

     // Actually move the character
     movement += Physics.gravity;
     character.Move( movement );

     //called to make the bottom half face the direction he is walking
     FaceMovementDirection();

     //here we make a vector of where your right joystick is
     var targetPos = rotateJoystick.position;

     //With this code we take an empty game object and put it in the direction that your right Joystick is pointing
     //a side not is that I timesed the possition of the object by 150 because 150 is far larger then the field my level is in
     //if u sence is bigger make sure the 150 bellow is larger then your sence to insure the code works
     targetTrans.position = Vector3(targetPos.x*150,1.3,targetPos.y*150);

     //Then we make the Top half of our player face that empty game object
     newTransform.LookAt(targetTrans);

     //this way the player can look in the direction of the Right hand side Joystick
     //and walk in the direction of the Left Hand Side Joystick


}

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
Wiki

Answer by Patrick Neaves · Jan 07, 2011 at 02:49 AM

Can you tell us how? I need to know as well :S

EDIT: Sorry for putting as an answer :/. Can't delete though.

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 AtomicMarine · Jan 07, 2011 at 03:21 PM 0
Share

sure ill put the code up now bellow in an actual answer and np about u putting the answer up :) haha

avatar image
0

Answer by be7a · Apr 08, 2012 at 10:48 PM

Anyone know how i can add a jump to the right joystick and have a character moving with the left joystick as the script above shows, (so not clamping back to the center) i have tried and struggling to put the two together!

Thanks :)

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 juanrodriguez001 · May 09, 2012 at 06:34 PM

I might be wrong on this but I have come to realize that the Joystick connects to the character controller and the character controller makes the character move in whatever direction. Well I had the same problem and it was easily solved by setting up my camera behind my character and adhering to the the following values:

The Joystick : The Actual Character

pos.x = pos.x neg.x = neg.x pos.Y = pos.Z neg.Y = neg.Z

so positive motion on the X in the joystick is equal to positive movement of the character, positive motion on the Y in the joystick = positive motion on the Z in the actual character.

so if your character is facing the right way and your camera is right behind it facing the correct direction you should have no problem using the first person character controller provided by unity and simply moving the camera back a bit. Its silly but the original position of your character and how it's facing is very critical.

regards,

J.

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 jona519b · May 24, 2012 at 03:31 PM

Can anyone tell me how i can change the axis that the player is moving on, im working on a 2d game and would like my player to move up and down on the Y axis, but atm it is moving on the X and Z axis meaning that it walks through my background im using the script from the answer above (Dominic) and then the Joystick from the Penelope tutorial, please help me thank you :)

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 juanrodriguez001 · Jun 02, 2012 at 10:31 PM 0
Share

I work in Blender so when I export to FBX ..Blender allows you to change the "Z" axis to be "Y" in unity. Unity seems to be the only program that considers the "Y" as up. In pretty much all 3D software Packages "z" is up. so before you export make sure to change your "Axis" via the settings of your objects origin. So that when you bring your "object" into unity "z" should be in front of the character as if he/she was looking in the "z" direction.

apply the script and done.

Hope this helps.

  • 1
  • 2
  • ›

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to Fire a projectile using the right joystick(mobile assets) ? 1 Answer

Rotate the Player With Joystick 0 Answers

Dual virtual joysticks to move my character and to move my camera,How do I make a virtual joystick for my camera and character? 0 Answers

How to use the joystick to control the character 3 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