- Home /
Duplicate Question
make my object face the direction of movement?
I have these two classes here, given to me as part of a project:
using UnityEngine;
using System.Collections;
public class Ball: MonoBehaviour {
public float moveSpeed = 30;
void Update () {
//var dir: Vector3 = Vector3.zero;
CharacterController controller = GetComponent<CharacterController>();
Vector3 moveDirection = Vector3.zero;
moveDirection += Camera.main.transform.up * Input.GetAxis("Vertical");
moveDirection += Camera.main.transform.right * Input.GetAxis("Horizontal");
moveDirection.z = 0;
controller.Move(moveDirection * moveSpeed * Time.deltaTime);
}
}
and this one
using UnityEngine;
using System.Collections;
public class GamePad : MonoBehaviour {
public float speed = 40;
// Update is called once per frame
void Update () {
transform.Translate (Input.GetAxis("Horizontal")*Time.deltaTime*speed,
Input.GetAxis ("Vertical")*Time.deltaTime*speed,
0);
}
}
these two I'm almost certain control movement along the X and Y axis. I have a sphere slightly streched and I want it to rotate it to the direction it is moving. Any ideas? Thanks!
Answer by robertbu · May 10, 2013 at 01:08 AM
In order to make your object face the direction you need to:
Create your stretch sphere on the Z axis (i.e. the head and tail need to be on the Z axis). If you are creating it from a standard sphere, just increase the size of the Z scale, or decrease the size of the x and y scale. In Unity, positive 'Z' is forward (which is where you want the the head).
Add the two lines of code at the bottom of this post at line 18 in the first script.
Note if you want a smoother rotation, take a look at the answer I posted for your other question (here)...the few lines posted by @runevision just after the bold 'Edit' are an implementation of a smoothed rotation that build on the two lines of code below.
if (moveDirection != Vector3.zero)
transform.rotation = Quaternion.LookRotation(moveDirection);
this is the code after adding your two lines
using UnityEngine;
using System.Collections;
public class Ball: $$anonymous$$onoBehaviour {
public float moveSpeed = 30;
void Update () {
CharacterController controller = GetComponent<CharacterController>();
Vector3 moveDirection = Vector3.zero;
moveDirection += Camera.main.transform.up * Input.GetAxis("Vertical");
moveDirection += Camera.main.transform.right * Input.GetAxis("Horizontal");
moveDirection.z = 0;
if (moveDirection.sqr$$anonymous$$agnitude != Vector3.zero)
{
transform.rotation = Quaternion.LookRotation(moveDirection);
}
controller.$$anonymous$$ove(moveDirection * moveSpeed * Time.deltaTime);
}
}
However, I'm getting an error that says on the line of the if(moveDirection...) statement you gave me that says Assets/Ball.cs(16,27): error CS0019: Operator !=' cannot be applied to operands of type
float' and `UnityEngine.Vector3'
I'm sorry, I changed that at the last moment. You can do it one of two ways. You can say:
if (moveDirection != Vector3.zero)
or
if (moveDirection.sqr$$anonymous$$agnitude < some_small_value)
I think the first statement is good enough and likely better, but when I ran a quick test of the concept, I used the second version. I
Follow this Question
Related Questions
Lock rotation axis? 4 Answers
Rotating Toward an Object at a Constant Speed 2 Answers
hoe to rotate a AI as a animation 1 Answer
Rotate to a certain position 2 Answers
Merge a rotation(vector3) to a lookAt(quaternion) ? 1 Answer