I know this has been asked but can't figure out how to solve this.
if (this.gameObject.tag == "1Team"){
unitGO.transform.position += transform.right * movementSpeed * Time.deltaTime;
} else if (this.gameObject.tag == "2Team"){
unitGO.transform.position += transform.left * movementSpeed * Time.deltaTime;
}
Unity marks in red transform.left ( Transform' does not contain a definition for 'left' ), so I dont know how to move my character to the left. I have tried using -Transform.right, Transform.right * -movementSpeed, new Vector3( - 1,0,0), unitGO.transform.position -= new Vector3(1,0,0) * movementSpeed * Time.deltaTime; and other options without getting the movement I want.
if I change the tag from the GameObject I want to move, it actually moves right, so I dont think it's anything related to attaching scripts
https://docs.unity3d.com/ScriptReference/Vector3-left.html
Any suggestions?
Thank you
While Vector3.left exists, for Transform there is only transform.right.
Which is no problem since for moving left you simply use -transform.right.
Now note that your intempts of using new Vector3 also didn't work since if you use that you move in global space along Unity's X axis .. not in local space.
Now there are multiple possible solutions:
For moving in your local space do e.g.
if (gameObject.CompareTag("1Team"))
{
unitGO.transform.position += transform.right * movementSpeed * Time.deltaTime;
}
else if (gameObject.CompareTag("2Team"))
{
unitGO.transform.position -= transform.right * movementSpeed * Time.deltaTime;
// Same as
//unitGO.transform.position += -transform.right * movementSpeed * Time.deltaTime;
}
Or if you wan to move in Unity's global X axis then
if (gameObject.CompareTag("1Team"))
{
unitGO.transform.position += Vector3.right * movementSpeed * Time.deltaTime;
}
else if (gameObject.CompareTag("2Team"))
{
unitGO.transform.position += Vector3.left * movementSpeed * Time.deltaTime;
// Same as
//unitGO.transform.position -= Vector3.right * movementSpeed * Time.deltaTime;
}
Or if you actually rather wanted to move along the local X axis of the object you are moving you could rather use Translate which by default uses the local space of the moved object
if (gameObject.CompareTag("1Team"))
{
unitGO.transform.Translate(Vector3.right * movementSpeed * Time.deltaTime);
}
else if (gameObject.CompareTag("2Team"))
{
unitGO.transform.Translate(Vector3.left * movementSpeed * Time.deltaTime);
// Same as
//unitGO.transform.Translate(-Vector3.right * movementSpeed * Time.deltaTime);
}
In general rather use CompareTag instead of == to throw an error if there is a typo
Related
void UpdateMovement()
{
Vector2 targetDir = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
targetDir.Normalize();
currentDir = Vector2.SmoothDamp(currentDir, targetDir, ref currentDirVelocity, moveSmoothTime);
if (controller.isGrounded)
{
velocityY = -gravity;
}
// jump
if (controller.isGrounded && Input.GetKeyDown(KeyCode.Space))
{
velocityY = jumpForce;
}
// sprint
if (Input.GetKey(KeyCode.LeftShift))
{
velocity = (transform.forward * currentDir.y + transform.right * currentDir.x) * sprintSpeed + Vector3.up * velocityY;
}
else
{
velocity = (transform.forward * currentDir.y + transform.right * currentDir.x) * walkSpeed + Vector3.up * velocityY;
}
velocityY -= gravity * Time.deltaTime;
Debug.Log(velocityY);
controller.Move(velocity * Time.deltaTime);
}
This is the code that I am using to move and jump in my game. Those functions work fine, the problem is in the gravity when walking off a surface. When jumping gravity works as intended but when falling without jumping (walking off the edge) gravity is still set to it's default value (15) so the characters falls incredibly fast.
I understand that this is caused by having no function to change this value when falling from an edge but I have found no way of making this happen without breaking all of the vertical movement.
You can reset the gravity as Brackeys did in his FPS Controller Tutorial:
if (isGrounded) {
velocityY = -2f;
}
How can I shorten this code and avoid copying, what do you advise?
The method is used to move the camera in space using buttons
private void HandleMovementInput() {
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
{
new_position += (transform.forward * movement_speed);
}
if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
{
new_position += (transform.forward * -movement_speed);
}
if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
{
new_position += (transform.right * movement_speed);
}
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
{
new_position += (transform.right * -movement_speed);
}
if (Input.GetKey(KeyCode.Q))
{
new_rotation *= Quaternion.Euler(Vector3.up * rotation_amount);
}
if (Input.GetKey(KeyCode.E))
{
new_rotation *= Quaternion.Euler(Vector3.up * -rotation_amount);
}
//Shit code for zoom
if (Input.GetKey(KeyCode.R))
{
new_zoom += zoom_amount;
}
if (Input.GetKey(KeyCode.F))
{
new_zoom -= zoom_amount;
}
transform.position = Vector3.Lerp(transform.position, new_position, Time.deltaTime * movement_time);
transform.rotation = Quaternion.Lerp(transform.rotation, new_rotation, Time.deltaTime * movement_time);
camera_transform.localPosition = Vector3.Lerp(camera_transform.localPosition,new_zoom, Time.deltaTime * movement_time);
}
Unfortunately, I'm not so strong in Unity to solve this architectural case.
Try using Input.Getaxis.
https://docs.unity3d.com/ScriptReference/Input.GetAxis.html
I used this code for my player movement script
//playerinput is a Vector3 variable
playerinput = new Vector3(Input.GetAxis("Horizontal"), 0f,
Input.GetAxis("Vertical"));
//transform.TransformDirection is here to move the player depending in their rotation
//playerinput.normalized is there for stopping strafing(for example, holding w and s together makes the player faster)
//I don't think you should change Mathf.Clamp01(playerinput.magnitude). This is very important for the movement to look good.
Vector3 movevector = transform.TransformDirection(playerinput.normalized * Mathf.Clamp01(playerinput.magnitude)) * speed;
//rb is the rigidbody by the way. Add rigidbody component to your camera
rb.velocity = new Vector3(movevector.x, rb.velocity.y, movevector.z);
This will do the trick, you can remove some lines of code to fit your game. Hope this helped!
float distance1 = Vector3.Distance(transform.position, target.position);
if(distance1 < 10)
{
movementSpeed -= 0.01f;
}
transform.position = Vector3.MoveTowards(transform.position, target.position, Time.deltaTime * movementSpeed);
If for example I did it this way it will increase the movement speed slowly :
movementSpeed += Time.deltaTime * 0.01f;
transform.position = Vector3.MoveTowards(transform.position, destinationTransform.position, Time.deltaTime * movementSpeed);
but i want the object to slowly decrease the movement speed down when getting close to the target.
when i'm changing it to -= the object is moving backward.
I have tried to make a Player Controller where you control it via WSAD or the arrow keys and it worked fine, but when I tried to add the part that registers the mouse movements on the X Y axes it went wrong...
Input.GetAxis ("Mouse X") worked fine but Input.GetAxis ("Mouse Y") only returns zero no matter what I do... I have tried checking inside the Input Manager and there was nothing wrong, nor anything they're missing and I tried to Clear All PlayerPrefs and that did not work and I have also tried to restart my whole PC but it did not work either...
I do not know what to do and I have tried everything but without luck
If there are any of you who can help me it will be a great help
Thanks
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Controller : MonoBehaviour
{
private Rigidbody RB;
public int Speed = 250, JumpPawer = 250, RotationSpeed = 20, MaxAngle = 130;
private Animator A;
public GameObject Came;
// Start is called before the first frame update
void Start()
{
RB = GetComponent<Rigidbody>();
A = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
//rotation
transform.eulerAngles += new Vector3(0, Input.GetAxis("Mouse X"), 0) * Time.deltaTime * RotationSpeed;
if (Came.transform.eulerAngles.x + (Input.GetAxis("Mouse Y") * Time.deltaTime * RotationSpeed) < MaxAngle) {
print(Input.GetAxis("Mouse Y"));
Came.transform.eulerAngles += new Vector3(Input.GetAxis("Mouse Y"), 0, 0) * Time.deltaTime * RotationSpeed;
}
//controls
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow)) {
RB.velocity = transform.forward * Time.deltaTime * Speed;
A.SetInteger("A", 1);
}
else if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow)) {
RB.velocity = -transform.forward * Time.deltaTime * Speed;
A.SetInteger("A", 1);
}
else if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) {
RB.velocity = -transform.right * Time.deltaTime * Speed;
A.SetInteger("A", 2);
}
else if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) {
RB.velocity = transform.right * Time.deltaTime * Speed;
A.SetInteger("A", 3);
}
else if (Input.GetKeyDown(KeyCode.Space)) {
RB.velocity = Vector3.up * Time.deltaTime * JumpPawer;
}
else
A.SetInteger("A", 0);
}
}
Unless you changed the input manager’s settings, it isn’t a problem with the actual input. You could make sure the problem isn’t with the input system, by typing this (make sure it is out of the if statement you wrote before):
print(Input.GetAxis(“Mouse Y”);
If you do this and test the game, move your mouse up and down, then if you get a value, that then tells us, that the problem lies with the if statement, if it still returns 0, then there is a problem with unity. Here is what the if statement says:
if (Came.transform.eulerAngles.x + (Input.GetAxis("Mouse Y") * Time.deltaTime * RotationSpeed) < MaxAngle) {
print(Input.GetAxis("Mouse Y"));
Came.transform.eulerAngles += new Vector3(Input.GetAxis("Mouse Y"), 0, 0) * Time.deltaTime * RotationSpeed;
}
Everything here is written out of my style (which is fine, it is just harder to understand), and there are no obvious errors, except for the code inside the parentheses. They say:
(Came.transform.eulerAngles.x + (Input.GetAxis("Mouse Y") * Time.deltaTime * RotationSpeed) < MaxAngle)
The only thing I can spot is that you are only checking if the x rotation goes over. This won’t work the way you wanted to, because it wouldn’t be cow clamped when it goes under to. Since we can’t find the error, (someone might) we should just rewrite it.
Instead of putting an if statement, just use Mathf.Clamp(v, a, b); Mathf.Clamp clamps a number, v, so it doesn’t go under a, or over b. This is useful for a camera script, because you don’t want it to rotate behind the player. You should try this method:
void Camera()
{
float MouseY = Input.GetAxis(“Mouse Y”) * Time.deltaTime * RotationSpeed;
Mathf.Clamp(MouseY, -89.5f, 89.5f);
Came.transform.localEulerAngles = new Vector3(MouseY, 0, 0);
print(MouseY);
}
I removed the if statement entirely because we use Mathf.Clamp. Now we don’t have to test if it goes over or under because if this. You should multiply Time.deltaTime and RotationSpeed earlier, because if you multiply it later, it will go above/below the clamp. I then use local Euler angles, because if the player rig rotates, then so does the camera, and local means https://docs.unity3d.com/ScriptReference/Transform-localEulerAngles.html. I then use the print statement just to test everything.
I hope this helps, if something doesn’t work, I’ll be sure to edit this post.
I am having some trouble with a piece of code that I am working on in C#.
What this code is meant to do is move the player up and forward so that he is standing on a 1x1x1 block in front of him and not allow him to get over anything that is 1x2x1 or higher. but I am having a problem , Image. The further away I get from 0,0,0 the worse it gets. could anyone help me?
Code:
//Player Climbs Up One Block Heights But Dose Not Climb Anything Higher
if ((PlayerClimAction.posOneHit == true) && (PlayerClimAction.posTwoHit == false))
{
// Moves the Player Up By 0.9
controller.Move ((transform.position + transform.up * (float) 0.9f) * Time.deltaTime);
// Moves The Player Forward By 0.9
controller.Move ((transform.TransformDirection (input) * (float) 0.9f) * Time.deltaTime);
//Debug
print("Up Vector3: " + (transform.position + transform.up * (float) 0.9f) * Time.deltaTime);
print("////////////////////////////////////////////////");
print ("Forward: " + (transform.TransformDirection (input) * (float) 0.9f) * Time.deltaTime);
// Reset Triggers
PlayerClimAction.posOneHit = false;
PlayerClimAction.posTwoHit = false;
}else
{
controller.Move (motion * Time.deltaTime); // Move Normaly
}
Big thanks to Tone for the information. Thanks to that I was able to edit my code and change it to this:
//Player Climbs Up One Block Heights But Dose Not Climb Anything Higher
if ((PlayerClimAction.posOneHit == true) && (PlayerClimAction.posTwoHit == false))
{
// Moves the Player Up By 0.5
controller.Move ((transform.up * (float) 0.5f));
// Moves The Player Forward
controller.Move (motion * Time.deltaTime); // Move Normaly
// Reset Triggers
PlayerClimAction.posOneHit = false;
PlayerClimAction.posTwoHit = false;
}else
{
controller.Move (motion * Time.deltaTime); // Move Normaly
}
I changes the forward movement in the climb to just be a duplicate of the regular movement in the else statement, because that is the distance and speed that the player would move normally, and because of Tone's suggestion that I should change controller.Move ((transform.position + transform.up * (float) 0.9f) * Time.deltaTime); to controller.Move (transform.up * (float) 0.9f * Time.deltaTime); really helped me out in smoothing things out.
TY to all who looked at the post.
On the assumption that the argument to controller.Move is a movement vector rather than an absolute position (that's what the rest of your code suggests)
The line
controller.Move ((transform.position + transform.up * (float) 0.9f) * Time.deltaTime);
should read
controller.Move (transform.up * (float) 0.9f * Time.deltaTime);
If on the other hand the argument to controller.Move should be an absolute position you have several issues
a) the first call to controller.Move multiplies transform.position by Time.deltaTime. This will result in movement on all axes (and is why it gets worse the further away from 0,0,0)
b) the second and third calls to controller.Move don't include transform.Position in the calculation (depending on what transform.TransformDirection and mostion represent). Also I assume that is a typo and should be motion.