- Home /
Trying to move a sphere in the direction of the camera and can't seem to get anything to work.,Trying to move a sphere in the direction of the camera
I'm new to unity and trying to get the forward motion to be local positioning not world positioning. Others have noted that you should get the forward motion from the position of the camera not the sphere. Any help would be greatly appreciated. My character will be rolling in this game and right now, I can't do any turns without problems.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerController : MonoBehaviour {
public float speed = 20;
private Rigidbody rig;
public LayerMask groundLayers;
public float jumpForce = 7;
public SphereCollider col;
// Start is called before the first frame update
void Start()
{
rig = GetComponent<Rigidbody>();
col = GetComponent<SphereCollider>();
}
// Update is called once per frame
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rig.AddForce (movement * speed);
if(IsGrounded() && Input.GetKeyDown(KeyCode.Space))
{
rig.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
}
private bool IsGrounded()
{
return Physics.CheckCapsule(col.bounds.center, new Vector3(col.bounds.center.x,
col.bounds.min.y, col.bounds.min.z), col.radius * .9f, groundLayers);
}
},I've seen a few instances of this on this forum but still cant figure out how to implement it into my script. Right now forward is world position and I need it to be local position.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerController : MonoBehaviour {
public float speed = 20;
private Rigidbody rig;
public LayerMask groundLayers;
public float jumpForce = 7;
public SphereCollider col;
// Start is called before the first frame update
void Start()
{
rig = GetComponent<Rigidbody>();
col = GetComponent<SphereCollider>();
}
// Update is called once per frame
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rig.AddForce (movement * speed);
if(IsGrounded() && Input.GetKeyDown(KeyCode.Space))
{
rig.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
}
private bool IsGrounded()
{
return Physics.CheckCapsule(col.bounds.center, new Vector3(col.bounds.center.x,
col.bounds.min.y, col.bounds.min.z), col.radius * .9f, groundLayers);
}
}
I should note I'm using Cinemachine virtual camera to move it with the player.
Your answer
![](https://koobas.hobune.stream/wayback/20220612235139im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Cannot resolve unity container 2 Answers
OntriggerEnter acting weird 1 Answer
Unity freezes when I try to click play 1 Answer
How do I stop moving when attacking? 1 Answer
How to change RawImage texture to another one when i press a buton for it 0 Answers