- Home /
Im trying to make a 3d snake game, how do i move my character 90 degrees and then make it move in that direction
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class movement : MonoBehaviour { public Rigidbody rb; public float Force = 500f;
void Start()
{
Rigidbody rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
rb.AddForce(0, 0, Force * Time.deltaTime);
if (Input.GetKey("a"))
{
transform.Rotate(0, -90, 0);
}
if (Input.GetKey("d"))
{
transform.Rotate(0, 90, 0);
}
}
}
Answer by Waterpolo7 · Feb 18, 2021 at 04:21 PM
Well, your rotating is done correctly, so you should now just move the character with transform.forward;
void Start()
{
Rigidbody rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
gameobject.transform.Translate(transform.forward * Force * Time.deltaTime);
if (Input.GetKeyDown("a"))
{
transform.Rotate(0, -90, 0);
}
if (Input.GetKeyDown("d"))
{
transform.Rotate(0, 90, 0);
}
}
You were moving the snake by the z-axis, which doesn't change when the snake is facing another way.
Hope this works ;)
Just wanted to add that you probably want to use GetKeyDown instead of GetKey
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Scrollview can't scroll with buttons that have Event Triggers 0 Answers
How can you make an object visible in Unity when you jump on it? 1 Answer
Does anybody know how to fix (repositories.cfg could not be loaded) problem? 3 Answers